Class: Pastore::Params::Validation
- Inherits:
-
Object
- Object
- Pastore::Params::Validation
- Defined in:
- lib/pastore/params/validation.rb
Overview
Implements the logic of a single param validation
Direct Known Subclasses
BooleanValidation, DateValidation, NumberValidation, ObjectValidation, StringValidation
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.validate!(name, type, value, modifier = nil, **options) ⇒ Object
Validates the value based on the given type and values with the appropriate validator.
Instance Method Summary collapse
-
#add_error(error_type, message) ⇒ Object
Adds an error to the list of errors.
-
#initialize(name, type, value, modifier = nil, **options) ⇒ Validation
constructor
A new instance of Validation.
-
#required? ⇒ Boolean
Returns true if the value is required, false otherwise.
-
#valid? ⇒ Boolean
Returns true if the value is valid, false otherwise.
Constructor Details
#initialize(name, type, value, modifier = nil, **options) ⇒ Validation
Returns a new instance of Validation.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pastore/params/validation.rb', line 29 def initialize(name, type, value, modifier = nil, **) @name = name @type = type @modifier = modifier @value = value.nil? ? [:default] : value @required = ([:required] == true) # default: false @allow_blank = ([:allow_blank].nil? || [:allow_blank]) # default: true @allowed_values = [:in] @exclude_values = [:exclude] @errors = [] validate! end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
27 28 29 |
# File 'lib/pastore/params/validation.rb', line 27 def errors @errors end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
27 28 29 |
# File 'lib/pastore/params/validation.rb', line 27 def value @value end |
Class Method Details
.validate!(name, type, value, modifier = nil, **options) ⇒ Object
Validates the value based on the given type and values with the appropriate validator.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/pastore/params/validation.rb', line 14 def self.validate!(name, type, value, modifier = nil, **) case type when 'string' then Pastore::Params::StringValidation.new(name, value, modifier, **) when 'number' then Pastore::Params::NumberValidation.new(name, value, modifier, **) when 'boolean' then Pastore::Params::BooleanValidation.new(name, value, modifier, **) when 'object' then Pastore::Params::ObjectValidation.new(name, value, modifier, **) when 'date' then Pastore::Params::DateValidation.new(name, value, modifier, **) when 'any' then Validation.new(name, 'any', value, modifier, **) else raise Pastore::Params::InvalidValidationTypeError, "Invalid validation type: #{type}" end end |
Instance Method Details
#add_error(error_type, message) ⇒ Object
Adds an error to the list of errors.
55 56 57 |
# File 'lib/pastore/params/validation.rb', line 55 def add_error(error_type, ) @errors << { type: 'param', name: @name, value: @value, error: error_type, message: } end |
#required? ⇒ Boolean
Returns true if the value is required, false otherwise.
50 51 52 |
# File 'lib/pastore/params/validation.rb', line 50 def required? @required end |
#valid? ⇒ Boolean
Returns true if the value is valid, false otherwise.
45 46 47 |
# File 'lib/pastore/params/validation.rb', line 45 def valid? @errors.empty? end |