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
- JSON_POINTER_TOKEN_ESCAPE_CHARS =
{ '~' => '~0', '/' => '~1' }
- JSON_POINTER_TOKEN_ESCAPE_REGEXP =
Regexp.union(JSON_POINTER_TOKEN_ESCAPE_CHARS.keys)
Constants included from Format
Format::DATE_TIME_OFFSET_REGEX, Format::EMAIL_REGEX, Format::HOUR_24_REGEX, Format::INVALID_QUERY_REGEX, Format::IP_REGEX, Format::JSON_POINTER_REGEX, Format::JSON_POINTER_REGEX_STRING, Format::LEAP_SECOND_REGEX, Format::RELATIVE_JSON_POINTER_REGEX
Constants included from Format::Hostname
Format::Hostname::ARABIC_EXTENDED_DIGITS_REGEX, Format::Hostname::ARABIC_INDIC_DIGITS_REGEX, Format::Hostname::CONTEXT_REGEX, Format::Hostname::EXCEPTIONS_DISALLOWED, Format::Hostname::EXCEPTIONS_PVALID, Format::Hostname::GREEK_LOWER_NUMERAL_SIGN, Format::Hostname::HEBREW_PUNCTUATION, Format::Hostname::HOSTNAME_REGEX, Format::Hostname::JOINING_TYPE_D_CHARACTER_CLASS, Format::Hostname::JOINING_TYPE_L_CHARACTER_CLASS, Format::Hostname::JOINING_TYPE_R_CHARACTER_CLASS, Format::Hostname::JOINING_TYPE_T_CHARACTER_CLASS, Format::Hostname::KATAKANA_MIDDLE_DOT_CONTEXT_REGEX, Format::Hostname::KATAKANA_MIDDLE_DOT_REGEX, Format::Hostname::LABEL_CHARACTER_CLASS, Format::Hostname::LABEL_REGEX_STRING, Format::Hostname::LEADING_CHARACTER_CLASS, Format::Hostname::LETTER_DIGITS, Format::Hostname::MARKS, Format::Hostname::MIDDLE_DOT, Format::Hostname::VIRAMA_CHARACTER_CLASS, Format::Hostname::ZERO_WIDTH_NON_JOINER_JOINING_TYPE, Format::Hostname::ZERO_WIDTH_VIRAMA
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_ip?, #valid_json?, #valid_json_pointer?, #valid_relative_json_pointer?, #valid_spec_format?, #valid_uri?, #valid_uri_reference?, #valid_uri_template?
Methods included from Format::Hostname
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.
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 |
# File 'lib/json_schemer/schema/base.rb', line 50 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
80 81 82 |
# File 'lib/json_schemer/schema/base.rb', line 80 def valid?(data) valid_instance?(Instance.new(data, '', root, '', nil, @before_property_validation, @after_property_validation)) end |
#validate(data) ⇒ Object
84 85 86 |
# File 'lib/json_schemer/schema/base.rb', line 84 def validate(data) validate_instance(Instance.new(data, '', root, '', nil, @before_property_validation, @after_property_validation)) end |