Class: JSON::Schema::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/json-schema/attribute.rb

Constant Summary collapse

TYPE_CLASS_MAPPINGS =
{
  'string' => String,
  'number' => Numeric,
  'integer' => Integer,
  'boolean' => [TrueClass, FalseClass],
  'object' => Hash,
  'array' => Array,
  'null' => NilClass,
  'any' => Object,
}

Class Method Summary collapse

Class Method Details

.build_fragment(fragments) ⇒ Object



8
9
10
# File 'lib/json-schema/attribute.rb', line 8

def self.build_fragment(fragments)
  "#/#{fragments.join('/')}"
end

.data_valid_for_type?(data, type) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/json-schema/attribute.rb', line 36

def self.data_valid_for_type?(data, type)
  valid_classes = TYPE_CLASS_MAPPINGS.fetch(type) { return true }
  Array(valid_classes).any? { |c| data.is_a?(c) }
end

.type_of_data(data) ⇒ Object

Lookup Schema type of given class instance



42
43
44
45
46
47
48
49
# File 'lib/json-schema/attribute.rb', line 42

def self.type_of_data(data)
  type, = TYPE_CLASS_MAPPINGS.map { |k, v| [k, v] }.sort_by do |(_, v)|
    -Array(v).map { |klass| klass.ancestors.size }.max
  end.find do |(_, v)|
    Array(v).any? { |klass| data.is_a?(klass) }
  end
  type
end

.validate(current_schema, data, fragments, processor, validator, options = {}) ⇒ Object



6
# File 'lib/json-schema/attribute.rb', line 6

def self.validate(current_schema, data, fragments, processor, validator, options = {}); end

.validation_error(processor, message, fragments, current_schema, failed_attribute, record_errors) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/json-schema/attribute.rb', line 12

def self.validation_error(processor, message, fragments, current_schema, failed_attribute, record_errors)
  error = ValidationError.new(message, fragments, failed_attribute, current_schema)
  if record_errors
    processor.validation_error(error)
  else
    raise error
  end
end

.validation_errors(validator) ⇒ Object



21
22
23
# File 'lib/json-schema/attribute.rb', line 21

def self.validation_errors(validator)
  validator.validation_errors
end