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



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

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

.data_valid_for_type?(data, type) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

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



6
7
# 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



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

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



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

def self.validation_errors(validator)
  validator.validation_errors
end