Class: JSONSchemer::Schema::Base

Inherits:
Object
  • Object
show all
Includes:
Format
Defined in:
lib/json_schemer/schema/base.rb

Direct Known Subclasses

Draft4, Draft6, Draft7

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

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

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, formats: nil, keywords: nil, ref_resolver: DEFAULT_REF_RESOLVER) ⇒ Base

Returns a new instance of Base.

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/json_schemer/schema/base.rb', line 32

def initialize(
  schema,
  format: true,
  insert_property_defaults: false,
  formats: nil,
  keywords: nil,
  ref_resolver: DEFAULT_REF_RESOLVER
)
  raise InvalidSymbolKey, 'schemas must use string keys' if schema.is_a?(Hash) && !schema.empty? && !schema.first.first.is_a?(String)
  @root = schema
  @format = format
  @insert_property_defaults = insert_property_defaults
  @formats = formats
  @keywords = keywords
  @ref_resolver = ref_resolver == 'net/http' ? CachedRefResolver.new(&NET_HTTP_REF_RESOLVER) : ref_resolver
end

Instance Method Details

#valid?(data) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/json_schemer/schema/base.rb', line 49

def valid?(data)
  valid_instance?(Instance.new(data, '', root, '', nil, !!@insert_property_defaults))
end

#validate(data) ⇒ Object



53
54
55
# File 'lib/json_schemer/schema/base.rb', line 53

def validate(data)
  validate_instance(Instance.new(data, '', root, '', nil, !!@insert_property_defaults))
end