Class: Philiprehberger::JsonSchema::Validator

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

Overview

Core validation engine that checks data against a JSON Schema (draft-07 subset)

Instance Method Summary collapse

Instance Method Details

#validate(data, schema, path: '$', root_schema: nil) ⇒ Array<String>

Validate data against a schema

Parameters:

  • data (Object)

    the data to validate

  • schema (Hash)

    the JSON Schema

  • path (String) (defaults to: '$')

    the current path for error reporting

  • root_schema (Hash, nil) (defaults to: nil)

    the root schema for $ref resolution

Returns:

  • (Array<String>)

    list of validation error messages



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/philiprehberger/json_schema/validator.rb', line 16

def validate(data, schema, path: '$', root_schema: nil)
  root_schema ||= schema
  errors = []

  validate_type(data, schema, path, errors)
  validate_const(data, schema, path, errors)
  validate_required(data, schema, path, errors)
  validate_object_size(data, schema, path, errors)
  validate_properties(data, schema, path, errors, root_schema)
  validate_additional_properties(data, schema, path, errors, root_schema)
  validate_pattern_properties(data, schema, path, errors, root_schema)
  validate_pattern(data, schema, path, errors)
  validate_string_length(data, schema, path, errors)
  validate_numeric_range(data, schema, path, errors)
  validate_exclusive_range(data, schema, path, errors)
  validate_multiple_of(data, schema, path, errors)
  validate_enum(data, schema, path, errors)
  validate_items(data, schema, path, errors, root_schema)
  validate_array_length(data, schema, path, errors)
  validate_unique_items(data, schema, path, errors)
  validate_ref(data, schema, path, errors, root_schema)
  validate_all_of(data, schema, path, errors, root_schema)
  validate_any_of(data, schema, path, errors, root_schema)
  validate_one_of(data, schema, path, errors, root_schema)
  validate_not(data, schema, path, errors, root_schema)
  validate_if_then_else(data, schema, path, errors, root_schema)

  errors
end