lutaml-model 0.8.203 → 0.8.205
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/.gitattributes +5 -0
- data/.github/workflows/lml-conformance.yml +71 -0
- data/.gitignore +7 -0
- data/README.adoc +1 -0
- data/docs/_guides/lml-cross-language.adoc +128 -0
- data/docs/_guides/native-engines.adoc +10 -0
- data/docs/_references/lml-conformance.adoc +78 -0
- data/docs/_references/lml-contract.adoc +290 -0
- data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
- data/lib/lutaml/model/lml/bundle.rb +171 -0
- data/lib/lutaml/model/lml/document.rb +46 -10
- data/lib/lutaml/model/lml/document_validator.rb +840 -0
- data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
- data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
- data/lib/lutaml/model/lml/exporter.rb +223 -87
- data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
- data/lib/lutaml/model/lml/importer.rb +180 -0
- data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
- data/lib/lutaml/model/lml/materialization.rb +257 -0
- data/lib/lutaml/model/lml/ordered_content.rb +577 -0
- data/lib/lutaml/model/lml/portable_json.rb +385 -0
- data/lib/lutaml/model/lml/rejected_error.rb +64 -15
- data/lib/lutaml/model/lml/round_trip.rb +86 -0
- data/lib/lutaml/model/lml/value_declarations.rb +84 -0
- data/lib/lutaml/model/lml.rb +61 -0
- data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
- data/lib/lutaml/model/schema/registry_generator.rb +1 -1
- data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
- data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
- data/lib/lutaml/model/sequence.rb +7 -0
- data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
- data/lib/lutaml/model/serialize/initialization.rb +2 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
- data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
- data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
- data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
- data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
- data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
- data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
- data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
- data/schema/lml/document.schema.json +179 -0
- data/scripts/lml/export_choices.rb +57 -0
- data/scripts/lml/export_conformance.rb +23 -0
- data/scripts/lml/export_mml.rb +253 -0
- data/scripts/lml/export_preservation.rb +421 -0
- data/scripts/lml/verify_artifacts.rb +31 -0
- data/spec/fixtures/lml_artifacts/model.json +1 -0
- data/spec/fixtures/lml_artifacts/valid.json +1 -0
- data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
- data/spec/fixtures/lml_conformance/core/index.json +1 -0
- data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
- data/spec/fixtures/lml_conformance/core/schema.json +1 -0
- data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
- data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
- data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
- data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
- data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
- data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
- data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
- data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
- data/spec/fixtures/lml_contract/bundle.json +81 -0
- data/spec/fixtures/lml_contract/manifest.json +30 -0
- data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
- data/spec/fixtures/lml_contract/v1_unit.json +1 -0
- data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
- data/spec/fixtures/lml_contract/v2_node.json +32 -0
- data/spec/fixtures/lml_mml/inventory.json +1 -0
- data/spec/fixtures/lml_mml/manifest.json +1 -0
- data/spec/fixtures/lml_mml/oracle.json +1 -0
- data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
- data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
- data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
- data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
- data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
- data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
- data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
- data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
- data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
- data/spec/lutaml/model/lml_document_spec.rb +279 -0
- data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
- data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
- data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
- data/spec/lutaml/model/lml_importer_spec.rb +290 -0
- data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
- data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
- data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
- data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
- data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
- data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
- data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
- data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
- data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
- data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
- data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
- data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
- data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
- data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
- data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
- data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
- data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
- data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
- data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
- data/spec/lutaml/model/sequence_spec.rb +48 -0
- data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
- data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
- data/spec/support/lml_any_attribute_fixtures.rb +26 -0
- data/spec/support/lml_conformance.rb +292 -0
- data/spec/support/lml_corpus.rb +75 -0
- data/spec/support/lml_export_fixtures.rb +190 -0
- data/spec/support/lml_graph_fixtures.rb +108 -0
- data/spec/support/lml_import_fixtures.rb +52 -0
- data/spec/support/lml_mapping_fixtures.rb +170 -0
- data/spec/support/lml_materialization_fixtures.rb +53 -0
- data/spec/support/lml_model_choices_fixtures.rb +150 -0
- data/spec/support/lml_order_fixtures.rb +151 -0
- metadata +94 -3
- data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
|
@@ -0,0 +1,840 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "rejected_error"
|
|
4
|
+
|
|
5
|
+
module Lutaml
|
|
6
|
+
module Model
|
|
7
|
+
module Lml
|
|
8
|
+
# Validates LML hashes against the agreed contract and returns a
|
|
9
|
+
# deeply frozen neutral tree. Raises RejectedError (named code,
|
|
10
|
+
# JSON-pointer location) on the first violation; never returns a
|
|
11
|
+
# tree with a feature silently dropped.
|
|
12
|
+
#
|
|
13
|
+
# DocumentValidator.validate!(hash, kind: :document | :bundle | :manifest)
|
|
14
|
+
#
|
|
15
|
+
# Schema-evolution policy: versions are exact. Each kind dispatches
|
|
16
|
+
# on its version field through a closed table; an unlisted version
|
|
17
|
+
# is `unsupported_version` and is never coerced, down-converted or
|
|
18
|
+
# defaulted. Version 1 documents are accepted indefinitely and
|
|
19
|
+
# unchanged (legacy tolerance: unknown keys are ignored, as the
|
|
20
|
+
# v1 Document always did, so existing checksums hold). Version 2
|
|
21
|
+
# is strict: an unknown key, kind, type, facet or format is a
|
|
22
|
+
# refusal, so any new feature requires a new version (or an
|
|
23
|
+
# explicit new capability) rather than being dropped by old readers.
|
|
24
|
+
class DocumentValidator
|
|
25
|
+
BUILTIN_TYPES = %w[
|
|
26
|
+
string token uri email hostname ip boolean integer decimal float
|
|
27
|
+
rational date time date_time time_without_date duration qname
|
|
28
|
+
base64_binary hex_binary symbol hash
|
|
29
|
+
email_address ip_v4_address ip_v6_address uuid reference uri_reference
|
|
30
|
+
date_with_timezone date_time_with_timezone day_time_duration
|
|
31
|
+
year_month_duration positive_integer non_negative_integer
|
|
32
|
+
g_year g_year_month g_month g_month_day g_day idrefs nmtokens
|
|
33
|
+
].freeze
|
|
34
|
+
# Builtins whose default is a JSON integer; every other builtin but
|
|
35
|
+
# boolean, hash and float is a string (the registry's emitted names
|
|
36
|
+
# are all in BUILTIN_TYPES; email, ip and rational are contract
|
|
37
|
+
# names the exporter never writes).
|
|
38
|
+
INTEGER_TYPES = %w[integer positive_integer non_negative_integer].freeze
|
|
39
|
+
# IEEE-754 binary64 wire form: shortest round-trip decimal, one
|
|
40
|
+
# canonical spelling (see float_wire).
|
|
41
|
+
FLOAT_WIRE = /\A-?\d+\.\d+(?:e-?\d+)?\z/
|
|
42
|
+
UNSUPPORTED_BUILTINS = %w[double].freeze
|
|
43
|
+
FORMATS = %w[xml json yaml toml].freeze
|
|
44
|
+
NAMESPACE_MODES = %w[inherit unqualified uri].freeze
|
|
45
|
+
MANIFEST_TARGETS = %w[ruby ts rust wasm].freeze
|
|
46
|
+
XML_RULE_KINDS = %w[attribute element content any_attribute sequence choice].freeze
|
|
47
|
+
ANY_ATTRIBUTE_KEYS = %w[kind to].freeze
|
|
48
|
+
REGISTER_PATTERN = /\A[A-Za-z0-9_.:-]+\z/
|
|
49
|
+
SHA256_PATTERN = /\Asha256:[0-9a-f]{64}\z/
|
|
50
|
+
ORDERED_CONTENT_MEMBER = "__lml_content"
|
|
51
|
+
FLAG_KEYS = %w[required initialize_empty].freeze
|
|
52
|
+
BOOLEANS = [true, false].freeze
|
|
53
|
+
DOT_SEGMENTS = ["", ".", ".."].freeze
|
|
54
|
+
MAX_SAFE_INTEGER = (2**53) - 1
|
|
55
|
+
# Containers nest at most this deep (the json gem's own default
|
|
56
|
+
# limit), so a hand-built Hash cannot overflow the Ruby stack.
|
|
57
|
+
MAX_DEPTH = 100
|
|
58
|
+
# Attribute names become instance variables and accessors:
|
|
59
|
+
# identifiers only (no ?, ! or = suffix, no leading digit/capital).
|
|
60
|
+
ATTRIBUTE_NAME = /\A[a-z_][a-zA-Z0-9_]*\z/
|
|
61
|
+
# A model name is a Ruby constant path (the rule Materialization
|
|
62
|
+
# applies to v2 names); v1 names reach Object.const_get on verify.
|
|
63
|
+
MODEL_NAME = /\A[A-Z]\w*(::[A-Z]\w*)*\z/
|
|
64
|
+
# Attribute names a generated class cannot carry. MEASURED, not
|
|
65
|
+
# guessed: every identifier-shaped public/protected instance method
|
|
66
|
+
# of Object and Lutaml::Model::Serializable, plus the construction
|
|
67
|
+
# hooks (93 candidates), was declared as `attribute :<name>,
|
|
68
|
+
# :string` on a throwaway Serializable and kept here if it broke
|
|
69
|
+
# new, new(<name>: "v"), or a json/xml/yaml round trip in either of
|
|
70
|
+
# two runs: a bare script with default adapters (20 broke) and the
|
|
71
|
+
# spec_helper adapter setup (24 broke); the union is pinned.
|
|
72
|
+
# Overridable names such as display, hash or schema_location work
|
|
73
|
+
# and stay allowed. The spec "re-derives the reserved attribute
|
|
74
|
+
# names" repeats the spec_helper measurement, so a lutaml-model
|
|
75
|
+
# change shows up.
|
|
76
|
+
RESERVED_ATTRIBUTE_NAMES = %w[
|
|
77
|
+
__id__ __lutaml_default_attribute_name__ __lutaml_default_owner__
|
|
78
|
+
__send__ attribute_order class doctype element_order encoding
|
|
79
|
+
extract_register_id import_declaration_plan initialize
|
|
80
|
+
initialize_clone initialize_copy initialize_dup lutaml_register
|
|
81
|
+
pending_plan_root_element prepare_instance_format_options
|
|
82
|
+
public_send record_mutation to_format to_json to_xml to_yaml
|
|
83
|
+
value_set_for xml_declaration xml_ns_prefixes
|
|
84
|
+
].freeze
|
|
85
|
+
MAPPING_OBJECT_KEYS = %w[root_mappings child_mappings].freeze
|
|
86
|
+
XML_PREFIX = /\A[A-Za-z_][A-Za-z0-9_.-]*\z/
|
|
87
|
+
|
|
88
|
+
MODEL_KEYS = %w[lml model register attributes mappings ordered_content choices].freeze
|
|
89
|
+
ATTRIBUTE_KEYS = %w[name type required collection default
|
|
90
|
+
initialize_empty values discriminator].freeze
|
|
91
|
+
RULE_KEYS = %w[kind name key to namespace prefix cdata render
|
|
92
|
+
discriminator members min max child_mappings root_mappings polymorphic_map].freeze
|
|
93
|
+
FORMAT_KEYS = %w[root namespace rules root_mappings child_mappings
|
|
94
|
+
ordered mixed_content sequence].freeze
|
|
95
|
+
COMPOSITOR_KINDS = %w[sequence choice].freeze
|
|
96
|
+
EXECUTABLE_RULE_KEYS = %w[with custom_methods delegate transform
|
|
97
|
+
proc lambda method callback].freeze
|
|
98
|
+
BUNDLE_KEYS = %w[lml_bundle roots models value_types capabilities provenance].freeze
|
|
99
|
+
MANIFEST_KEYS = %w[manifest lml_checksum schema_version generator_version
|
|
100
|
+
target capabilities corpora files dependencies].freeze
|
|
101
|
+
|
|
102
|
+
KINDS = %i[document bundle manifest].freeze
|
|
103
|
+
|
|
104
|
+
def self.validate!(hash, kind:)
|
|
105
|
+
raise ArgumentError, "unknown kind #{RejectedError.show(kind)}" unless KINDS.include?(kind)
|
|
106
|
+
|
|
107
|
+
new.validate!(hash, kind)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def validate!(hash, kind)
|
|
111
|
+
tree = neutral(hash, "", legacy: legacy_document?(hash, kind), depth: 0)
|
|
112
|
+
case kind
|
|
113
|
+
when :document then document(tree)
|
|
114
|
+
when :bundle then bundle(tree)
|
|
115
|
+
else manifest(tree)
|
|
116
|
+
end
|
|
117
|
+
tree
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# The one JSON shape a present default (or enum value) takes for a
|
|
121
|
+
# builtin type, shared by the validator and the exporter's projector
|
|
122
|
+
# so export and import cannot drift: integer is a JSON integer,
|
|
123
|
+
# (also positive_integer, non_negative_integer), boolean a JSON
|
|
124
|
+
# boolean, hash an object, and every other builtin
|
|
125
|
+
# (token, decimal, rational, date, time, qname, ...) its string
|
|
126
|
+
# spelling. Float keeps its own canonical-string rule
|
|
127
|
+
# (float_wire / canonical_float?) and any non-builtin kind (a model
|
|
128
|
+
# or value type) is not shape-checked here. Null is always allowed.
|
|
129
|
+
def self.default_shape?(kind, item)
|
|
130
|
+
return true if item.nil? || !BUILTIN_TYPES.include?(kind)
|
|
131
|
+
|
|
132
|
+
case kind
|
|
133
|
+
when *INTEGER_TYPES then item.is_a?(::Integer)
|
|
134
|
+
when "boolean" then BOOLEANS.include?(item)
|
|
135
|
+
when "hash" then item.is_a?(::Hash)
|
|
136
|
+
when "float" then true
|
|
137
|
+
else item.is_a?(::String)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Canonical `float` wire string: Ruby's shortest round-trip digits
|
|
142
|
+
# with the exponent normalised (no "+", no leading zeros), e.g.
|
|
143
|
+
# 0.71 -> "0.71", 1e-5 -> "1.0e-5", 1e20 -> "1.0e20", -0.0 -> "-0.0".
|
|
144
|
+
# Non-finite values have no wire form.
|
|
145
|
+
def self.float_wire(value)
|
|
146
|
+
float = Float(value)
|
|
147
|
+
raise ArgumentError, "non-finite float" unless float.finite?
|
|
148
|
+
|
|
149
|
+
float.to_s.sub(/e(-?)\+?0*(\d+)\z/) { "e#{Regexp.last_match(1)}#{Regexp.last_match(2)}" }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# The importer's string rule, shared with the exporter: valid
|
|
153
|
+
# UTF-8 (or plain ASCII in an ASCII-compatible encoding).
|
|
154
|
+
def self.portable_string?(string)
|
|
155
|
+
string.valid_encoding? && (string.encoding == Encoding::UTF_8 || string.ascii_only?)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def self.canonical_float?(value)
|
|
159
|
+
return false unless value.is_a?(::String) && FLOAT_WIRE.match?(value)
|
|
160
|
+
|
|
161
|
+
float_wire(Float(value)) == value
|
|
162
|
+
rescue ArgumentError
|
|
163
|
+
false
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
private
|
|
167
|
+
|
|
168
|
+
def fail!(reason, detail, location, **context)
|
|
169
|
+
raise RejectedError.new(reason, nil, detail, location: location, **context)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def pointer(*parts)
|
|
173
|
+
parts.map { |part| "/#{part.to_s.gsub('~', '~0').gsub('/', '~1')}" }.join
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def legacy_document?(hash, kind)
|
|
177
|
+
kind == :document && hash.is_a?(::Hash) && hash["lml"] == 1
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Deep-copy into a frozen neutral tree. v2 admits only JSON
|
|
181
|
+
# scalars with bounded integers; legacy v1 keeps whatever the
|
|
182
|
+
# exporter produced (floats in defaults) so identity is unchanged.
|
|
183
|
+
def neutral(node, at, legacy:, depth:)
|
|
184
|
+
if (node.is_a?(::Hash) || node.is_a?(::Array)) && depth >= MAX_DEPTH
|
|
185
|
+
fail!(:invalid_document, "nesting deeper than #{MAX_DEPTH} levels", at)
|
|
186
|
+
end
|
|
187
|
+
case node
|
|
188
|
+
when ::Hash
|
|
189
|
+
node.each_with_object({}) do |(key, value), out|
|
|
190
|
+
fail!(:invalid_document, "non-string key #{RejectedError.show(key)}", at) unless key.is_a?(::String)
|
|
191
|
+
out[key.dup.freeze] = neutral(value, "#{at}#{pointer(key)}", legacy: legacy, depth: depth + 1)
|
|
192
|
+
end.freeze
|
|
193
|
+
when ::Array
|
|
194
|
+
node.each_with_index.map { |item, i| neutral(item, "#{at}/#{i}", legacy: legacy, depth: depth + 1) }.freeze
|
|
195
|
+
when ::String
|
|
196
|
+
unless self.class.portable_string?(node)
|
|
197
|
+
fail!(:invalid_document, "string is not valid UTF-8", at)
|
|
198
|
+
end
|
|
199
|
+
node.dup.freeze
|
|
200
|
+
when ::Integer
|
|
201
|
+
if !legacy && node.abs > MAX_SAFE_INTEGER
|
|
202
|
+
fail!(:invalid_document, "structural integer out of bounds", at)
|
|
203
|
+
end
|
|
204
|
+
node
|
|
205
|
+
when true, false, nil then node
|
|
206
|
+
when ::Float
|
|
207
|
+
if legacy
|
|
208
|
+
return node if node.finite?
|
|
209
|
+
|
|
210
|
+
fail!(:invalid_document, "non-finite float has no wire form", at)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
fail!(:invalid_document, "floats are not portable; use an exact string", at)
|
|
214
|
+
else
|
|
215
|
+
fail!(:invalid_document, "unsupported value #{node.class}", at)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def expect_hash(node, at)
|
|
220
|
+
fail!(:invalid_document, "expected an object", at) unless node.is_a?(::Hash)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def expect_array(node, at)
|
|
224
|
+
fail!(:invalid_document, "expected an array", at) unless node.is_a?(::Array)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def expect_string(node, at)
|
|
228
|
+
return if node.is_a?(::String) && !node.empty?
|
|
229
|
+
|
|
230
|
+
fail!(:invalid_document, "expected a non-empty string", at)
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def expect_bool(node, at)
|
|
234
|
+
return if [true, false].include?(node)
|
|
235
|
+
|
|
236
|
+
fail!(:invalid_document, "expected a boolean", at)
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def require_keys(node, keys, at)
|
|
240
|
+
keys.each do |key|
|
|
241
|
+
fail!(:invalid_document, "missing #{key}", at + pointer(key)) unless node.key?(key)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def only_keys(node, allowed, at, reason = :invalid_document)
|
|
246
|
+
node.each_key do |key|
|
|
247
|
+
next if allowed.include?(key)
|
|
248
|
+
|
|
249
|
+
fail!(reason, "unknown key #{key}", at + pointer(key))
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def check_version(node, field, supported, at)
|
|
254
|
+
version = node[field]
|
|
255
|
+
fail!(:invalid_document, "missing #{field}", at + pointer(field)) unless node.key?(field)
|
|
256
|
+
return if version.is_a?(::Integer) && supported.include?(version)
|
|
257
|
+
|
|
258
|
+
fail!(:unsupported_version, "#{field} #{RejectedError.show(version)} is not supported " \
|
|
259
|
+
"(supported: #{supported.join(', ')})", at + pointer(field))
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# -- documents -------------------------------------------------
|
|
263
|
+
|
|
264
|
+
def document(node, at = "")
|
|
265
|
+
expect_hash(node, at)
|
|
266
|
+
check_version(node, "lml", [1, 2], at)
|
|
267
|
+
node["lml"] == 1 ? document_v1(node, at) : model_v2(node, at)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def document_v1(node, at)
|
|
271
|
+
expect_string(node["model"], at + pointer("model"))
|
|
272
|
+
unless node["model"].match?(MODEL_NAME)
|
|
273
|
+
fail!(:invalid_document, "model name #{RejectedError.show(node['model'])} is not a Ruby constant path",
|
|
274
|
+
at + pointer("model"))
|
|
275
|
+
end
|
|
276
|
+
unless node["register"].nil? || node["register"].is_a?(::String)
|
|
277
|
+
fail!(:invalid_document, "register must be a string", at + pointer("register"))
|
|
278
|
+
end
|
|
279
|
+
if node.key?("attributes")
|
|
280
|
+
expect_array(node["attributes"], at + pointer("attributes"))
|
|
281
|
+
node["attributes"].each_with_index do |attr, i|
|
|
282
|
+
expect_hash(attr, "#{at}/attributes/#{i}")
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
expect_hash(node["mappings"], at + pointer("mappings")) if node.key?("mappings")
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def model_v2(node, at, expected_id: nil)
|
|
289
|
+
reject_executable_flags(node, at)
|
|
290
|
+
only_keys(node, MODEL_KEYS, at)
|
|
291
|
+
require_keys(node, %w[lml model register attributes], at)
|
|
292
|
+
expect_string(node["model"], at + pointer("model"))
|
|
293
|
+
register = node["register"]
|
|
294
|
+
unless register.is_a?(::String) && register.match?(REGISTER_PATTERN)
|
|
295
|
+
fail!(:invalid_document, "register must match #{REGISTER_PATTERN.source}", at + pointer("register"))
|
|
296
|
+
end
|
|
297
|
+
id = "#{node['model']}@#{register}"
|
|
298
|
+
if expected_id && expected_id != id
|
|
299
|
+
fail!(:invalid_document, "key #{expected_id} does not match model ID #{id}", at, model_id: id)
|
|
300
|
+
end
|
|
301
|
+
attributes_v2(node["attributes"], at, id)
|
|
302
|
+
mappings_v2(node["mappings"], at, id, node["attributes"]) if node.key?("mappings")
|
|
303
|
+
ordered_content(node, at, id)
|
|
304
|
+
model_choices(node["choices"], node["attributes"], at + pointer("choices"), id) if node.key?("choices")
|
|
305
|
+
id
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def model_choices(spec, attributes, at, id)
|
|
309
|
+
expect_hash(spec, at)
|
|
310
|
+
only_keys(spec, %w[version validation groups], at)
|
|
311
|
+
require_keys(spec, %w[version validation groups], at)
|
|
312
|
+
check_version(spec, "version", [1], at)
|
|
313
|
+
unless spec["validation"] == "explicit"
|
|
314
|
+
fail!(:invalid_document, "model choices require explicit validation", at + pointer("validation"), model_id: id)
|
|
315
|
+
end
|
|
316
|
+
expect_array(spec["groups"], at + pointer("groups"))
|
|
317
|
+
names = attributes.map { |attribute| attribute["name"] }
|
|
318
|
+
spec["groups"].each_with_index do |group, index|
|
|
319
|
+
model_choice(group, names, "#{at}/groups/#{index}", id)
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def model_choice(group, names, at, id, depth = 0)
|
|
324
|
+
if depth >= MAX_DEPTH
|
|
325
|
+
fail!(:invalid_document, "model choice nesting exceeds #{MAX_DEPTH}", at, model_id: id)
|
|
326
|
+
end
|
|
327
|
+
expect_hash(group, at)
|
|
328
|
+
only_keys(group, %w[min max format members], at)
|
|
329
|
+
require_keys(group, %w[min max format members], at)
|
|
330
|
+
unless group["min"].is_a?(::Integer) && group["min"] >= 0 &&
|
|
331
|
+
(group["max"].nil? || (group["max"].is_a?(::Integer) && group["max"] >= 0))
|
|
332
|
+
fail!(:invalid_document, "model choice bounds must be nonnegative integers or null maximum", at, model_id: id)
|
|
333
|
+
end
|
|
334
|
+
unless [nil, "xml"].include?(group["format"])
|
|
335
|
+
fail!(:unsupported_mapping, "model choice format must be XML or null", at + pointer("format"), model_id: id)
|
|
336
|
+
end
|
|
337
|
+
expect_array(group["members"], at + pointer("members"))
|
|
338
|
+
group["members"].each_with_index do |member, index|
|
|
339
|
+
loc = "#{at}/members/#{index}"
|
|
340
|
+
expect_hash(member, loc)
|
|
341
|
+
if member.keys == ["attribute"]
|
|
342
|
+
expect_string(member["attribute"], loc + pointer("attribute"))
|
|
343
|
+
unless names.include?(member["attribute"])
|
|
344
|
+
fail!(:dangling_reference, "unknown choice attribute #{member['attribute']}", loc, model_id: id)
|
|
345
|
+
end
|
|
346
|
+
elsif member.keys == ["choice"]
|
|
347
|
+
model_choice(member["choice"], names, loc + pointer("choice"), id, depth + 1)
|
|
348
|
+
else
|
|
349
|
+
fail!(:invalid_document, "choice member must contain one attribute or nested choice", loc, model_id: id)
|
|
350
|
+
end
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def reject_executable_flags(node, at)
|
|
355
|
+
{ "custom_initialize" => :custom_initialize,
|
|
356
|
+
"imperative_element_order" => :imperative_element_order }.each do |key, reason|
|
|
357
|
+
next unless node.key?(key)
|
|
358
|
+
|
|
359
|
+
fail!(reason, "#{key} models have no declarative equivalent", at + pointer(key),
|
|
360
|
+
model_id: node["model"] && "#{node['model']}@#{node['register']}")
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def attributes_v2(attributes, at, id)
|
|
365
|
+
expect_array(attributes, at + pointer("attributes"))
|
|
366
|
+
seen = {}
|
|
367
|
+
attributes.each_with_index do |attr, i|
|
|
368
|
+
loc = "#{at}/attributes/#{i}"
|
|
369
|
+
expect_hash(attr, loc)
|
|
370
|
+
reject_lambda_default(attr, loc, id)
|
|
371
|
+
only_keys(attr, ATTRIBUTE_KEYS, loc)
|
|
372
|
+
require_keys(attr, %w[name type], loc)
|
|
373
|
+
expect_string(attr["name"], loc + pointer("name"))
|
|
374
|
+
name = attr["name"]
|
|
375
|
+
attribute_name(name, loc + pointer("name"), id)
|
|
376
|
+
if name == ORDERED_CONTENT_MEMBER
|
|
377
|
+
fail!(:reserved_member, "#{name} is reserved for ordered content", loc + pointer("name"),
|
|
378
|
+
model_id: id, attribute: name)
|
|
379
|
+
end
|
|
380
|
+
if seen[name]
|
|
381
|
+
fail!(:duplicate_id, "attribute #{name} declared twice", loc + pointer("name"),
|
|
382
|
+
model_id: id, attribute: name)
|
|
383
|
+
end
|
|
384
|
+
seen[name] = true
|
|
385
|
+
type(attr["type"], loc + pointer("type"), id, name)
|
|
386
|
+
FLAG_KEYS.each do |flag|
|
|
387
|
+
expect_bool(attr[flag], loc + pointer(flag)) if attr.key?(flag)
|
|
388
|
+
end
|
|
389
|
+
collection(attr["collection"], loc + pointer("collection")) if attr.key?("collection")
|
|
390
|
+
if attr.key?("default")
|
|
391
|
+
default(attr["default"], loc + pointer("default"), id, name)
|
|
392
|
+
typed_default(attr, loc, id, name)
|
|
393
|
+
end
|
|
394
|
+
float_default(attr, loc, id, name)
|
|
395
|
+
expect_array(attr["values"], loc + pointer("values")) if attr.key?("values")
|
|
396
|
+
discriminator_shape(attr["discriminator"], loc + pointer("discriminator"), id) if attr.key?("discriminator")
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
def attribute_name(name, at, id)
|
|
401
|
+
unless name.match?(ATTRIBUTE_NAME)
|
|
402
|
+
fail!(:invalid_document, "attribute name #{RejectedError.show(name)} must match #{ATTRIBUTE_NAME.source}", at,
|
|
403
|
+
model_id: id, attribute: name)
|
|
404
|
+
end
|
|
405
|
+
return unless RESERVED_ATTRIBUTE_NAMES.include?(name)
|
|
406
|
+
|
|
407
|
+
fail!(:reserved_member, "attribute name #{name} breaks Serializable (reserved attribute name)", at,
|
|
408
|
+
model_id: id, attribute: name)
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def reject_lambda_default(attr, loc, id)
|
|
412
|
+
default = attr["default"]
|
|
413
|
+
return unless default.is_a?(::Hash) && (default.key?("lambda") || default.key?("proc"))
|
|
414
|
+
|
|
415
|
+
fail!(:lambda_default, "executable defaults are not portable", loc + pointer("default"),
|
|
416
|
+
model_id: id, attribute: attr["name"])
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def type(node, at, id, attribute)
|
|
420
|
+
expect_hash(node, at)
|
|
421
|
+
case node["kind"]
|
|
422
|
+
when "builtin"
|
|
423
|
+
only_keys(node, %w[kind name], at)
|
|
424
|
+
name = node["name"]
|
|
425
|
+
return if BUILTIN_TYPES.include?(name)
|
|
426
|
+
|
|
427
|
+
reason = UNSUPPORTED_BUILTINS.include?(name) ? :unsupported_type : :unknown_type
|
|
428
|
+
fail!(reason, "builtin type #{RejectedError.show(name)}", at + pointer("name"), model_id: id, attribute: attribute)
|
|
429
|
+
when "model", "value"
|
|
430
|
+
only_keys(node, %w[kind id], at)
|
|
431
|
+
expect_string(node["id"], at + pointer("id"))
|
|
432
|
+
else
|
|
433
|
+
fail!(:unknown_type, "type kind #{RejectedError.show(node['kind'])}", at + pointer("kind"),
|
|
434
|
+
model_id: id, attribute: attribute)
|
|
435
|
+
end
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
def collection(node, at)
|
|
439
|
+
return if node == false
|
|
440
|
+
|
|
441
|
+
expect_hash(node, at)
|
|
442
|
+
only_keys(node, %w[min max class], at)
|
|
443
|
+
require_keys(node, %w[min max], at)
|
|
444
|
+
min = node["min"]
|
|
445
|
+
max = node["max"]
|
|
446
|
+
fail!(:invalid_document, "min must be a non-negative integer", at + pointer("min")) unless min.is_a?(::Integer) && min >= 0
|
|
447
|
+
unless max.nil? || (max.is_a?(::Integer) && max >= min)
|
|
448
|
+
fail!(:invalid_document, "max must be null or an integer >= min", at + pointer("max"))
|
|
449
|
+
end
|
|
450
|
+
expect_string(node["class"], at + pointer("class")) if node.key?("class")
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def default(node, at, id, attribute)
|
|
454
|
+
expect_hash(node, at)
|
|
455
|
+
only_keys(node, %w[present value], at)
|
|
456
|
+
expect_bool(node["present"], at + pointer("present"))
|
|
457
|
+
if node["present"]
|
|
458
|
+
unless node.key?("value")
|
|
459
|
+
fail!(:invalid_document, "present default needs a value", at + pointer("value"),
|
|
460
|
+
model_id: id, attribute: attribute)
|
|
461
|
+
end
|
|
462
|
+
elsif node.key?("value")
|
|
463
|
+
fail!(:invalid_document, "absent default carries no value", at + pointer("value"),
|
|
464
|
+
model_id: id, attribute: attribute)
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
# A present default must be the JSON shape its builtin type
|
|
469
|
+
# declares (an integer on a string attribute is refused, as Rust does).
|
|
470
|
+
def typed_default(attr, loc, id, name)
|
|
471
|
+
return unless attr.dig("default", "present") && attr.dig("type", "kind") == "builtin"
|
|
472
|
+
|
|
473
|
+
kind = attr.dig("type", "name")
|
|
474
|
+
return if kind == "float" # float_default owns the wire string rule
|
|
475
|
+
|
|
476
|
+
value = attr["default"]["value"]
|
|
477
|
+
items = attr["collection"] && value.is_a?(::Array) ? value : [value]
|
|
478
|
+
return if items.all? { |item| self.class.default_shape?(kind, item) }
|
|
479
|
+
|
|
480
|
+
fail!(:non_serializable_default, "default #{RejectedError.show(value)} is not a #{kind}",
|
|
481
|
+
loc + pointer("default", "value"), model_id: id, attribute: name)
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
def float_default(attr, loc, id, name)
|
|
485
|
+
return unless attr.dig("type", "kind") == "builtin" && attr.dig("type", "name") == "float"
|
|
486
|
+
return unless attr.dig("default", "present")
|
|
487
|
+
|
|
488
|
+
value = attr["default"]["value"]
|
|
489
|
+
return if self.class.canonical_float?(value)
|
|
490
|
+
|
|
491
|
+
fail!(:invalid_document, "float default must be a canonical float string", loc + pointer("default", "value"),
|
|
492
|
+
model_id: id, attribute: name)
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
def discriminator_shape(node, at, id)
|
|
496
|
+
expect_hash(node, at)
|
|
497
|
+
only_keys(node, %w[attribute cases], at)
|
|
498
|
+
require_keys(node, %w[attribute cases], at)
|
|
499
|
+
expect_string(node["attribute"], at + pointer("attribute"))
|
|
500
|
+
expect_hash(node["cases"], at + pointer("cases"))
|
|
501
|
+
fail!(:unknown_discriminator, "no cases", at + pointer("cases"), model_id: id) if node["cases"].empty?
|
|
502
|
+
node["cases"].each do |wire, target|
|
|
503
|
+
expect_string(target, "#{at}/cases#{pointer(wire)}")
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
def mappings_v2(mappings, at, id, attributes = [])
|
|
508
|
+
expect_hash(mappings, at + pointer("mappings"))
|
|
509
|
+
mappings.each do |format, mapping|
|
|
510
|
+
loc = "#{at}/mappings#{pointer(format)}"
|
|
511
|
+
fail!(:unsupported_format, "format #{format}", loc, model_id: id, format: format) unless FORMATS.include?(format)
|
|
512
|
+
expect_hash(mapping, loc)
|
|
513
|
+
only_keys(mapping, FORMAT_KEYS, loc, :unsupported_mapping)
|
|
514
|
+
namespace(mapping["namespace"], "#{loc}/namespace", id, format) if mapping.key?("namespace")
|
|
515
|
+
mapping_root(mapping["root"], "#{loc}/root", id, format) if mapping.key?("root")
|
|
516
|
+
MAPPING_OBJECT_KEYS.each do |key|
|
|
517
|
+
expect_hash(mapping[key], "#{loc}/#{key}") if mapping.key?(key)
|
|
518
|
+
end
|
|
519
|
+
mapping_flags(mapping, loc, id, format)
|
|
520
|
+
next unless mapping.key?("rules")
|
|
521
|
+
|
|
522
|
+
expect_array(mapping["rules"], "#{loc}/rules")
|
|
523
|
+
mapping["rules"].each_with_index { |rule, i| rule_v2(rule, "#{loc}/rules/#{i}", id, format) }
|
|
524
|
+
any_attribute_rules(mapping["rules"], loc, id, format, attributes)
|
|
525
|
+
end
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
# any_attribute (map_all_attributes): xml only, top-level only,
|
|
529
|
+
# at most one per mapping, `to` a declared hash attribute.
|
|
530
|
+
def any_attribute_rules(rules, loc, id, format, attributes)
|
|
531
|
+
found = rules.each_index.select { |i| rules[i].is_a?(::Hash) && rules[i]["kind"] == "any_attribute" }
|
|
532
|
+
return if found.empty?
|
|
533
|
+
|
|
534
|
+
unless format == "xml"
|
|
535
|
+
fail!(:unsupported_mapping, "any_attribute is xml only", "#{loc}/rules/#{found.first}", model_id: id,
|
|
536
|
+
format: format)
|
|
537
|
+
end
|
|
538
|
+
if found.length > 1
|
|
539
|
+
fail!(:unsupported_mapping, "at most one any_attribute rule per mapping", "#{loc}/rules/#{found[1]}",
|
|
540
|
+
model_id: id, format: format)
|
|
541
|
+
end
|
|
542
|
+
at = "#{loc}/rules/#{found.first}"
|
|
543
|
+
rule = rules[found.first]
|
|
544
|
+
only_keys(rule, ANY_ATTRIBUTE_KEYS, at, :unsupported_mapping)
|
|
545
|
+
expect_string(rule["to"], at + pointer("to"))
|
|
546
|
+
target = Array(attributes).find { |a| a.is_a?(::Hash) && a["name"] == rule["to"] }
|
|
547
|
+
unless target
|
|
548
|
+
fail!(:dangling_reference, "any_attribute `to` #{RejectedError.show(rule['to'])} is not an attribute of the model",
|
|
549
|
+
at + pointer("to"), model_id: id, format: format)
|
|
550
|
+
end
|
|
551
|
+
return if target.dig("type", "kind") == "builtin" && target.dig("type", "name") == "hash" && !target["collection"]
|
|
552
|
+
|
|
553
|
+
fail!(:unsupported_mapping, "any_attribute `to` #{RejectedError.show(rule['to'])} must be a non-collection hash attribute",
|
|
554
|
+
at + pointer("to"), model_id: id, format: format)
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
def mapping_root(node, at, id, format)
|
|
558
|
+
expect_hash(node, at)
|
|
559
|
+
expect_string(node["element"], at + pointer("element")) if node.key?("element")
|
|
560
|
+
return unless node.key?("mixed") && !BOOLEANS.include?(node["mixed"])
|
|
561
|
+
|
|
562
|
+
fail!(:unsupported_mapping, "root mixed must be true or false", at + pointer("mixed"),
|
|
563
|
+
model_id: id, format: format)
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
# ordered / mixed_content are booleans; sequence is an array of
|
|
567
|
+
# sequence compositors (xml only).
|
|
568
|
+
def mapping_flags(mapping, loc, id, format)
|
|
569
|
+
%w[ordered mixed_content].each do |flag|
|
|
570
|
+
next unless mapping.key?(flag)
|
|
571
|
+
|
|
572
|
+
next if BOOLEANS.include?(mapping[flag])
|
|
573
|
+
|
|
574
|
+
fail!(:unsupported_mapping, "#{flag} must be true or false", "#{loc}/#{flag}",
|
|
575
|
+
model_id: id, format: format)
|
|
576
|
+
end
|
|
577
|
+
return unless mapping.key?("sequence")
|
|
578
|
+
|
|
579
|
+
unless format == "xml"
|
|
580
|
+
fail!(:unsupported_mapping, "sequence is xml only", "#{loc}/sequence", model_id: id, format: format)
|
|
581
|
+
end
|
|
582
|
+
expect_array(mapping["sequence"], "#{loc}/sequence")
|
|
583
|
+
mapping["sequence"].each_with_index do |group, i|
|
|
584
|
+
compositor_v2(group, "#{loc}/sequence/#{i}", id, format)
|
|
585
|
+
end
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
def compositor_v2(node, at, id, format)
|
|
589
|
+
rule_v2(node, at, id, format)
|
|
590
|
+
unless COMPOSITOR_KINDS.include?(node["kind"])
|
|
591
|
+
fail!(:unsupported_mapping, "expected a sequence or choice", at + pointer("kind"),
|
|
592
|
+
model_id: id, format: format)
|
|
593
|
+
end
|
|
594
|
+
expect_array(node["members"], "#{at}/members")
|
|
595
|
+
node["members"].each_with_index do |member, i|
|
|
596
|
+
loc = "#{at}/members/#{i}"
|
|
597
|
+
expect_hash(member, loc)
|
|
598
|
+
if member["kind"] == "any_attribute"
|
|
599
|
+
fail!(:unsupported_mapping, "any_attribute is not allowed inside a compositor", loc + pointer("kind"),
|
|
600
|
+
model_id: id, format: format)
|
|
601
|
+
end
|
|
602
|
+
COMPOSITOR_KINDS.include?(member["kind"]) ? compositor_v2(member, loc, id, format) : rule_v2(member, loc, id, format)
|
|
603
|
+
end
|
|
604
|
+
end
|
|
605
|
+
|
|
606
|
+
def rule_v2(rule, at, id, format)
|
|
607
|
+
expect_hash(rule, at)
|
|
608
|
+
rule.each_key do |key|
|
|
609
|
+
next unless EXECUTABLE_RULE_KEYS.include?(key)
|
|
610
|
+
|
|
611
|
+
fail!(:unsupported_mapping, "executable rule key #{key}", at + pointer(key),
|
|
612
|
+
model_id: id, format: format)
|
|
613
|
+
end
|
|
614
|
+
only_keys(rule, RULE_KEYS, at, :unsupported_mapping)
|
|
615
|
+
if format == "xml" && rule.key?("kind") && !XML_RULE_KINDS.include?(rule["kind"])
|
|
616
|
+
fail!(:unsupported_mapping, "rule kind #{RejectedError.show(rule['kind'])}", at + pointer("kind"),
|
|
617
|
+
model_id: id, format: format)
|
|
618
|
+
end
|
|
619
|
+
namespace(rule["namespace"], "#{at}/namespace", id, format) if rule.key?("namespace")
|
|
620
|
+
MAPPING_OBJECT_KEYS.each do |key|
|
|
621
|
+
expect_hash(rule[key], "#{at}/#{key}") if rule.key?(key)
|
|
622
|
+
end
|
|
623
|
+
expect_bool(rule["cdata"], "#{at}/cdata") if rule.key?("cdata")
|
|
624
|
+
if rule.key?("polymorphic_map")
|
|
625
|
+
expect_hash(rule["polymorphic_map"], "#{at}/polymorphic_map")
|
|
626
|
+
rule["polymorphic_map"].each { |k, v| expect_string(v, "#{at}/polymorphic_map#{pointer(k)}") }
|
|
627
|
+
end
|
|
628
|
+
expect_array(rule["name"], "#{at}/name") if rule.key?("name")
|
|
629
|
+
expect_array(rule["key"], "#{at}/key") if rule.key?("key")
|
|
630
|
+
render(rule["render"], "#{at}/render", id, format) if rule.key?("render")
|
|
631
|
+
discriminator_shape(rule["discriminator"], "#{at}/discriminator", id) if rule.key?("discriminator")
|
|
632
|
+
end
|
|
633
|
+
|
|
634
|
+
def namespace(node, at, id, format)
|
|
635
|
+
expect_hash(node, at)
|
|
636
|
+
unless NAMESPACE_MODES.include?(node["mode"])
|
|
637
|
+
fail!(:unsupported_mapping, "namespace mode #{RejectedError.show(node['mode'])}", at + pointer("mode"),
|
|
638
|
+
model_id: id, format: format)
|
|
639
|
+
end
|
|
640
|
+
only_keys(node, %w[mode uri prefix], at, :unsupported_mapping)
|
|
641
|
+
prefix(node["prefix"], at + pointer("prefix")) if node.key?("prefix")
|
|
642
|
+
if node["mode"] == "uri"
|
|
643
|
+
expect_string(node["uri"], at + pointer("uri"))
|
|
644
|
+
elsif node.key?("uri") || node.key?("prefix")
|
|
645
|
+
fail!(:unsupported_mapping, "uri/prefix only with mode uri", at, model_id: id, format: format)
|
|
646
|
+
end
|
|
647
|
+
end
|
|
648
|
+
|
|
649
|
+
def prefix(node, at)
|
|
650
|
+
return if node.is_a?(::String) && node.match?(XML_PREFIX)
|
|
651
|
+
|
|
652
|
+
fail!(:invalid_document, "prefix must match #{XML_PREFIX.source}", at)
|
|
653
|
+
end
|
|
654
|
+
|
|
655
|
+
def render(node, at, id, format)
|
|
656
|
+
expect_hash(node, at)
|
|
657
|
+
only_keys(node, %w[nil empty default], at, :unsupported_mapping)
|
|
658
|
+
node.each do |key, value|
|
|
659
|
+
next if value.is_a?(::String) || BOOLEANS.include?(value)
|
|
660
|
+
|
|
661
|
+
fail!(:unsupported_mapping, "render policy #{key}", at + pointer(key), model_id: id, format: format)
|
|
662
|
+
end
|
|
663
|
+
end
|
|
664
|
+
|
|
665
|
+
def ordered_content(node, at, id)
|
|
666
|
+
return unless node.key?("ordered_content")
|
|
667
|
+
|
|
668
|
+
loc = at + pointer("ordered_content")
|
|
669
|
+
content = node["ordered_content"]
|
|
670
|
+
valid = content.is_a?(::Hash) && content.keys.sort == %w[member version] &&
|
|
671
|
+
content["member"] == ORDERED_CONTENT_MEMBER && content["version"] == 1
|
|
672
|
+
unless valid
|
|
673
|
+
fail!(:invalid_ordered_content,
|
|
674
|
+
"expected {member: #{ORDERED_CONTENT_MEMBER}, version: 1}", loc, model_id: id)
|
|
675
|
+
end
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
# -- bundles ---------------------------------------------------
|
|
679
|
+
|
|
680
|
+
def bundle(node)
|
|
681
|
+
expect_hash(node, "")
|
|
682
|
+
check_version(node, "lml_bundle", [1], "")
|
|
683
|
+
only_keys(node, BUNDLE_KEYS, "")
|
|
684
|
+
require_keys(node, %w[roots models value_types capabilities], "")
|
|
685
|
+
roots, models, values, caps = node.values_at("roots", "models", "value_types", "capabilities")
|
|
686
|
+
expect_hash(models, "/models")
|
|
687
|
+
expect_hash(values, "/value_types")
|
|
688
|
+
sorted_unique_strings(roots, "/roots")
|
|
689
|
+
sorted_unique_strings(caps, "/capabilities")
|
|
690
|
+
expect_hash(node["provenance"], "/provenance") if node.key?("provenance")
|
|
691
|
+
|
|
692
|
+
values.each do |vid, definition|
|
|
693
|
+
value_type(vid, definition, "/value_types#{pointer(vid)}")
|
|
694
|
+
end
|
|
695
|
+
models.each do |mid, doc|
|
|
696
|
+
loc = "/models#{pointer(mid)}"
|
|
697
|
+
expect_hash(doc, loc)
|
|
698
|
+
check_version(doc, "lml", [2], loc)
|
|
699
|
+
model_v2(doc, loc, expected_id: mid)
|
|
700
|
+
if doc.key?("choices") && !caps.include?("model.choices.v1")
|
|
701
|
+
fail!(:capability_missing, "model choices require model.choices.v1", "#{loc}/choices", model_id: mid)
|
|
702
|
+
end
|
|
703
|
+
doc.fetch("mappings", {}).each_key do |format|
|
|
704
|
+
next if caps.include?(format)
|
|
705
|
+
|
|
706
|
+
fail!(:capability_missing, "format #{format} is not in capabilities",
|
|
707
|
+
"#{loc}/mappings#{pointer(format)}", model_id: mid, format: format)
|
|
708
|
+
end
|
|
709
|
+
end
|
|
710
|
+
roots.each_with_index do |root, i|
|
|
711
|
+
next if models.key?(root)
|
|
712
|
+
|
|
713
|
+
fail!(:dangling_reference, "root #{root} is not a bundled model", "/roots/#{i}", model_id: root)
|
|
714
|
+
end
|
|
715
|
+
check_graph(models, values)
|
|
716
|
+
end
|
|
717
|
+
|
|
718
|
+
def sorted_unique_strings(node, at)
|
|
719
|
+
expect_array(node, at)
|
|
720
|
+
node.each_with_index { |item, i| expect_string(item, "#{at}/#{i}") }
|
|
721
|
+
if node.uniq.length != node.length
|
|
722
|
+
dup = node.find { |item| node.count(item) > 1 }
|
|
723
|
+
fail!(:duplicate_id, "#{dup} listed twice", at)
|
|
724
|
+
end
|
|
725
|
+
fail!(:invalid_document, "must be sorted", at) unless node == node.sort
|
|
726
|
+
end
|
|
727
|
+
|
|
728
|
+
def value_type(vid, definition, at)
|
|
729
|
+
expect_hash(definition, at)
|
|
730
|
+
only_keys(definition, %w[base facets], at)
|
|
731
|
+
require_keys(definition, %w[base facets], at)
|
|
732
|
+
expect_hash(definition["facets"], "#{at}/facets")
|
|
733
|
+
base = definition["base"]
|
|
734
|
+
return if BUILTIN_TYPES.include?(base)
|
|
735
|
+
|
|
736
|
+
reason = UNSUPPORTED_BUILTINS.include?(base) ? :unsupported_type : :unknown_type
|
|
737
|
+
fail!(reason, "value type #{vid} base #{RejectedError.show(base)}", "#{at}/base")
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
def check_graph(models, values)
|
|
741
|
+
models.each do |mid, doc|
|
|
742
|
+
doc["attributes"].each_with_index do |attr, i|
|
|
743
|
+
loc = "/models#{pointer(mid)}/attributes/#{i}"
|
|
744
|
+
ref = attr["type"]
|
|
745
|
+
check_type_ref(ref, models, values, "#{loc}/type", mid)
|
|
746
|
+
check_discriminator(attr["discriminator"], models, "#{loc}/discriminator", mid) if attr["discriminator"]
|
|
747
|
+
end
|
|
748
|
+
doc.fetch("mappings", {}).each do |format, mapping|
|
|
749
|
+
(mapping["rules"] || []).each_with_index do |rule, i|
|
|
750
|
+
next unless rule["discriminator"]
|
|
751
|
+
|
|
752
|
+
check_discriminator(rule["discriminator"], models,
|
|
753
|
+
"/models#{pointer(mid)}/mappings#{pointer(format)}/rules/#{i}/discriminator", mid)
|
|
754
|
+
end
|
|
755
|
+
end
|
|
756
|
+
end
|
|
757
|
+
end
|
|
758
|
+
|
|
759
|
+
def check_type_ref(ref, models, values, at, mid)
|
|
760
|
+
table = { "model" => models, "value" => values }[ref["kind"]]
|
|
761
|
+
return if table.nil? || table.key?(ref["id"])
|
|
762
|
+
|
|
763
|
+
fail!(:dangling_reference, "#{ref['kind']} #{ref['id']} is not in the bundle", "#{at}/id", model_id: mid)
|
|
764
|
+
end
|
|
765
|
+
|
|
766
|
+
def check_discriminator(disc, models, at, mid)
|
|
767
|
+
disc["cases"].each do |wire, target|
|
|
768
|
+
unless models.key?(target)
|
|
769
|
+
fail!(:dangling_reference, "case #{wire} targets #{target}", "#{at}/cases#{pointer(wire)}", model_id: mid)
|
|
770
|
+
end
|
|
771
|
+
next if models[target]["attributes"].any? { |a| a["name"] == disc["attribute"] }
|
|
772
|
+
|
|
773
|
+
fail!(:unknown_discriminator, "#{target} has no attribute #{disc['attribute']}",
|
|
774
|
+
"#{at}/attribute", model_id: mid)
|
|
775
|
+
end
|
|
776
|
+
end
|
|
777
|
+
|
|
778
|
+
# -- manifests -------------------------------------------------
|
|
779
|
+
|
|
780
|
+
def manifest(node)
|
|
781
|
+
expect_hash(node, "")
|
|
782
|
+
check_version(node, "manifest", [1], "")
|
|
783
|
+
only_keys(node, MANIFEST_KEYS, "")
|
|
784
|
+
require_keys(node, MANIFEST_KEYS, "")
|
|
785
|
+
digest(node["lml_checksum"], "/lml_checksum")
|
|
786
|
+
check_version(node, "schema_version", [2], "")
|
|
787
|
+
expect_string(node["generator_version"], "/generator_version")
|
|
788
|
+
unless MANIFEST_TARGETS.include?(node["target"])
|
|
789
|
+
fail!(:invalid_document, "target #{RejectedError.show(node['target'])}", "/target")
|
|
790
|
+
end
|
|
791
|
+
sorted_unique_strings(node["capabilities"], "/capabilities")
|
|
792
|
+
expect_array(node["corpora"], "/corpora")
|
|
793
|
+
node["corpora"].each_with_index { |c, i| corpus(c, "/corpora/#{i}") }
|
|
794
|
+
pinned_list(node["files"], "/files", "path", relative: true)
|
|
795
|
+
pinned_list(node["dependencies"], "/dependencies", "name", extra: "revision")
|
|
796
|
+
end
|
|
797
|
+
|
|
798
|
+
def digest(node, at)
|
|
799
|
+
return if node.is_a?(::String) && node.match?(SHA256_PATTERN)
|
|
800
|
+
|
|
801
|
+
fail!(:invalid_document, "expected sha256:<64 lowercase hex>", at)
|
|
802
|
+
end
|
|
803
|
+
|
|
804
|
+
def corpus(node, at)
|
|
805
|
+
expect_hash(node, at)
|
|
806
|
+
only_keys(node, %w[generation sha256], at)
|
|
807
|
+
require_keys(node, %w[generation sha256], at)
|
|
808
|
+
digest(node["sha256"], "#{at}/sha256")
|
|
809
|
+
end
|
|
810
|
+
|
|
811
|
+
def pinned_list(list, at, id_key, relative: false, extra: nil)
|
|
812
|
+
expect_array(list, at)
|
|
813
|
+
keys = [id_key, extra, "sha256"].compact
|
|
814
|
+
ids = list.each_with_index.map do |entry, i|
|
|
815
|
+
loc = "#{at}/#{i}"
|
|
816
|
+
expect_hash(entry, loc)
|
|
817
|
+
only_keys(entry, keys, loc)
|
|
818
|
+
require_keys(entry, keys, loc)
|
|
819
|
+
expect_string(entry[id_key], "#{loc}/#{id_key}")
|
|
820
|
+
expect_string(entry[extra], "#{loc}/#{extra}") if extra
|
|
821
|
+
digest(entry["sha256"], "#{loc}/sha256")
|
|
822
|
+
safe_path(entry[id_key], "#{loc}/#{id_key}") if relative
|
|
823
|
+
entry[id_key]
|
|
824
|
+
end
|
|
825
|
+
fail!(:duplicate_id, "duplicate #{id_key}", at) if ids.uniq.length != ids.length
|
|
826
|
+
fail!(:invalid_document, "must be sorted by #{id_key}", at) unless ids == ids.sort
|
|
827
|
+
end
|
|
828
|
+
|
|
829
|
+
def safe_path(path, at)
|
|
830
|
+
segments = path.split("/", -1)
|
|
831
|
+
unsafe = path.start_with?("/") || path.include?("\\") || path.include?("\0") ||
|
|
832
|
+
segments.any? { |s| DOT_SEGMENTS.include?(s) }
|
|
833
|
+
return unless unsafe
|
|
834
|
+
|
|
835
|
+
fail!(:invalid_document, "path must be relative without traversal", at)
|
|
836
|
+
end
|
|
837
|
+
end
|
|
838
|
+
end
|
|
839
|
+
end
|
|
840
|
+
end
|