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]
FRIENDLY_TYPES =
{
  Array      => "array",
  FalseClass => "boolean",
  Float      => "number",
  Hash       => "object",
  Integer    => "integer",
  NilClass   => "null",
  String     => "string",
  TrueClass  => "boolean",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



18
19
20
# File 'lib/json_schema/parser.rb', line 18

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).



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/json_schema/parser.rb', line 22

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



35
36
37
38
39
40
41
# File 'lib/json_schema/parser.rb', line 35

def parse!(data, parent = nil)
  schema = parse(data, parent)
  if !schema
    raise SchemaError.aggregate(@errors).join(" ")
  end
  schema
end