Module: JTD

Defined in:
lib/jtd.rb,
lib/jtd/schema.rb,
lib/jtd/version.rb,
lib/jtd/validate.rb

Overview

JTD is an implementation of JSON Type Definition validation for Ruby.

Defined Under Namespace

Classes: MaxDepthExceededError, Schema, ValidationError, ValidationOptions

Constant Summary collapse

VERSION =

The version of the jtd gem you are using.

'0.1.6'

Class Method Summary collapse

Class Method Details

.validate(schema, instance, options = ValidationOptions.new) ⇒ Object

Validates instance against schema according to the JSON Type Definition specification.

Returns a list of ValidationError. If there are no validation errors, then the returned list will be empty.

By default, all errors are returned, and an unlimited number of references will be followed. If you are running #validate against schemas that may return a lot of errors, or which may contain circular references, then this can cause performance issues or stack overflows.

To mitigate this risk, consider using options, which must be an instance of ValidationOptions, to limit the number of errors returned or references followed.

If ValidationOptions#max_depth is reached, then #validate will raise a MaxDepthExceededError.

The return value of #validate is not well-defined if the schema is not valid, i.e. Schema#verify raises an error.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jtd/validate.rb', line 24

def self.validate(schema, instance, options = ValidationOptions.new)
  state = ValidationState.new
  state.options = options
  state.root_schema = schema
  state.instance_tokens = []
  state.schema_tokens = [[]]
  state.errors = []

  begin
    validate_with_state(state, schema, instance)
  rescue MaxErrorsReachedError
    # This is just a dummy error to immediately stop validation. We swallow
    # the error here, and return the abridged set of errors.
  end

  state.errors
end