lutaml-model 0.7.3 → 0.7.5
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/.envrc +1 -0
- data/.github/workflows/release.yml +3 -0
- data/.gitignore +6 -1
- data/.irbrc +1 -0
- data/.pryrc +1 -0
- data/.rubocop_todo.yml +25 -52
- data/README.adoc +2177 -192
- data/docs/custom_registers.adoc +228 -0
- data/docs/schema_generation.adoc +898 -0
- data/docs/schema_import.adoc +364 -0
- data/flake.lock +114 -0
- data/flake.nix +103 -0
- data/lib/lutaml/model/attribute.rb +230 -94
- data/lib/lutaml/model/choice.rb +30 -0
- data/lib/lutaml/model/collection.rb +194 -0
- data/lib/lutaml/model/comparable_model.rb +3 -3
- data/lib/lutaml/model/config.rb +26 -3
- data/lib/lutaml/model/constants.rb +2 -0
- data/lib/lutaml/model/error/element_count_out_of_range_error.rb +29 -0
- data/lib/lutaml/model/error/invalid_attribute_name_error.rb +15 -0
- data/lib/lutaml/model/error/invalid_attribute_options_error.rb +16 -0
- data/lib/lutaml/model/error/invalid_choice_range_error.rb +3 -5
- data/lib/lutaml/model/error/register/not_registrable_class_error.rb +11 -0
- data/lib/lutaml/model/error/type/invalid_value_error.rb +5 -3
- data/lib/lutaml/model/error/type/max_bound_error.rb +20 -0
- data/lib/lutaml/model/error/type/max_length_error.rb +20 -0
- data/lib/lutaml/model/error/type/min_bound_error.rb +20 -0
- data/lib/lutaml/model/error/type/min_length_error.rb +20 -0
- data/lib/lutaml/model/error/type/pattern_not_matched_error.rb +18 -0
- data/lib/lutaml/model/error/validation_failed_error.rb +9 -0
- data/lib/lutaml/model/error.rb +10 -0
- data/lib/lutaml/model/errors.rb +36 -0
- data/lib/lutaml/model/format_registry.rb +5 -2
- data/lib/lutaml/model/global_register.rb +41 -0
- data/lib/lutaml/model/{hash.rb → hash_adapter.rb} +5 -5
- data/lib/lutaml/model/jsonl/document.rb +14 -0
- data/lib/lutaml/model/jsonl/mapping.rb +19 -0
- data/lib/lutaml/model/jsonl/mapping_rule.rb +9 -0
- data/lib/lutaml/model/jsonl/standard_adapter.rb +33 -0
- data/lib/lutaml/model/jsonl/transform.rb +19 -0
- data/lib/lutaml/model/jsonl.rb +21 -0
- data/lib/lutaml/model/key_value_document.rb +3 -2
- data/lib/lutaml/model/mapping/key_value_mapping.rb +64 -4
- data/lib/lutaml/model/mapping/key_value_mapping_rule.rb +4 -0
- data/lib/lutaml/model/mapping/mapping_rule.rb +8 -3
- data/lib/lutaml/model/register.rb +105 -0
- data/lib/lutaml/model/registrable.rb +6 -0
- data/lib/lutaml/model/schema/base_schema.rb +64 -0
- data/lib/lutaml/model/schema/decorators/attribute.rb +114 -0
- data/lib/lutaml/model/schema/decorators/choices.rb +31 -0
- data/lib/lutaml/model/schema/decorators/class_definition.rb +85 -0
- data/lib/lutaml/model/schema/decorators/definition_collection.rb +97 -0
- data/lib/lutaml/model/schema/generator/definition.rb +53 -0
- data/lib/lutaml/model/schema/generator/definitions_collection.rb +81 -0
- data/lib/lutaml/model/schema/generator/properties_collection.rb +63 -0
- data/lib/lutaml/model/schema/generator/property.rb +110 -0
- data/lib/lutaml/model/schema/generator/ref.rb +24 -0
- data/lib/lutaml/model/schema/helpers/template_helper.rb +49 -0
- data/lib/lutaml/model/schema/json_schema.rb +42 -49
- data/lib/lutaml/model/schema/relaxng_schema.rb +14 -10
- data/lib/lutaml/model/schema/renderer.rb +36 -0
- data/lib/lutaml/model/schema/shared_methods.rb +24 -0
- data/lib/lutaml/model/schema/templates/model.erb +9 -0
- data/lib/lutaml/model/schema/xml_compiler/attribute.rb +85 -0
- data/lib/lutaml/model/schema/xml_compiler/attribute_group.rb +45 -0
- data/lib/lutaml/model/schema/xml_compiler/choice.rb +65 -0
- data/lib/lutaml/model/schema/xml_compiler/complex_content.rb +27 -0
- data/lib/lutaml/model/schema/xml_compiler/complex_content_restriction.rb +34 -0
- data/lib/lutaml/model/schema/xml_compiler/complex_type.rb +136 -0
- data/lib/lutaml/model/schema/xml_compiler/element.rb +104 -0
- data/lib/lutaml/model/schema/xml_compiler/group.rb +97 -0
- data/lib/lutaml/model/schema/xml_compiler/restriction.rb +101 -0
- data/lib/lutaml/model/schema/xml_compiler/sequence.rb +50 -0
- data/lib/lutaml/model/schema/xml_compiler/simple_content.rb +36 -0
- data/lib/lutaml/model/schema/xml_compiler/simple_type.rb +189 -0
- data/lib/lutaml/model/schema/xml_compiler.rb +231 -587
- data/lib/lutaml/model/schema/xsd_schema.rb +12 -8
- data/lib/lutaml/model/schema/yaml_schema.rb +41 -35
- data/lib/lutaml/model/schema.rb +1 -0
- data/lib/lutaml/model/sequence.rb +60 -30
- data/lib/lutaml/model/serialize.rb +175 -53
- data/lib/lutaml/model/services/base.rb +11 -0
- data/lib/lutaml/model/services/logger.rb +2 -2
- data/lib/lutaml/model/services/rule_value_extractor.rb +92 -0
- data/lib/lutaml/model/services/type/validator/number.rb +25 -0
- data/lib/lutaml/model/services/type/validator/string.rb +52 -0
- data/lib/lutaml/model/services/type/validator.rb +43 -0
- data/lib/lutaml/model/services/validator.rb +145 -0
- data/lib/lutaml/model/services.rb +3 -0
- data/lib/lutaml/model/transform/key_value_transform.rb +60 -50
- data/lib/lutaml/model/transform/xml_transform.rb +46 -57
- data/lib/lutaml/model/transform.rb +22 -8
- data/lib/lutaml/model/type/boolean.rb +1 -1
- data/lib/lutaml/model/type/date.rb +1 -1
- data/lib/lutaml/model/type/date_time.rb +1 -1
- data/lib/lutaml/model/type/decimal.rb +11 -9
- data/lib/lutaml/model/type/float.rb +2 -1
- data/lib/lutaml/model/type/integer.rb +24 -21
- data/lib/lutaml/model/type/string.rb +4 -2
- data/lib/lutaml/model/type/time.rb +1 -1
- data/lib/lutaml/model/type/time_without_date.rb +1 -1
- data/lib/lutaml/model/type/value.rb +5 -1
- data/lib/lutaml/model/type.rb +5 -2
- data/lib/lutaml/model/utils.rb +30 -8
- data/lib/lutaml/model/validation.rb +6 -4
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/model/xml/document.rb +37 -19
- data/lib/lutaml/model/xml/mapping.rb +74 -13
- data/lib/lutaml/model/xml/mapping_rule.rb +10 -2
- data/lib/lutaml/model/xml/nokogiri_adapter.rb +5 -3
- data/lib/lutaml/model/xml/oga/element.rb +4 -1
- data/lib/lutaml/model/xml/oga_adapter.rb +4 -3
- data/lib/lutaml/model/xml/ox_adapter.rb +20 -6
- data/lib/lutaml/model/xml/xml_element.rb +3 -28
- data/lib/lutaml/model/xml_adapter/element.rb +1 -1
- data/lib/lutaml/model/xml_adapter/nokogiri_adapter.rb +1 -1
- data/lib/lutaml/model/xml_adapter/oga_adapter.rb +1 -1
- data/lib/lutaml/model/xml_adapter/ox_adapter.rb +1 -1
- data/lib/lutaml/model/yamls/document.rb +14 -0
- data/lib/lutaml/model/yamls/mapping.rb +19 -0
- data/lib/lutaml/model/yamls/mapping_rule.rb +9 -0
- data/lib/lutaml/model/yamls/standard_adapter.rb +34 -0
- data/lib/lutaml/model/yamls/transform.rb +19 -0
- data/lib/lutaml/model/yamls.rb +21 -0
- data/lib/lutaml/model.rb +7 -31
- data/spec/benchmarks/xml_parsing_benchmark_spec.rb +4 -5
- data/spec/fixtures/xml/advanced_test_schema.xsd +134 -0
- data/spec/fixtures/xml/examples/nested_categories.xml +55 -0
- data/spec/fixtures/xml/examples/valid_catalog.xml +43 -0
- data/spec/fixtures/xml/product_catalog.xsd +151 -0
- data/spec/fixtures/xml/specifications_schema.xsd +38 -0
- data/spec/lutaml/model/attribute_collection_spec.rb +101 -0
- data/spec/lutaml/model/attribute_spec.rb +41 -44
- data/spec/lutaml/model/choice_spec.rb +44 -0
- data/spec/lutaml/model/custom_collection_spec.rb +830 -0
- data/spec/lutaml/model/custom_model_spec.rb +15 -3
- data/spec/lutaml/model/defaults_spec.rb +5 -1
- data/spec/lutaml/model/global_register_spec.rb +108 -0
- data/spec/lutaml/model/group_spec.rb +9 -3
- data/spec/lutaml/model/jsonl/standard_adapter_spec.rb +91 -0
- data/spec/lutaml/model/jsonl_spec.rb +229 -0
- data/spec/lutaml/model/multiple_mapping_spec.rb +1 -1
- data/spec/lutaml/model/register/key_value_spec.rb +275 -0
- data/spec/lutaml/model/register/xml_spec.rb +187 -0
- data/spec/lutaml/model/register_spec.rb +147 -0
- data/spec/lutaml/model/rule_value_extractor_spec.rb +162 -0
- data/spec/lutaml/model/schema/generator/definitions_collection_spec.rb +120 -0
- data/spec/lutaml/model/schema/json_schema_spec.rb +412 -51
- data/spec/lutaml/model/schema/json_schema_to_models_spec.rb +383 -0
- data/spec/lutaml/model/schema/xml_compiler/attribute_group_spec.rb +65 -0
- data/spec/lutaml/model/schema/xml_compiler/attribute_spec.rb +63 -0
- data/spec/lutaml/model/schema/xml_compiler/choice_spec.rb +71 -0
- data/spec/lutaml/model/schema/xml_compiler/complex_content_restriction_spec.rb +55 -0
- data/spec/lutaml/model/schema/xml_compiler/complex_content_spec.rb +37 -0
- data/spec/lutaml/model/schema/xml_compiler/complex_type_spec.rb +173 -0
- data/spec/lutaml/model/schema/xml_compiler/element_spec.rb +63 -0
- data/spec/lutaml/model/schema/xml_compiler/group_spec.rb +86 -0
- data/spec/lutaml/model/schema/xml_compiler/restriction_spec.rb +76 -0
- data/spec/lutaml/model/schema/xml_compiler/sequence_spec.rb +59 -0
- data/spec/lutaml/model/schema/xml_compiler/simple_content_spec.rb +55 -0
- data/spec/lutaml/model/schema/xml_compiler/simple_type_spec.rb +181 -0
- data/spec/lutaml/model/schema/xml_compiler_spec.rb +503 -1804
- data/spec/lutaml/model/schema/yaml_schema_spec.rb +249 -26
- data/spec/lutaml/model/sequence_spec.rb +36 -0
- data/spec/lutaml/model/serializable_spec.rb +31 -0
- data/spec/lutaml/model/type_spec.rb +8 -4
- data/spec/lutaml/model/utils_spec.rb +3 -3
- data/spec/lutaml/model/xml/derived_attributes_spec.rb +1 -1
- data/spec/lutaml/model/xml/xml_element_spec.rb +7 -1
- data/spec/lutaml/model/xml_adapter/xml_namespace_spec.rb +6 -6
- data/spec/lutaml/model/xml_adapter_spec.rb +24 -0
- data/spec/lutaml/model/xml_mapping_rule_spec.rb +11 -4
- data/spec/lutaml/model/xml_mapping_spec.rb +1 -1
- data/spec/lutaml/model/yamls/standard_adapter_spec.rb +183 -0
- data/spec/lutaml/model/yamls_spec.rb +294 -0
- data/spec/spec_helper.rb +1 -0
- metadata +105 -9
- data/lib/lutaml/model/schema/templates/simple_type.rb +0 -247
- /data/lib/lutaml/model/{hash → hash_adapter}/document.rb +0 -0
- /data/lib/lutaml/model/{hash → hash_adapter}/mapping.rb +0 -0
- /data/lib/lutaml/model/{hash → hash_adapter}/mapping_rule.rb +0 -0
- /data/lib/lutaml/model/{hash → hash_adapter}/standard_adapter.rb +0 -0
- /data/lib/lutaml/model/{hash → hash_adapter}/transform.rb +0 -0
@@ -15,6 +15,7 @@ RSpec.describe Lutaml::Model::Attribute do
|
|
15
15
|
Class.new(Lutaml::Model::Serializable) do
|
16
16
|
attribute :age, :integer
|
17
17
|
attribute :image, :string
|
18
|
+
restrict :image, collection: 1..., pattern: /.*\.\w+$/
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
@@ -44,6 +45,45 @@ RSpec.describe Lutaml::Model::Attribute do
|
|
44
45
|
)
|
45
46
|
end
|
46
47
|
|
48
|
+
describe "#validate_name!" do
|
49
|
+
Lutaml::Model::Serializable.instance_methods.each do |method|
|
50
|
+
if Lutaml::Model::Attribute::ALLOW_OVERRIDING.include?(method)
|
51
|
+
before do
|
52
|
+
allow(Lutaml::Model::Logger).to receive(:warn)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "does not raise an error when method is `#{method}`" do
|
56
|
+
expect do
|
57
|
+
Class.new(Lutaml::Model::Serializable) do
|
58
|
+
attribute method, :string
|
59
|
+
end
|
60
|
+
end.not_to raise_error
|
61
|
+
end
|
62
|
+
|
63
|
+
it "logs a warning, when method is `#{method}`" do
|
64
|
+
Class.new(Lutaml::Model::Serializable) do
|
65
|
+
attribute method, :string
|
66
|
+
end
|
67
|
+
|
68
|
+
expect(Lutaml::Model::Logger)
|
69
|
+
.to have_received(:warn)
|
70
|
+
.with("Attribute name `#{method}` conflicts with a built-in method")
|
71
|
+
end
|
72
|
+
else
|
73
|
+
it "raise exception, when method is `#{method}`" do
|
74
|
+
expect do
|
75
|
+
Class.new(Lutaml::Model::Serializable) do
|
76
|
+
attribute method, :string
|
77
|
+
end
|
78
|
+
end.to raise_error(
|
79
|
+
Lutaml::Model::InvalidAttributeNameError,
|
80
|
+
"Attribute name '#{method}' is not allowed",
|
81
|
+
)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
47
87
|
describe "#validate_options!" do
|
48
88
|
let(:validate_options) { name_attr.method(:validate_options!) }
|
49
89
|
|
@@ -56,7 +96,7 @@ RSpec.describe Lutaml::Model::Attribute do
|
|
56
96
|
it "raise exception if option is not allowed" do
|
57
97
|
expect do
|
58
98
|
validate_options.call({ foo: "bar" })
|
59
|
-
end.to raise_error(
|
99
|
+
end.to raise_error(Lutaml::Model::InvalidAttributeOptionsError, "Invalid options given for `name` [:foo]")
|
60
100
|
end
|
61
101
|
|
62
102
|
it "raise exception if pattern is given with non string type" do
|
@@ -71,49 +111,6 @@ RSpec.describe Lutaml::Model::Attribute do
|
|
71
111
|
end
|
72
112
|
end
|
73
113
|
|
74
|
-
describe "#validate_type!" do
|
75
|
-
let(:validate_type) { name_attr.method(:validate_type!) }
|
76
|
-
|
77
|
-
it "return true if type is a class" do
|
78
|
-
expect(validate_type.call(TestRecord)).to be(true)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "return true if type is a valid symbol" do
|
82
|
-
expect(validate_type.call(:string)).to be(true)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "return true if type is a valid string" do
|
86
|
-
expect(validate_type.call("String")).to be(true)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "raise exception if type is invalid symbol" do
|
90
|
-
expect do
|
91
|
-
validate_type.call(:foo)
|
92
|
-
end.to raise_error(StandardError, "Unknown Lutaml::Model::Type: foo")
|
93
|
-
end
|
94
|
-
|
95
|
-
it "raise exception if type is invalid string" do
|
96
|
-
expect do
|
97
|
-
validate_type.call("foo")
|
98
|
-
end.to raise_error(StandardError, "Unknown Lutaml::Model::Type: foo")
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe "#cast_type!" do
|
103
|
-
it "cast :string to Lutaml::Model::Type::String" do
|
104
|
-
expect(name_attr.cast_type!(:string)).to eq(Lutaml::Model::Type::String)
|
105
|
-
end
|
106
|
-
|
107
|
-
it "cast :integer to Lutaml::Model::Type::Integer" do
|
108
|
-
expect(name_attr.cast_type!(:integer)).to eq(Lutaml::Model::Type::Integer)
|
109
|
-
end
|
110
|
-
|
111
|
-
it "raises ArgumentError on unknown type" do
|
112
|
-
expect { name_attr.cast_type!(:foobar) }
|
113
|
-
.to raise_error(ArgumentError, "Unknown Lutaml::Model::Type: foobar")
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
114
|
describe "#derived?" do
|
118
115
|
context "when type is set" do
|
119
116
|
let(:attribute) { described_class.new("name", :string) }
|
@@ -59,6 +59,29 @@ module ChoiceSpec
|
|
59
59
|
map :last_name, to: :last_name
|
60
60
|
end
|
61
61
|
end
|
62
|
+
|
63
|
+
class ContactEmail < Lutaml::Model::Serializable
|
64
|
+
attribute :email, :string
|
65
|
+
xml { no_root }
|
66
|
+
end
|
67
|
+
|
68
|
+
class ContactPhone < Lutaml::Model::Serializable
|
69
|
+
attribute :phone, :string
|
70
|
+
xml { no_root }
|
71
|
+
end
|
72
|
+
|
73
|
+
class Person < Lutaml::Model::Serializable
|
74
|
+
choice(min: 1, max: 2) do
|
75
|
+
import_model_attributes ContactEmail
|
76
|
+
import_model_attributes ContactPhone
|
77
|
+
end
|
78
|
+
|
79
|
+
xml do
|
80
|
+
root "Person"
|
81
|
+
map_element :email, to: :email
|
82
|
+
map_element :phone, to: :phone
|
83
|
+
end
|
84
|
+
end
|
62
85
|
end
|
63
86
|
|
64
87
|
RSpec.describe "Choice" do
|
@@ -165,4 +188,25 @@ RSpec.describe "Choice" do
|
|
165
188
|
end.to raise_error(Lutaml::Model::InvalidChoiceRangeError, "Choice lower bound `-1` must be positive")
|
166
189
|
end
|
167
190
|
end
|
191
|
+
|
192
|
+
context "with choice using import_model_attributes" do
|
193
|
+
it "parses XML with both email and phone using choice and import_model_attributes" do
|
194
|
+
xml = <<~XML
|
195
|
+
<Person>
|
196
|
+
<email>john.doe@example.com</email>
|
197
|
+
<phone>1234567890</phone>
|
198
|
+
</Person>
|
199
|
+
XML
|
200
|
+
person = ChoiceSpec::Person.from_xml(xml)
|
201
|
+
expect(person.email).to eq("john.doe@example.com")
|
202
|
+
expect(person.phone).to eq("1234567890")
|
203
|
+
end
|
204
|
+
|
205
|
+
it "raises error if no contact info is provided" do
|
206
|
+
xml = <<~XML
|
207
|
+
<Person></Person>
|
208
|
+
XML
|
209
|
+
expect { ChoiceSpec::Person.from_xml(xml).validate! }.to raise_error(Lutaml::Model::ValidationError)
|
210
|
+
end
|
211
|
+
end
|
168
212
|
end
|