Class: JSONSchemer::Schema::Base
- Inherits:
-
Object
- Object
- JSONSchemer::Schema::Base
- Includes:
- Format
- Defined in:
- lib/json_schemer/schema/base.rb
Defined Under Namespace
Classes: Instance
Constant Summary collapse
- ID_KEYWORD =
'$id'- DEFAULT_REF_RESOLVER =
proc { |uri| raise UnknownRef, uri.to_s }
- NET_HTTP_REF_RESOLVER =
proc { |uri| JSON.parse(Net::HTTP.get(uri)) }
- BOOLEANS =
Set[true, false].freeze
- RUBY_REGEX_ANCHORS_TO_ECMA_262 =
{ :bos => 'A', :eos => 'z', :bol => '\A', :eol => '\z' }.freeze
- ECMA_262_REGEXP_RESOLVER =
proc do |pattern| Regexp.new( Regexp::Scanner.scan(pattern).map do |type, token, text| type == :anchor ? RUBY_REGEX_ANCHORS_TO_ECMA_262.fetch(token, text) : text end.join ) end
- INSERT_DEFAULT_PROPERTY =
proc do |data, property, property_schema, _parent| if !data.key?(property) && property_schema.is_a?(Hash) && property_schema.key?('default') data[property] = property_schema.fetch('default').clone end end
Constants included from Format
Format::DATE_TIME_OFFSET_REGEX, Format::EMAIL_REGEX, Format::HOSTNAME_REGEX, Format::INVALID_QUERY_REGEX, Format::JSON_POINTER_REGEX, Format::JSON_POINTER_REGEX_STRING, Format::LABEL_REGEX_STRING, Format::RELATIVE_JSON_POINTER_REGEX
Instance Method Summary collapse
-
#initialize(schema, format: true, insert_property_defaults: false, before_property_validation: nil, after_property_validation: nil, formats: nil, keywords: nil, ref_resolver: DEFAULT_REF_RESOLVER, regexp_resolver: 'ecma') ⇒ Base
constructor
A new instance of Base.
- #valid?(data) ⇒ Boolean
- #validate(data) ⇒ Object
Methods included from Format
#iri_escape, #parse_uri_scheme, #valid_date_time?, #valid_email?, #valid_hostname?, #valid_ip?, #valid_json?, #valid_json_pointer?, #valid_relative_json_pointer?, #valid_spec_format?, #valid_uri?, #valid_uri_reference?, #valid_uri_template?
Constructor Details
#initialize(schema, format: true, insert_property_defaults: false, before_property_validation: nil, after_property_validation: nil, formats: nil, keywords: nil, ref_resolver: DEFAULT_REF_RESOLVER, regexp_resolver: 'ecma') ⇒ Base
Returns a new instance of Base.
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 |
# File 'lib/json_schemer/schema/base.rb', line 47 def initialize( schema, format: true, insert_property_defaults: false, before_property_validation: nil, after_property_validation: nil, formats: nil, keywords: nil, ref_resolver: DEFAULT_REF_RESOLVER, regexp_resolver: 'ecma' ) raise InvalidSymbolKey, 'schemas must use string keys' if schema.is_a?(Hash) && !schema.empty? && !schema.first.first.is_a?(String) @root = schema @format = format @before_property_validation = [*before_property_validation] @before_property_validation.unshift(INSERT_DEFAULT_PROPERTY) if insert_property_defaults @after_property_validation = [*after_property_validation] @formats = formats @keywords = keywords @ref_resolver = ref_resolver == 'net/http' ? CachedResolver.new(&NET_HTTP_REF_RESOLVER) : ref_resolver @regexp_resolver = case regexp_resolver when 'ecma' CachedResolver.new(&ECMA_262_REGEXP_RESOLVER) when 'ruby' CachedResolver.new(&Regexp.method(:new)) else regexp_resolver end end |
Instance Method Details
#valid?(data) ⇒ Boolean
77 78 79 |
# File 'lib/json_schemer/schema/base.rb', line 77 def valid?(data) valid_instance?(Instance.new(data, '', root, '', nil, @before_property_validation, @after_property_validation)) end |
#validate(data) ⇒ Object
81 82 83 |
# File 'lib/json_schemer/schema/base.rb', line 81 def validate(data) validate_instance(Instance.new(data, '', root, '', nil, @before_property_validation, @after_property_validation)) end |