Class: SchemaTest::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_test/validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(json_or_data) ⇒ Validator

Returns a new instance of Validator.



7
8
9
10
11
12
13
14
15
16
# File 'lib/schema_test/validator.rb', line 7

def initialize(json_or_data)
  @data = case json_or_data
          when Hash, Array
            JSON.parse(json_or_data.to_json)
          when String
            JSON.parse(json_or_data)
          else
            json_or_data
          end
end

Instance Method Details

#validate_using_definition(definition) ⇒ Object



18
19
20
# File 'lib/schema_test/validator.rb', line 18

def validate_using_definition(definition)
  validate_using_json_schema(definition.as_json_schema)
end

#validate_using_json_schema(schema) ⇒ Object



22
23
24
25
26
# File 'lib/schema_test/validator.rb', line 22

def validate_using_json_schema(schema)
  json_schema = JSONSchemer.schema(schema)
  errors = json_schema.validate(@data).to_a
  convert_json_schemer_errors(errors)
end