parametric 0.2.10 → 0.2.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/README.md +265 -36
- data/bench/struct_bench.rb +53 -0
- data/lib/parametric/block_validator.rb +2 -0
- data/lib/parametric/context.rb +6 -1
- data/lib/parametric/default_types.rb +2 -0
- data/lib/parametric/dsl.rb +2 -0
- data/lib/parametric/field.rb +62 -25
- data/lib/parametric/field_dsl.rb +2 -0
- data/lib/parametric/policies.rb +28 -0
- data/lib/parametric/policy_adapter.rb +57 -0
- data/lib/parametric/registry.rb +2 -0
- data/lib/parametric/results.rb +2 -0
- data/lib/parametric/schema.rb +39 -3
- data/lib/parametric/struct.rb +30 -20
- data/lib/parametric/tagged_one_of.rb +134 -0
- data/lib/parametric/version.rb +3 -1
- data/lib/parametric.rb +2 -0
- data/parametric.gemspec +1 -2
- data/spec/field_spec.rb +32 -0
- data/spec/policies_spec.rb +1 -1
- data/spec/schema_lifecycle_hooks_spec.rb +133 -0
- data/spec/schema_spec.rb +81 -0
- data/spec/struct_spec.rb +32 -35
- data/spec/validators_spec.rb +7 -0
- metadata +13 -23
data/spec/struct_spec.rb
CHANGED
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
require 'parametric/struct'
|
3
3
|
|
4
4
|
describe Parametric::Struct do
|
5
|
-
|
6
|
-
|
5
|
+
let(:friend_class) do
|
6
|
+
Class.new do
|
7
7
|
include Parametric::Struct
|
8
8
|
|
9
9
|
schema do
|
@@ -11,16 +11,19 @@ describe Parametric::Struct do
|
|
11
11
|
field(:age).type(:integer)
|
12
12
|
end
|
13
13
|
end
|
14
|
+
end
|
15
|
+
let(:klass) do
|
16
|
+
Class.new.tap do |cl|
|
17
|
+
cl.send(:include, Parametric::Struct)
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
schema do
|
19
|
-
field(:title).type(:string).present
|
20
|
-
field(:friends).type(:array).default([]).schema friend_class
|
19
|
+
cl.schema do |sc, _|
|
20
|
+
sc.field(:title).type(:string).present
|
21
|
+
sc.field(:friends).type(:array).default([]).schema friend_class
|
21
22
|
end
|
22
23
|
end
|
24
|
+
end
|
23
25
|
|
26
|
+
it "works" do
|
24
27
|
new_instance = klass.new
|
25
28
|
expect(new_instance.title).to eq ''
|
26
29
|
expect(new_instance.friends).to eq []
|
@@ -40,6 +43,10 @@ describe Parametric::Struct do
|
|
40
43
|
expect(instance.friends.first.name).to eq 'Ismael'
|
41
44
|
expect(instance.friends.first).to be_a friend_class
|
42
45
|
|
46
|
+
# Hash access with #[]
|
47
|
+
expect(instance[:title]).to eq instance.title
|
48
|
+
expect(instance[:friends].first[:name]).to eq instance.friends.first.name
|
49
|
+
|
43
50
|
invalid_instance = klass.new({
|
44
51
|
friends: [
|
45
52
|
{name: 'Ismael', age: 40},
|
@@ -53,6 +60,18 @@ describe Parametric::Struct do
|
|
53
60
|
expect(invalid_instance.friends[1].errors['$.name']).not_to be_nil
|
54
61
|
end
|
55
62
|
|
63
|
+
it 'supports #structure' do
|
64
|
+
st = klass.schema.structure
|
65
|
+
expect(st[:title][:type]).to eq(:string)
|
66
|
+
expect(st[:friends][:structure][:age][:type]).to eq(:integer)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'supports #walk' do
|
70
|
+
output = klass.schema.walk(:type).output
|
71
|
+
expect(output[:title]).to eq(:string)
|
72
|
+
expect(output[:friends][0][:name]).to eq(:string)
|
73
|
+
end
|
74
|
+
|
56
75
|
it "is inmutable by default" do
|
57
76
|
klass = Class.new do
|
58
77
|
include Parametric::Struct
|
@@ -99,32 +118,6 @@ describe Parametric::Struct do
|
|
99
118
|
expect(instance.friends.first.age).to eq 10
|
100
119
|
end
|
101
120
|
|
102
|
-
it 'wraps nested schemas in custom class' do
|
103
|
-
klass = Class.new do
|
104
|
-
include Parametric::Struct
|
105
|
-
|
106
|
-
def self.parametric_build_class_for_child(key, child_schema)
|
107
|
-
Class.new do
|
108
|
-
include Parametric::Struct
|
109
|
-
schema child_schema
|
110
|
-
def salutation
|
111
|
-
"my age is #{age}"
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
schema do
|
117
|
-
field(:name).type(:string).present
|
118
|
-
field(:friends).type(:array).schema do
|
119
|
-
field(:age).type(:integer)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
user = klass.new(name: 'Ismael', friends: [{age: 43}])
|
125
|
-
expect(user.friends.first.salutation).to eq 'my age is 43'
|
126
|
-
end
|
127
|
-
|
128
121
|
it "wraps regular schemas in structs" do
|
129
122
|
friend_schema = Parametric::Schema.new do
|
130
123
|
field(:name)
|
@@ -308,6 +301,9 @@ describe Parametric::Struct do
|
|
308
301
|
|
309
302
|
schema do
|
310
303
|
field(:title).type(:string).present
|
304
|
+
field(:consumption).type(:object).present.schema do
|
305
|
+
field(:type).present.options(%w[aaa bbb])
|
306
|
+
end
|
311
307
|
end
|
312
308
|
end
|
313
309
|
|
@@ -315,9 +311,10 @@ describe Parametric::Struct do
|
|
315
311
|
klass.new!(title: '')
|
316
312
|
rescue Parametric::InvalidStructError => e
|
317
313
|
expect(e.errors['$.title']).not_to be nil
|
314
|
+
expect(e.errors['$.consumption']).not_to be nil
|
318
315
|
end
|
319
316
|
|
320
|
-
valid = klass.new!(title: 'foo')
|
317
|
+
valid = klass.new!(title: 'foo', consumption: { type: 'aaa' })
|
321
318
|
expect(valid.title).to eq 'foo'
|
322
319
|
end
|
323
320
|
end
|
data/spec/validators_spec.rb
CHANGED
@@ -9,6 +9,13 @@ describe 'default validators' do
|
|
9
9
|
expect(validator.valid?(payload[key], key, payload)).to eq valid
|
10
10
|
end
|
11
11
|
|
12
|
+
describe ':value' do
|
13
|
+
it {
|
14
|
+
test_validator({key: 'Foobar'}, :key, :value, true, true, 'Foobar')
|
15
|
+
test_validator({key: 'Nope'}, :key, :value, true, false, 'Foobar')
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
12
19
|
describe ':format' do
|
13
20
|
it {
|
14
21
|
test_validator({key: 'Foobar'}, :key, :format, true, true, /^Foo/)
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parametric
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.19
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.5'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.5'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,14 +30,14 @@ dependencies:
|
|
44
30
|
requirements:
|
45
31
|
- - '='
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3.
|
33
|
+
version: 3.12.0
|
48
34
|
type: :development
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
38
|
- - '='
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3.
|
40
|
+
version: 3.12.0
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: byebug
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,6 +67,7 @@ files:
|
|
81
67
|
- LICENSE.txt
|
82
68
|
- README.md
|
83
69
|
- Rakefile
|
70
|
+
- bench/struct_bench.rb
|
84
71
|
- bin/console
|
85
72
|
- lib/parametric.rb
|
86
73
|
- lib/parametric/block_validator.rb
|
@@ -90,10 +77,12 @@ files:
|
|
90
77
|
- lib/parametric/field.rb
|
91
78
|
- lib/parametric/field_dsl.rb
|
92
79
|
- lib/parametric/policies.rb
|
80
|
+
- lib/parametric/policy_adapter.rb
|
93
81
|
- lib/parametric/registry.rb
|
94
82
|
- lib/parametric/results.rb
|
95
83
|
- lib/parametric/schema.rb
|
96
84
|
- lib/parametric/struct.rb
|
85
|
+
- lib/parametric/tagged_one_of.rb
|
97
86
|
- lib/parametric/version.rb
|
98
87
|
- parametric.gemspec
|
99
88
|
- spec/custom_block_validator_spec.rb
|
@@ -101,6 +90,7 @@ files:
|
|
101
90
|
- spec/expand_spec.rb
|
102
91
|
- spec/field_spec.rb
|
103
92
|
- spec/policies_spec.rb
|
93
|
+
- spec/schema_lifecycle_hooks_spec.rb
|
104
94
|
- spec/schema_spec.rb
|
105
95
|
- spec/schema_walk_spec.rb
|
106
96
|
- spec/spec_helper.rb
|
@@ -110,7 +100,7 @@ homepage: ''
|
|
110
100
|
licenses:
|
111
101
|
- MIT
|
112
102
|
metadata: {}
|
113
|
-
post_install_message:
|
103
|
+
post_install_message:
|
114
104
|
rdoc_options: []
|
115
105
|
require_paths:
|
116
106
|
- lib
|
@@ -125,9 +115,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
115
|
- !ruby/object:Gem::Version
|
126
116
|
version: '0'
|
127
117
|
requirements: []
|
128
|
-
|
129
|
-
|
130
|
-
signing_key:
|
118
|
+
rubygems_version: 3.4.18
|
119
|
+
signing_key:
|
131
120
|
specification_version: 4
|
132
121
|
summary: DSL for declaring allowed parameters with options, regexp patern and default
|
133
122
|
values.
|
@@ -137,6 +126,7 @@ test_files:
|
|
137
126
|
- spec/expand_spec.rb
|
138
127
|
- spec/field_spec.rb
|
139
128
|
- spec/policies_spec.rb
|
129
|
+
- spec/schema_lifecycle_hooks_spec.rb
|
140
130
|
- spec/schema_spec.rb
|
141
131
|
- spec/schema_walk_spec.rb
|
142
132
|
- spec/spec_helper.rb
|