Custom module Validation (written in Ruby without Rails) which:
-
Contains the class method
validate. This method takes as parameters the name of the attribute being checked, as well as the type of validation and, if necessary, additional parameters. Possible types of validations:presence- requires the attribute value to be non-nil and not an empty string. Usage example:validate :name, presence: trueformat(with a separate parameter specifying a regular expression for the format). Requires an attribute value to match a specified regular expression. Example:validate :number, format: /A-Z{0,3}/type(the third parameter is the attribute class). Requires the attribute value to match the specified class. Example:validate :owner, type: User
-
Contains an instance method
validate!, which runs all the checks (validations) specified in the class via the class methodvalidate. In case of a validation error, it throws an exception with a message about which validation failed. -
Contains an instance method
valid?which returnstrueif all validation checks are successful andfalseif there are validation errors.
This Validation module is connected to ValidationTest and demonstrates it usage.
- Ruby executable script
- All required gems installed with
Bundler - Code style is provided via RuboCop
- System: Linux, Mac
- Git
- Ruby version manager (
rbenvorRVM) - Ruby 2.6.2
Bundler- Gems installed via Bundler Gemfile
Clone with SSH:
$ git clone [email protected]:alex-petr/validation_module.gitOr clone with HTTPS:
$ git clone https://github.com/alex-petr/validation_module.git$ cd validation_module/ && brew install rbenv$ rbenv install 2.6.2$ gem install bundler && bundleNo automated test suite based on testing frameworks like RSpec, minitest or test-unit is available.
Instead of this ValidationTest class contains custom testing code for Validation module.
$ bin/validation