dry-schema 0.1.0 → 1.0.0
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/CHANGELOG.md +166 -0
- data/LICENSE +1 -1
- data/README.md +13 -4
- data/config/errors.yml +59 -54
- data/lib/dry/schema/compiler.rb +24 -0
- data/lib/dry/schema/config.rb +23 -27
- data/lib/dry/schema/constants.rb +27 -3
- data/lib/dry/schema/dsl.rb +36 -49
- data/lib/dry/schema/extensions/hints/compiler_methods.rb +19 -0
- data/lib/dry/schema/extensions/hints/message_compiler_methods.rb +84 -0
- data/lib/dry/schema/extensions/hints/message_set_methods.rb +44 -0
- data/lib/dry/schema/extensions/hints/result_methods.rb +47 -0
- data/lib/dry/schema/extensions/hints.rb +56 -0
- data/lib/dry/schema/extensions/monads.rb +15 -4
- data/lib/dry/schema/extensions.rb +6 -0
- data/lib/dry/schema/json.rb +4 -2
- data/lib/dry/schema/key.rb +8 -10
- data/lib/dry/schema/key_coercer.rb +6 -2
- data/lib/dry/schema/key_map.rb +5 -4
- data/lib/dry/schema/macros/array.rb +39 -0
- data/lib/dry/schema/macros/core.rb +2 -0
- data/lib/dry/schema/macros/dsl.rb +149 -4
- data/lib/dry/schema/macros/each.rb +12 -1
- data/lib/dry/schema/macros/filled.rb +14 -7
- data/lib/dry/schema/macros/hash.rb +6 -32
- data/lib/dry/schema/macros/key.rb +38 -60
- data/lib/dry/schema/macros/maybe.rb +5 -3
- data/lib/dry/schema/macros/optional.rb +8 -1
- data/lib/dry/schema/macros/required.rb +3 -1
- data/lib/dry/schema/macros/schema.rb +46 -0
- data/lib/dry/schema/macros/value.rb +18 -2
- data/lib/dry/schema/macros.rb +4 -0
- data/lib/dry/schema/message/or.rb +54 -0
- data/lib/dry/schema/message.rb +56 -72
- data/lib/dry/schema/message_compiler/visitor_opts.rb +28 -1
- data/lib/dry/schema/message_compiler.rb +85 -74
- data/lib/dry/schema/message_set.rb +82 -50
- data/lib/dry/schema/messages/abstract.rb +84 -44
- data/lib/dry/schema/messages/i18n.rb +45 -9
- data/lib/dry/schema/messages/namespaced.rb +22 -5
- data/lib/dry/schema/messages/template.rb +68 -0
- data/lib/dry/schema/messages/yaml.rb +76 -22
- data/lib/dry/schema/messages.rb +19 -24
- data/lib/dry/schema/namespaced_rule.rb +37 -0
- data/lib/dry/schema/params.rb +4 -2
- data/lib/dry/schema/path.rb +102 -0
- data/lib/dry/schema/predicate.rb +15 -0
- data/lib/dry/schema/predicate_inferrer.rb +116 -23
- data/lib/dry/schema/predicate_registry.rb +2 -0
- data/lib/dry/schema/processor.rb +70 -39
- data/lib/dry/schema/result.rb +16 -31
- data/lib/dry/schema/rule_applier.rb +10 -3
- data/lib/dry/schema/trace.rb +16 -3
- data/lib/dry/schema/type_registry.rb +5 -3
- data/lib/dry/schema/types.rb +3 -1
- data/lib/dry/schema/value_coercer.rb +10 -4
- data/lib/dry/schema/version.rb +3 -1
- data/lib/dry/schema.rb +39 -13
- data/lib/dry-schema.rb +2 -0
- metadata +43 -47
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 63100127599f36457192bd183784388f1ca2328c0bd91f4e4d2adb2498b57bb2
|
|
4
|
+
data.tar.gz: 82c6cad8a1bda4c89723a2ccccce7eab8bb56c9e90b0ce385cc4d632993be90c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dbef0b527a8e4742b3845d8bebf45b88d960fa969956599f6504ce785fb9d2b7461ad8cc94e2e4b238956dc79641007d54be3b435f73551f2219f01db8b3f60d
|
|
7
|
+
data.tar.gz: c01c98b4600c7fcec9efa3f38f0cd6a27b86191804a7502b80da81d564282cb581ac5bc69c5c006cebb4826d0f7d6653230612a80f530a301494d1fe5af01203
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,169 @@
|
|
|
1
|
+
# 1.0.0 2019-05-03
|
|
2
|
+
|
|
3
|
+
### Changed
|
|
4
|
+
|
|
5
|
+
* [BREAKING] `Result#to_hash` was removed (solnic)
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
* Setting `:any` as the type spec no longer crashes (solnic)
|
|
10
|
+
* `Result#error?` handles paths to array elements correctly (solnic)
|
|
11
|
+
|
|
12
|
+
[Compare v0.6.0...v1.0.0](https://github.com/dry-rb/dry-schema/compare/v0.6.0...v1.0.0)
|
|
13
|
+
|
|
14
|
+
# 0.6.0 2019-04-24
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
* Dependency on `dry-types` was bumped to `~> 1.0` (solnic)
|
|
19
|
+
* Dependency on `dry-logic` was bumped to `~> 1.0` (solnic)
|
|
20
|
+
* Dependency on `dry-initializer` was bumped to `~> 3.0` (solnic)
|
|
21
|
+
|
|
22
|
+
[Compare v0.5.1...v0.6.0](https://github.com/dry-rb/dry-schema/compare/v0.5.1...v0.6.0)
|
|
23
|
+
|
|
24
|
+
# 0.5.1 2019-04-17
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
* Key map no longer crashes on unexpected input (issue #118) (solnic)
|
|
29
|
+
|
|
30
|
+
[Compare v0.5.0...v0.5.1](https://github.com/dry-rb/dry-schema/compare/v0.5.0...v0.5.1)
|
|
31
|
+
|
|
32
|
+
# 0.5.0 2019-04-04
|
|
33
|
+
|
|
34
|
+
### Added
|
|
35
|
+
|
|
36
|
+
* Support for arbitrary meta-data in messages, ie:
|
|
37
|
+
|
|
38
|
+
```yaml
|
|
39
|
+
en:
|
|
40
|
+
dry_schema:
|
|
41
|
+
errors:
|
|
42
|
+
filled?:
|
|
43
|
+
text: "cannot be blank"
|
|
44
|
+
code: 123
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Now your error hash will include `{ foo: [{ text: 'cannot be blank', code: 123 }] }` (solnic + flash-gordon)
|
|
48
|
+
|
|
49
|
+
* Support for type specs in `array` macro, ie `required(:tags).array(:integer)` (solnic)
|
|
50
|
+
* Support for type specs in `each` macro, ie `required(:tags).each(:integer)` (solnic)
|
|
51
|
+
* Shortcut for defining an array with hash as its member, ie:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
Dry::Schema.Params do
|
|
55
|
+
required(:tags).array(:hash) do
|
|
56
|
+
required(:name).filled(:string)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Fixed
|
|
62
|
+
|
|
63
|
+
* Inferring predicates doesn't crash when `Any` type is used (flash-gordon)
|
|
64
|
+
* Inferring type specs when type is already set works correctly (solnic)
|
|
65
|
+
|
|
66
|
+
### Changed
|
|
67
|
+
|
|
68
|
+
* [BREAKING] `:monads` extension wraps entire result objects in `Success` or `Failure` (flash-gordon)
|
|
69
|
+
* When `:hints` are disabled, result AST will not include hint nodes (solnic)
|
|
70
|
+
|
|
71
|
+
[Compare v0.4.0...v0.5.0](https://github.com/dry-rb/dry-schema/compare/v0.4.0...v0.5.0)
|
|
72
|
+
|
|
73
|
+
# 0.4.0 2019-03-26
|
|
74
|
+
|
|
75
|
+
### Added
|
|
76
|
+
|
|
77
|
+
* Schemas are now compatible with procs via `#to_proc` (issue #53) (solnic)
|
|
78
|
+
* Support for configuring `top_namespace` for localized messages (solnic)
|
|
79
|
+
* Support for configuring more than one load path for localized messages (solnic)
|
|
80
|
+
* Support for inferring predicates from arbitrary types (issue #101) (solnic)
|
|
81
|
+
|
|
82
|
+
### Fixed
|
|
83
|
+
|
|
84
|
+
* Handling of messages for `optional` keys without value rules works correctly (issue #87) (solnic)
|
|
85
|
+
* Message structure for `optional` keys with an array of hashes no longer duplicates keys (issue #89) (solnic)
|
|
86
|
+
* Inferring `:date_time?` predicate works correctly with `DateTime` types (issue #97) (solnic)
|
|
87
|
+
|
|
88
|
+
### Changed
|
|
89
|
+
|
|
90
|
+
* [BREAKING] Updated to work with `dry-types 0.15.0` (flash-gordon)
|
|
91
|
+
* [BREAKING] `Result#{errors,messages,hints}` returns `MessageSet` object now which is an enumerable coercible to a hash (solnic)
|
|
92
|
+
* [BREAKING] `Messages` backend classes no longer use global configuration (solnic)
|
|
93
|
+
* [BREAKING] Passing a non-symbol key name in the DSL will raise `ArgumentError` (issue #29) (solnic)
|
|
94
|
+
* [BREAKING] Configuration for message backends is now nested under `messages` key with following settings:
|
|
95
|
+
* `messages.backend` - previously `messages`
|
|
96
|
+
* `messages.load_paths` - previously `messages_path`
|
|
97
|
+
* `messages.namespace` - previously `namespace`
|
|
98
|
+
* `messages.top_namespace` - **new setting** see above
|
|
99
|
+
* [BREAKING] `Messages::I18n` uses `I18.store_translations` instead of messing with `I18n.load_path` (solnic)
|
|
100
|
+
* Schemas (`Params` and `JSON`) have nicer inspect (solnic)
|
|
101
|
+
|
|
102
|
+
[Compare v0.3.0...v0.4.0](https://github.com/dry-rb/dry-schema/compare/v0.3.0...v0.4.0)
|
|
103
|
+
|
|
104
|
+
# 0.3.0 2018-03-04
|
|
105
|
+
|
|
106
|
+
### Fixed
|
|
107
|
+
|
|
108
|
+
* Configuration is properly inherited from a parent schema (skryukov)
|
|
109
|
+
* `Result#error?` returns `true` when a preceding key has errors (solnic)
|
|
110
|
+
* Predicate inferrer no longer chokes on sum, constructor and enum types (solnic)
|
|
111
|
+
* Predicate inferrer infers `:bool?` from boolean types (solnic)
|
|
112
|
+
* Block-based definitions using `array` works correctly (solnic)
|
|
113
|
+
* Using a disjunction with `array` and `hash` produces correct errors when element validation for array failed (solnic)
|
|
114
|
+
|
|
115
|
+
### Changed
|
|
116
|
+
|
|
117
|
+
* Required ruby version was removed from gemspec for people who are stuck on MRI 2.3.x (solnic)
|
|
118
|
+
|
|
119
|
+
[Compare v0.2.0...v0.3.0](https://github.com/dry-rb/dry-schema/compare/v0.2.0...v0.3.0)
|
|
120
|
+
|
|
121
|
+
# 0.2.0 2019-02-26
|
|
122
|
+
|
|
123
|
+
### Added
|
|
124
|
+
|
|
125
|
+
* New `hash` macro which prepends `hash?` type-check and allows nested schema definition (solnic)
|
|
126
|
+
* New `array` macro which works like `each` but prepends `array?` type-check (solnic)
|
|
127
|
+
|
|
128
|
+
### Fixed
|
|
129
|
+
|
|
130
|
+
* Rule name translation works correctly with I18n (issue #52) (solnic)
|
|
131
|
+
* Rule name translation works correctly with namespaced messages (both I18n and plain YAML) (issue #57) (solnic)
|
|
132
|
+
* Error messages under namespaces are correctly resolved for overridden names (issue #53) (solnic)
|
|
133
|
+
* Namespaced error messages work correctly when schemas are reused within other schemas (issue #49) (solnic)
|
|
134
|
+
* Child schema can override inherited rules now (issue #66) (skryukov)
|
|
135
|
+
* Hints are correctly generated for disjunction that use type-check predicates (issue #24) (solnic)
|
|
136
|
+
* Hints are correctly generated for nested schemas (issue #26) (solnic)
|
|
137
|
+
* `filled` macro respects inferred type-check predicates and puts them in front (solnic)
|
|
138
|
+
* Value coercion works correctly with re-usable nested schemas (issue #25) (solnic)
|
|
139
|
+
|
|
140
|
+
### Changed
|
|
141
|
+
|
|
142
|
+
* [BREAKING] **Messages are now configured under `dry_schema` namespace by default** (issue #38) (solnic)
|
|
143
|
+
* [BREAKING] Hints are now an optional feature provided by `:hints` extension, to load it do `Dry::Schema.load_extensions(:hints)` (solnic)
|
|
144
|
+
* [BREAKING] Hints generation was improved in general, output of `Result#messages` and `Result#hints` changed in some cases (solnic)
|
|
145
|
+
* [BREAKING] `schema` macro no longer prepends `hash?` check, for this behavior use the new `hash` macro (see #31) (solnic)
|
|
146
|
+
* [BREAKING] Support for MRI < 2.4 was dropped (solnic)
|
|
147
|
+
|
|
148
|
+
[Compare v0.1.1...v0.2.0](https://github.com/dry-rb/dry-schema/compare/v0.1.1...v0.2.0)
|
|
149
|
+
|
|
150
|
+
# 0.1.1 2019-02-17
|
|
151
|
+
|
|
152
|
+
### Added
|
|
153
|
+
|
|
154
|
+
- `Result#error?` supports checking nested errors too ie`result.error?('user.address')` (solnic)
|
|
155
|
+
|
|
156
|
+
### Fixed
|
|
157
|
+
|
|
158
|
+
- Fix issues with templates and invalid tokens (issue #27) (solnic)
|
|
159
|
+
- Fix Ruby warnings (flash-gordon)
|
|
160
|
+
|
|
161
|
+
### Internal
|
|
162
|
+
|
|
163
|
+
- Key and value coercers are now equalizable (flash-gordon)
|
|
164
|
+
|
|
165
|
+
[Compare v0.1.0...v0.1.1](https://github.com/dry-rb/dry-schema/compare/v0.1.0...v0.1.1)
|
|
166
|
+
|
|
1
167
|
# 0.1.0 2019-01-30
|
|
2
168
|
|
|
3
169
|
Initial release.
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[gem]: https://rubygems.org/gems/dry-schema
|
|
2
2
|
[travis]: https://travis-ci.org/dry-rb/dry-schema
|
|
3
3
|
[codeclimate]: https://codeclimate.com/github/dry-rb/dry-schema
|
|
4
|
-
[
|
|
4
|
+
[chat]: https://dry-rb.zulipchat.com
|
|
5
5
|
[inchpages]: http://inch-ci.org/github/dry-rb/dry-schema
|
|
6
6
|
|
|
7
|
-
# dry-schema [][chat]
|
|
8
8
|
|
|
9
9
|
[][gem]
|
|
10
10
|
[][travis]
|
|
@@ -12,9 +12,18 @@
|
|
|
12
12
|
[][codeclimate]
|
|
13
13
|
[][inchpages]
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
## Links
|
|
16
16
|
|
|
17
|
-
* [Documentation](http://dry-rb.org/gems/dry-schema)
|
|
17
|
+
* [Documentation](http://dry-rb.org/gems/dry-schema)
|
|
18
|
+
|
|
19
|
+
## Supported Ruby versions
|
|
20
|
+
|
|
21
|
+
This library officially supports following Ruby versions:
|
|
22
|
+
|
|
23
|
+
* MRI >= `2.4`
|
|
24
|
+
* jruby >= `9.2`
|
|
25
|
+
|
|
26
|
+
It **should** work on MRI `2.3.x` too, but there's no official support for this version.
|
|
18
27
|
|
|
19
28
|
## License
|
|
20
29
|
|
data/config/errors.yml
CHANGED
|
@@ -1,91 +1,96 @@
|
|
|
1
1
|
en:
|
|
2
|
-
|
|
2
|
+
dry_schema:
|
|
3
3
|
or: "or"
|
|
4
|
-
array?: "must be an array"
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
errors:
|
|
6
|
+
array?: "must be an array"
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
empty?: "must be empty"
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
arg:
|
|
12
|
-
default: "must not be one of: %{list}"
|
|
13
|
-
range: "must not be one of: %{list_left} - %{list_right}"
|
|
14
|
-
exclusion?: "must not be one of: %{list}"
|
|
10
|
+
excludes?: "must not include %{value}"
|
|
15
11
|
|
|
16
|
-
|
|
12
|
+
excluded_from?:
|
|
13
|
+
arg:
|
|
14
|
+
default: "must not be one of: %{list}"
|
|
15
|
+
range: "must not be one of: %{list_left} - %{list_right}"
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
exclusion?: "must not be one of: %{list}"
|
|
19
18
|
|
|
20
|
-
|
|
19
|
+
eql?: "must be equal to %{left}"
|
|
21
20
|
|
|
22
|
-
|
|
21
|
+
not_eql?: "must not be equal to %{left}"
|
|
23
22
|
|
|
24
|
-
|
|
23
|
+
filled?: "must be filled"
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
format?: "is in invalid format"
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
number?: "must be a number"
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
odd?: "must be odd"
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
even?: "must be even"
|
|
33
32
|
|
|
34
|
-
|
|
33
|
+
gt?: "must be greater than %{num}"
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
arg:
|
|
38
|
-
default: "must be one of: %{list}"
|
|
39
|
-
range: "must be one of: %{list_left} - %{list_right}"
|
|
40
|
-
inclusion?: "must be one of: %{list}"
|
|
35
|
+
gteq?: "must be greater than or equal to %{num}"
|
|
41
36
|
|
|
42
|
-
|
|
37
|
+
hash?: "must be a hash"
|
|
43
38
|
|
|
44
|
-
|
|
39
|
+
included_in?:
|
|
40
|
+
arg:
|
|
41
|
+
default: "must be one of: %{list}"
|
|
42
|
+
range: "must be one of: %{list_left} - %{list_right}"
|
|
45
43
|
|
|
46
|
-
|
|
44
|
+
inclusion?: "must be one of: %{list}"
|
|
47
45
|
|
|
48
|
-
|
|
46
|
+
includes?: "must include %{value}"
|
|
49
47
|
|
|
50
|
-
|
|
48
|
+
bool?: "must be boolean"
|
|
51
49
|
|
|
52
|
-
|
|
50
|
+
true?: "must be true"
|
|
53
51
|
|
|
54
|
-
|
|
52
|
+
false?: "must be false"
|
|
55
53
|
|
|
56
|
-
|
|
54
|
+
int?: "must be an integer"
|
|
57
55
|
|
|
58
|
-
|
|
56
|
+
float?: "must be a float"
|
|
59
57
|
|
|
60
|
-
|
|
58
|
+
decimal?: "must be a decimal"
|
|
61
59
|
|
|
62
|
-
|
|
60
|
+
date?: "must be a date"
|
|
63
61
|
|
|
64
|
-
|
|
62
|
+
date_time?: "must be a date time"
|
|
65
63
|
|
|
66
|
-
|
|
64
|
+
time?: "must be a time"
|
|
67
65
|
|
|
68
|
-
|
|
66
|
+
key?: "is missing"
|
|
69
67
|
|
|
70
|
-
|
|
68
|
+
attr?: "is missing"
|
|
71
69
|
|
|
72
|
-
|
|
70
|
+
lt?: "must be less than %{num}"
|
|
73
71
|
|
|
74
|
-
|
|
72
|
+
lteq?: "must be less than or equal to %{num}"
|
|
75
73
|
|
|
76
|
-
|
|
74
|
+
max_size?: "size cannot be greater than %{num}"
|
|
77
75
|
|
|
78
|
-
|
|
76
|
+
min_size?: "size cannot be less than %{num}"
|
|
79
77
|
|
|
80
|
-
|
|
81
|
-
arg:
|
|
82
|
-
default: "size must be %{size}"
|
|
83
|
-
range: "size must be within %{size_left} - %{size_right}"
|
|
78
|
+
nil?: "cannot be defined"
|
|
84
79
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
80
|
+
str?: "must be a string"
|
|
81
|
+
|
|
82
|
+
type?: "must be %{type}"
|
|
83
|
+
|
|
84
|
+
size?:
|
|
85
|
+
arg:
|
|
86
|
+
default: "size must be %{size}"
|
|
87
|
+
range: "size must be within %{size_left} - %{size_right}"
|
|
88
|
+
|
|
89
|
+
value:
|
|
90
|
+
string:
|
|
91
|
+
arg:
|
|
92
|
+
default: "length must be %{size}"
|
|
93
|
+
range: "length must be within %{size_left} - %{size_right}"
|
|
94
|
+
|
|
95
|
+
not:
|
|
96
|
+
empty?: "cannot be empty"
|
data/lib/dry/schema/compiler.rb
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'dry/logic/rule_compiler'
|
|
4
|
+
require 'dry/schema/namespaced_rule'
|
|
2
5
|
require 'dry/schema/predicate_registry'
|
|
3
6
|
|
|
4
7
|
module Dry
|
|
@@ -16,6 +19,27 @@ module Dry
|
|
|
16
19
|
super
|
|
17
20
|
end
|
|
18
21
|
|
|
22
|
+
# @api private
|
|
23
|
+
def visit_and(node)
|
|
24
|
+
super.with(hints: false)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Build a special rule that will produce namespaced failures
|
|
28
|
+
#
|
|
29
|
+
# This is needed for schemas that are namespaced and they are
|
|
30
|
+
# used as nested schemas
|
|
31
|
+
#
|
|
32
|
+
# @param [Array] node
|
|
33
|
+
# @param [Hash] opts
|
|
34
|
+
#
|
|
35
|
+
# @return [NamespacedRule]
|
|
36
|
+
#
|
|
37
|
+
# @api private
|
|
38
|
+
def visit_namespace(node, opts = EMPTY_HASH)
|
|
39
|
+
namespace, rest = node
|
|
40
|
+
NamespacedRule.new(namespace, visit(rest))
|
|
41
|
+
end
|
|
42
|
+
|
|
19
43
|
# Return true if a given predicate is supported by this compiler
|
|
20
44
|
#
|
|
21
45
|
# @param [Symbol] predicate
|
data/lib/dry/schema/config.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'dry/equalizer'
|
|
2
4
|
require 'dry/configurable'
|
|
3
5
|
|
|
6
|
+
require 'dry/schema/constants'
|
|
4
7
|
require 'dry/schema/predicate_registry'
|
|
5
8
|
|
|
6
9
|
module Dry
|
|
@@ -10,42 +13,35 @@ module Dry
|
|
|
10
13
|
# @see DSL#configure
|
|
11
14
|
#
|
|
12
15
|
# @api public
|
|
13
|
-
class Config
|
|
14
|
-
|
|
16
|
+
class Config
|
|
17
|
+
include Dry::Configurable
|
|
18
|
+
include Dry::Equalizer(:predicates, :messages)
|
|
15
19
|
|
|
16
|
-
setting
|
|
17
|
-
setting :messages, :yaml
|
|
18
|
-
setting :messages_file
|
|
19
|
-
setting :namespace
|
|
20
|
-
setting :rules, {}
|
|
20
|
+
setting(:predicates, Schema::PredicateRegistry.new)
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
setting(:messages) do
|
|
23
|
+
setting(:backend, :yaml)
|
|
24
|
+
setting(:namespace)
|
|
25
|
+
setting(:load_paths, Set[DEFAULT_MESSAGES_PATH], &:dup)
|
|
26
|
+
setting(:top_namespace, DEFAULT_MESSAGES_ROOT)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
#
|
|
29
|
+
# Return configured predicate registry
|
|
30
30
|
#
|
|
31
|
-
# @return [
|
|
31
|
+
# @return [Schema::PredicateRegistry]
|
|
32
32
|
#
|
|
33
|
-
# @api
|
|
34
|
-
def
|
|
35
|
-
|
|
33
|
+
# @api public
|
|
34
|
+
def predicates
|
|
35
|
+
config.predicates
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
# This method is used by `DSL#configure`
|
|
38
|
+
# Return configuration for message backend
|
|
41
39
|
#
|
|
42
|
-
# @return [Config]
|
|
40
|
+
# @return [Dry::Configurable::Config]
|
|
43
41
|
#
|
|
44
|
-
# @api
|
|
45
|
-
def
|
|
46
|
-
|
|
47
|
-
values.freeze
|
|
48
|
-
freeze
|
|
42
|
+
# @api public
|
|
43
|
+
def messages
|
|
44
|
+
config.messages
|
|
49
45
|
end
|
|
50
46
|
end
|
|
51
47
|
end
|
data/lib/dry/schema/constants.rb
CHANGED
|
@@ -1,13 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'pathname'
|
|
1
4
|
require 'dry/core/constants'
|
|
2
5
|
|
|
3
6
|
module Dry
|
|
7
|
+
# Common constants used across the library
|
|
8
|
+
#
|
|
9
|
+
# @api public
|
|
4
10
|
module Schema
|
|
5
11
|
include Core::Constants
|
|
6
12
|
|
|
13
|
+
LIST_SEPARATOR = ', '
|
|
14
|
+
QUESTION_MARK = '?'
|
|
15
|
+
DOT = '.'
|
|
16
|
+
|
|
17
|
+
# Path to the default set of localized messages bundled within the gem
|
|
18
|
+
DEFAULT_MESSAGES_PATH = Pathname(__dir__).join('../../../config/errors.yml').realpath.freeze
|
|
19
|
+
|
|
20
|
+
# Default namespace used for localized messages in YAML files
|
|
21
|
+
DEFAULT_MESSAGES_ROOT = 'dry_schema'
|
|
22
|
+
|
|
23
|
+
# An error raised when DSL is used in an incorrect way
|
|
7
24
|
InvalidSchemaError = Class.new(StandardError)
|
|
8
|
-
MissingMessageError = Class.new(StandardError)
|
|
9
25
|
|
|
10
|
-
|
|
11
|
-
|
|
26
|
+
# An error raised when a localized message cannot be found
|
|
27
|
+
MissingMessageError = Class.new(StandardError) do
|
|
28
|
+
# @api private
|
|
29
|
+
def initialize(path)
|
|
30
|
+
*rest, rule = path
|
|
31
|
+
super(<<~STR)
|
|
32
|
+
Message template for #{rule.inspect} under #{rest.join(DOT).inspect} was not found
|
|
33
|
+
STR
|
|
34
|
+
end
|
|
35
|
+
end
|
|
12
36
|
end
|
|
13
37
|
end
|