json-ld-validate 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/jsonld_validate/validator.rb +64 -8
- data/lib/jsonld_validate/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3b842c8a5568175c193e71d16e26fa41bab31e1bae79143487620096fa890ec4
|
|
4
|
+
data.tar.gz: 6daf575322e3247089f2f3e4d0ed9fc4b8959accfd5254de9e15261a0320d566
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3060a4e2e85af2ae2ef40ffb1a20c95c2864ee1ecf51dfeba746034e0c313d2ab51bd462febf4951dc6f8a21a4549fd63fa3c23eb866dc58a1d307d582d03306
|
|
7
|
+
data.tar.gz: 54fadf3ffd1807c70223c637afe7b81fa4d6adbcdf9060baafe372b7a44b7448f20df13249d36aa2ea1d0746f69167c773217609b3ff31298703ce95c1911006
|
|
@@ -13,27 +13,83 @@ module JsonldValidate
|
|
|
13
13
|
return false if content.nil?
|
|
14
14
|
return true if content.empty?
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
build_expanded_doc
|
|
17
|
+
build_doc
|
|
17
18
|
return false unless errors.empty?
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
find_unknown_fields(content, doc, [])
|
|
20
21
|
|
|
21
22
|
errors.empty?
|
|
22
23
|
end
|
|
23
24
|
|
|
24
25
|
private
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
attr_reader :doc, :expanded_doc
|
|
28
|
+
|
|
29
|
+
def build_doc
|
|
30
|
+
return if expanded_doc.nil?
|
|
31
|
+
|
|
32
|
+
@doc = JSON::LD::API.compact(expanded_doc, content['@context'])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def build_expanded_doc
|
|
36
|
+
@expanded_doc = clean_expanded_doc(JSON::LD::API.expand(content))
|
|
28
37
|
rescue JSON::LD::JsonLdError::LoadingDocumentFailed
|
|
29
38
|
@errors << 'failed loading document'
|
|
39
|
+
rescue JSON::LD::JsonLdError::InvalidLanguageMapValue => e
|
|
40
|
+
@errors << e.to_s
|
|
30
41
|
end
|
|
31
42
|
|
|
32
|
-
def
|
|
33
|
-
return
|
|
43
|
+
def find_unknown_fields(original, compacted, path)
|
|
44
|
+
return unless original.is_a?(Hash)
|
|
45
|
+
return report_all_fields(original, path) unless compacted.is_a?(Hash)
|
|
46
|
+
|
|
47
|
+
report_unknown_keys(original, compacted, path)
|
|
48
|
+
recurse_known_keys(original, compacted, path)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def report_all_fields(original, path)
|
|
52
|
+
original.each_key do |key|
|
|
53
|
+
next if key.start_with?('@')
|
|
54
|
+
|
|
55
|
+
errors << "found unknown fields: #{(path + [key]).join('.')}"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def report_unknown_keys(original, compacted, path)
|
|
60
|
+
(original.keys - compacted.keys).each do |field|
|
|
61
|
+
errors << "found unknown fields: #{(path + [field]).join('.')}"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def recurse_known_keys(original, compacted, path)
|
|
66
|
+
(original.keys & compacted.keys).each do |key|
|
|
67
|
+
recurse_field(original[key], compacted[key], path + [key])
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def recurse_field(orig_val, comp_val, path)
|
|
72
|
+
if orig_val.is_a?(Array)
|
|
73
|
+
comp_val = [] unless comp_val.is_a?(Array)
|
|
74
|
+
orig_val.each_with_index do |item, i|
|
|
75
|
+
find_unknown_fields(item, comp_val[i], path + [i.to_s])
|
|
76
|
+
end
|
|
77
|
+
else
|
|
78
|
+
find_unknown_fields(orig_val, comp_val, path)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
34
81
|
|
|
35
|
-
|
|
36
|
-
|
|
82
|
+
def clean_expanded_doc(obj)
|
|
83
|
+
case obj
|
|
84
|
+
when Array
|
|
85
|
+
obj.map { |item| clean_expanded_doc(item) }
|
|
86
|
+
when Hash
|
|
87
|
+
obj.each_with_object({}) do |(k, v), h|
|
|
88
|
+
h[k] = clean_expanded_doc(v) unless k.start_with?('_:')
|
|
89
|
+
end
|
|
90
|
+
else
|
|
91
|
+
obj
|
|
92
|
+
end
|
|
37
93
|
end
|
|
38
94
|
end
|
|
39
95
|
end
|