parametric 0.2.16 → 0.2.17
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/README.md +8 -0
- data/lib/parametric/field.rb +1 -1
- data/lib/parametric/policies.rb +26 -0
- data/lib/parametric/schema.rb +1 -1
- data/lib/parametric/version.rb +1 -1
- data/spec/field_spec.rb +10 -0
- data/spec/struct_spec.rb +23 -8
- data/spec/validators_spec.rb +7 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71e71a5ddf414bb333a499f425d68e891f60a2e0610a8a57d8362f6481da16e6
|
4
|
+
data.tar.gz: 5c13b835147015f11f66591d311612139b16c4001891451d299c39358045725c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cd1bb404bb5bcf24fc7b4826dcf360d367efb23cbe0e1d300a25f3e0a709ee5fb4e9d17db5820803a9e8f2f0b65e68ddae5c27325573f7fc213c7814a8f4e04
|
7
|
+
data.tar.gz: 0fd11bca14c7c75decb6dcc9781ef1b26a3ecc7bed3b4348b30f5c8c06ad57237b10beb7d3acd6a5b1f0e8989823da29e2f3bc3956f51856b1e57b852a7bdd5d
|
data/README.md
CHANGED
@@ -275,6 +275,14 @@ Useful for parsing comma-separated query-string parameters.
|
|
275
275
|
field(:status).policy(:split) # turns "pending,confirmed" into ["pending", "confirmed"]
|
276
276
|
```
|
277
277
|
|
278
|
+
### :value
|
279
|
+
|
280
|
+
A policy to return a static value
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
field(:currency).policy(:value, 'gbp') # this field always resolves to 'gbp'
|
284
|
+
```
|
285
|
+
|
278
286
|
## Custom policies
|
279
287
|
|
280
288
|
You can also register your own custom policy objects. A policy must implement the following methods:
|
data/lib/parametric/field.rb
CHANGED
@@ -61,7 +61,7 @@ module Parametric
|
|
61
61
|
|
62
62
|
def visit(meta_key = nil, &visitor)
|
63
63
|
if sc = meta_data[:schema]
|
64
|
-
r = sc.visit(meta_key, &visitor)
|
64
|
+
r = sc.schema.visit(meta_key, &visitor)
|
65
65
|
(meta_data[:type] == :array) ? [r] : r
|
66
66
|
else
|
67
67
|
meta_key ? meta_data[meta_key] : yield(self)
|
data/lib/parametric/policies.rb
CHANGED
@@ -26,6 +26,31 @@ module Parametric
|
|
26
26
|
{}
|
27
27
|
end
|
28
28
|
end
|
29
|
+
|
30
|
+
class Value
|
31
|
+
attr_reader :message
|
32
|
+
|
33
|
+
def initialize(val, msg = 'invalid value')
|
34
|
+
@message = msg
|
35
|
+
@val = val
|
36
|
+
end
|
37
|
+
|
38
|
+
def eligible?(value, key, payload)
|
39
|
+
payload.key?(key)
|
40
|
+
end
|
41
|
+
|
42
|
+
def coerce(_value, _key, _context)
|
43
|
+
@val
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid?(value, key, payload)
|
47
|
+
!payload.key?(key) || !!(value == @val)
|
48
|
+
end
|
49
|
+
|
50
|
+
def meta_data
|
51
|
+
{ value: @val }
|
52
|
+
end
|
53
|
+
end
|
29
54
|
end
|
30
55
|
|
31
56
|
# Default validators
|
@@ -33,6 +58,7 @@ module Parametric
|
|
33
58
|
|
34
59
|
Parametric.policy :format, Policies::Format
|
35
60
|
Parametric.policy :email, Policies::Format.new(EMAIL_REGEXP, 'invalid email')
|
61
|
+
Parametric.policy :value, Policies::Value
|
36
62
|
|
37
63
|
Parametric.policy :noop do
|
38
64
|
eligible do |value, key, payload|
|
data/lib/parametric/schema.rb
CHANGED
data/lib/parametric/version.rb
CHANGED
data/spec/field_spec.rb
CHANGED
@@ -264,6 +264,16 @@ describe Parametric::Field do
|
|
264
264
|
end
|
265
265
|
end
|
266
266
|
|
267
|
+
describe ":value policy" do
|
268
|
+
it 'always resolves to static value' do
|
269
|
+
resolve(subject.policy(:value, 'hello'), a_key: "tag1,tag2").tap do |r|
|
270
|
+
expect(r.eligible?).to be true
|
271
|
+
no_errors
|
272
|
+
expect(r.value).to eq 'hello'
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
267
277
|
describe ":declared policy" do
|
268
278
|
it "is eligible if key exists" do
|
269
279
|
resolve(subject.policy(:declared).present, a_key: "").tap do |r|
|
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 []
|
@@ -57,6 +60,18 @@ describe Parametric::Struct do
|
|
57
60
|
expect(invalid_instance.friends[1].errors['$.name']).not_to be_nil
|
58
61
|
end
|
59
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
|
+
|
60
75
|
it "is inmutable by default" do
|
61
76
|
klass = Class.new do
|
62
77
|
include Parametric::Struct
|
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,14 +1,14 @@
|
|
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.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|