Module: SchemaTools::Modules::Validations::ClassMethods
- Defined in:
- lib/schema_tools/modules/validations.rb
Instance Method Summary collapse
- #validate_length_opts(attr, is_required = false) ⇒ Object
- #validate_number_opts(attr, is_required = false) ⇒ Object
- #validate_with(schema, opts = {}) ⇒ Object
Instance Method Details
#validate_length_opts(attr, is_required = false) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/schema_tools/modules/validations.rb', line 46 def validate_length_opts(attr, is_required=false) opts = {} opts[:within] = attr['minLength']..attr['maxLength'] if attr['minLength'] && attr['maxLength'] opts[:maximum] = attr['maxLength'] if attr['maxLength'] && !attr['minLength'] opts[:minimum] = attr['minLength'] if attr['minLength'] && !attr['maxLength'] opts[:allow_blank] = true unless is_required opts end |
#validate_number_opts(attr, is_required = false) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/schema_tools/modules/validations.rb', line 56 def validate_number_opts(attr, is_required=false) opts = {} opts[:allow_blank] = true unless is_required # those vals should not be set both in one property opts[:greater_than_or_equal_to] = attr['minimum'] if attr['minimum'].present? opts[:less_than_or_equal_to] = attr['maximum'] if attr['maximum'].present? opts[:less_than] = attr['exclusiveMinimum'] if attr['exclusiveMinimum'].present? opts[:greater_than] = attr['exclusiveMaximum'] if attr['exclusiveMaximum'].present? opts end |
#validate_with(schema, opts = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/schema_tools/modules/validations.rb', line 32 def validate_with(schema, opts={}) reader = opts[:reader] || SchemaTools::Reader schema = reader.read(schema, opts[:path]) # create validation methods schema[:properties].each do |key, val| is_required = schema['required'] && schema['required'].include?("#{key}") validates_length_of key, validate_length_opts(val, is_required) if val['maxLength'] || val['minLength'] validates_presence_of key if is_required validates_numericality_of key, validate_number_opts(val, is_required) if val['type'] == 'number' || val['type'] == 'integer' #TODO array minItems, max unique, null, string # format: date-time, regex color style, email,uri, .. end end |