Class: JsonSchema::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schema/parser.rb

Constant Summary collapse

ALLOWED_TYPES =
%w{any array boolean integer number null object string}
BOOLEAN =
[FalseClass, TrueClass]
FORMATS =
JsonSchema::Validator::DEFAULT_FORMAT_VALIDATORS.keys
FRIENDLY_TYPES =
{
  Array      => "array",
  FalseClass => "boolean",
  Float      => "number",
  Hash       => "object",
  Integer    => "integer",
  NilClass   => "null",
  String     => "string",
  TrueClass  => "boolean",
}
EMPTY_ARRAY =

Reuse these frozen objects to avoid allocations

[].freeze
EMPTY_HASH =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors


24
25
26
# File 'lib/json_schema/parser.rb', line 24

def errors
  @errors
end

Instance Method Details

#parse(data, parent = nil) ⇒ Object

Basic parsing of a schema. May return a malformed schema! (Use `#parse!` to raise errors instead).


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/json_schema/parser.rb', line 28

def parse(data, parent = nil)
  # while #parse_data is recursed into for many schemas over the same
  # object, the @errors array is an instance-wide accumulator
  @errors = []

  schema = parse_data(data, parent, "#")
  if @errors.count == 0
    schema
  else
    nil
  end
end

#parse!(data, parent = nil) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/json_schema/parser.rb', line 41

def parse!(data, parent = nil)
  schema = parse(data, parent)
  if !schema
    raise AggregateError.new(@errors)
  end
  schema
end