Class: JSONSchemer::Schema
- Inherits:
-
Object
- Object
- JSONSchemer::Schema
- Defined in:
- lib/json_schemer.rb
Constant Summary collapse
- BOOLEANS =
Set[true, false].freeze
- EMAIL_REGEX =
this is no good
/\A[^@\s]+@([\p{L}\d-]+\.)+[\p{L}\d\-]{2,}\z/i.freeze
- LABEL_REGEX_STRING =
'\p{L}([\p{L}\p{N}\-]*[\p{L}\p{N}])?'- HOSTNAME_REGEX =
/\A(#{LABEL_REGEX_STRING}\.)*#{LABEL_REGEX_STRING}\z/i.freeze
- JSON_POINTER_REGEX_STRING =
'(\/([^~\/]|~[01])*)*'- JSON_POINTER_REGEX =
/\A#{JSON_POINTER_REGEX_STRING}\z/.freeze
- RELATIVE_JSON_POINTER_REGEX =
/\A(0|[1-9]\d*)(#|#{JSON_POINTER_REGEX_STRING})?\z/.freeze
Instance Method Summary collapse
-
#initialize(schema) ⇒ Schema
constructor
A new instance of Schema.
- #valid?(data, schema = root, pointer = '#', parent_uri = nil) ⇒ Boolean
- #validate(data, schema = root, pointer = '#', parent_uri = nil) {|error(data, schema, pointer, 'enum')| ... } ⇒ Object
Constructor Details
#initialize(schema) ⇒ Schema
Returns a new instance of Schema.
27 28 29 |
# File 'lib/json_schemer.rb', line 27 def initialize(schema) @root = schema end |
Instance Method Details
#valid?(data, schema = root, pointer = '#', parent_uri = nil) ⇒ Boolean
31 32 33 |
# File 'lib/json_schemer.rb', line 31 def valid?(data, schema = root, pointer = '#', parent_uri = nil) validate(data, schema, pointer, parent_uri).none? end |
#validate(data, schema = root, pointer = '#', parent_uri = nil) {|error(data, schema, pointer, 'enum')| ... } ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/json_schemer.rb', line 35 def validate(data, schema = root, pointer = '#', parent_uri = nil) return enum_for(:validate, data, schema, pointer, parent_uri) unless block_given? return if schema == true if schema == false yield error(data, schema, pointer, 'schema') return end return if schema.empty? type = schema['type'] enum = schema['enum'] all_of = schema['allOf'] any_of = schema['anyOf'] one_of = schema['oneOf'] not_schema = schema['not'] if_schema = schema['if'] then_schema = schema['then'] else_schema = schema['else'] ref = schema['$ref'] id = schema['$id'] parent_uri = join_uri(parent_uri, id) if ref validate_ref(data, schema, pointer, parent_uri, ref, &Proc.new) return end yield error(data, schema, pointer, 'enum') if enum && !enum.include?(data) yield error(data, schema, pointer, 'const') if schema.key?('const') && schema['const'] != data yield error(data, schema, pointer, 'allOf') if all_of && !all_of.all? { |subschema| valid?(data, subschema, pointer, parent_uri) } yield error(data, schema, pointer, 'anyOf') if any_of && !any_of.any? { |subschema| valid?(data, subschema, pointer, parent_uri) } yield error(data, schema, pointer, 'oneOf') if one_of && !one_of.one? { |subschema| valid?(data, subschema, pointer, parent_uri) } yield error(data, schema, pointer, 'not') if !not_schema.nil? && valid?(data, not_schema, pointer, parent_uri) if if_schema && valid?(data, if_schema, pointer, parent_uri) yield error(data, schema, pointer, 'then') if !then_schema.nil? && !valid?(data, then_schema, pointer, parent_uri) elsif if_schema yield error(data, schema, pointer, 'else') if !else_schema.nil? && !valid?(data, else_schema, pointer, parent_uri) end case type when nil validate_class(data, schema, pointer, parent_uri, &Proc.new) when String validate_type(data, schema, pointer, parent_uri, type, &Proc.new) when Array if valid_type = type.find { |subtype| valid?(data, { 'type' => subtype }, pointer, parent_uri) } validate_type(data, schema, pointer, parent_uri, valid_type, &Proc.new) else yield error(data, schema, pointer, 'type') end end end |