Class: JSONSkooma::Keywords::FormatAnnotation::Format

Inherits:
Base
  • Object
show all
Defined in:
lib/json_skooma/keywords/format_annotation/format.rb

Instance Attribute Summary

Attributes inherited from Base

#json, #parent_schema

Instance Method Summary collapse

Methods inherited from Base

#each_schema, inherited, #instance_types, #key, #resolve, set_defaults, #static, value_schema=

Constructor Details

#initialize(parent_schema, value) ⇒ Format

Returns a new instance of Format.



9
10
11
12
13
14
# File 'lib/json_skooma/keywords/format_annotation/format.rb', line 9

def initialize(parent_schema, value)
  super
  if parent_schema.registry.format_enabled?(value)
    @validator = Validators.validators[value]
  end
end

Instance Method Details

#evaluate(instance, result) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/json_skooma/keywords/format_annotation/format.rb', line 16

def evaluate(instance, result)
  result.annotate(json)
  return result.skip_assertion unless @validator&.assert?(instance)

  @validator.call(instance)
rescue Validators::FormatError => e
  result.failure("The instance is invalid against the #{json.value} format: #{e}")
end