Class: JDDF::Validator

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

Overview

Validates JSON instances against JDDF schemas.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#max_depthInteger

The maximum stack depth of references to follow when running #validate.

If this maximum depth is exceeded, such as if a schema passed to #validate is defined cyclically, then #validate throws a MaxDepthExceededError.

By default, no maximum depth is enforced. The validator may overflow the stack if a schema is defined cyclically.

Returns:

  • (Integer)

    the maximum depth of references to follow when validating


273
274
275
# File 'lib/jddf/validator.rb', line 273

def max_depth
  @max_depth
end

#max_errorsInteger

The maximum number of errors to return when running #validate.

If this value is set to a number, then it's guaranteed that #validate will return an array of size no greater than the value of this attribute.

By default, no maximum errors is enforced. All validation errors are returned.

Returns:

  • (Integer)

    the maximum errors to return when validating


284
285
286
# File 'lib/jddf/validator.rb', line 284

def max_errors
  @max_errors
end

Instance Method Details

#validate(schema, instance) ⇒ Array

Validate a JDDF schema against a JSON instance.

The precise rules of validation for this method are defined formally by the JDDF specification, and this method follows those rules exactly, assuming that instance is the result of calling JSON#parse using the standard library's JSON module.

Parameters:

  • schema (Schema)

    the schema to validate against

  • instance (Object)

    the input (“instance”) to validate

Returns:

Raises:


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/jddf/validator.rb', line 300

def validate(schema, instance)
  vm = VM.new
  vm.max_depth = max_depth
  vm.max_errors = max_errors
  vm.root_schema = schema
  vm.instance_tokens = []
  vm.schema_tokens = [[]]
  vm.errors = []

  begin
    vm.validate(schema, instance)
  rescue MaxErrorsError # rubocop:disable Lint/HandleExceptions
    # There is nothing to do here. MaxErrorsError is just a circuit-breaker.
  end

  vm.errors
end