Module: JsonldValidate::XsdTypeValidator

Included in:
Validator
Defined in:
lib/jsonld_validate/xsd_type_validator.rb

Constant Summary collapse

XSD =
'http://www.w3.org/2001/XMLSchema#'
INTEGER_TYPES =
%w[
  integer int long short byte
  unsignedInt unsignedLong unsignedShort unsignedByte
  nonNegativeInteger positiveInteger nonPositiveInteger negativeInteger
].to_set { |t| "#{XSD}#{t}" }.freeze
DECIMAL_TYPES =
%w[decimal double float].to_set { |t| "#{XSD}#{t}" }.freeze
BOOLEAN_TYPE =
"#{XSD}boolean".freeze

Instance Method Summary collapse

Instance Method Details

#build_iri_mapObject



24
25
26
27
28
29
30
31
32
# File 'lib/jsonld_validate/xsd_type_validator.rb', line 24

def build_iri_map
  ctx = JSON::LD::Context.parse(content['@context'])
  map = ctx.term_definitions.each_with_object({}) do |(term, defn), h|
    h[defn.id] = term if defn.id
  end
  [map, ctx.vocab]
rescue StandardError
  [{}, nil]
end

#check_node_types(node, iri_to_term, vocab, path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jsonld_validate/xsd_type_validator.rb', line 34

def check_node_types(node, iri_to_term, vocab, path)
  return unless node.is_a?(Hash)

  node.each do |iri, values|
    next if iri.start_with?('@')

    term = compact_term(iri, iri_to_term, vocab)
    Array(values).each_with_index do |value, i|
      child_path = Array(values).length > 1 ? path + [term, i.to_s] : path + [term]
      check_typed_value(value, child_path, iri_to_term, vocab)
    end
  end
end

#check_typed_value(value, path, iri_to_term, vocab) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/jsonld_validate/xsd_type_validator.rb', line 48

def check_typed_value(value, path, iri_to_term, vocab)
  return unless value.is_a?(Hash)

  if value.key?('@value') && value.key?('@type')
    report_type_mismatch(value['@value'], value['@type'], path)
  else
    check_node_types(value, iri_to_term, vocab, path)
  end
end

#compact_term(iri, iri_to_term, vocab) ⇒ Object



76
77
78
79
80
81
# File 'lib/jsonld_validate/xsd_type_validator.rb', line 76

def compact_term(iri, iri_to_term, vocab)
  return iri_to_term[iri] if iri_to_term.key?(iri)
  return iri.delete_prefix(vocab) if vocab && iri.start_with?(vocab)

  iri
end

#report_type_mismatch(val, type_iri, path) ⇒ Object



58
59
60
61
62
# File 'lib/jsonld_validate/xsd_type_validator.rb', line 58

def report_type_mismatch(val, type_iri, path)
  return if valid_xsd_value?(val, type_iri)

  @errors << "invalid type for #{path.join('.')}: #{val.inspect}"
end

#valid_xsd_value?(val, type_iri) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jsonld_validate/xsd_type_validator.rb', line 64

def valid_xsd_value?(val, type_iri)
  if INTEGER_TYPES.include?(type_iri)
    val.is_a?(Integer)
  elsif DECIMAL_TYPES.include?(type_iri)
    val.is_a?(Numeric)
  elsif type_iri == BOOLEAN_TYPE
    [true, false].include?(val)
  else
    true
  end
end

#validate_typed_valuesObject



17
18
19
20
21
22
# File 'lib/jsonld_validate/xsd_type_validator.rb', line 17

def validate_typed_values
  return unless expanded_doc

  iri_to_term, vocab = build_iri_map
  expanded_doc.each { |node| check_node_types(node, iri_to_term, vocab, []) }
end