dry-validation 0.13.3 → 1.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (196) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -25
  3. data/LICENSE +1 -1
  4. data/README.md +9 -4
  5. data/lib/dry-validation.rb +2 -0
  6. data/lib/dry/validation.rb +8 -35
  7. data/lib/dry/validation/constants.rb +12 -0
  8. data/lib/dry/validation/contract.rb +132 -0
  9. data/lib/dry/validation/contract/class_interface.rb +114 -0
  10. data/lib/dry/validation/evaluator.rb +121 -0
  11. data/lib/dry/validation/extensions/monads.rb +23 -7
  12. data/lib/dry/validation/messages.rb +50 -6
  13. data/lib/dry/validation/result.rb +109 -45
  14. data/lib/dry/validation/rule.rb +37 -0
  15. data/lib/dry/validation/version.rb +3 -1
  16. metadata +36 -337
  17. data/.codeclimate.yml +0 -17
  18. data/.gitignore +0 -9
  19. data/.rspec +0 -3
  20. data/.travis.yml +0 -29
  21. data/CONTRIBUTING.md +0 -31
  22. data/Gemfile +0 -25
  23. data/Rakefile +0 -22
  24. data/benchmarks/benchmark_form_invalid.rb +0 -64
  25. data/benchmarks/benchmark_form_valid.rb +0 -64
  26. data/benchmarks/benchmark_schema_invalid_huge.rb +0 -52
  27. data/benchmarks/profile_schema_call_invalid.rb +0 -20
  28. data/benchmarks/profile_schema_call_valid.rb +0 -20
  29. data/benchmarks/profile_schema_definition.rb +0 -14
  30. data/benchmarks/profile_schema_huge_invalid.rb +0 -30
  31. data/benchmarks/profile_schema_messages_invalid.rb +0 -20
  32. data/benchmarks/suite.rb +0 -5
  33. data/config/errors.yml +0 -89
  34. data/dry-validation.gemspec +0 -28
  35. data/examples/basic.rb +0 -15
  36. data/examples/each.rb +0 -14
  37. data/examples/json.rb +0 -12
  38. data/examples/multiple.rb +0 -27
  39. data/examples/nested.rb +0 -22
  40. data/examples/params.rb +0 -11
  41. data/lib/dry/validation/compat/form.rb +0 -67
  42. data/lib/dry/validation/deprecations.rb +0 -24
  43. data/lib/dry/validation/executor.rb +0 -91
  44. data/lib/dry/validation/extensions.rb +0 -7
  45. data/lib/dry/validation/extensions/struct.rb +0 -32
  46. data/lib/dry/validation/input_processor_compiler.rb +0 -137
  47. data/lib/dry/validation/input_processor_compiler/json.rb +0 -45
  48. data/lib/dry/validation/input_processor_compiler/params.rb +0 -49
  49. data/lib/dry/validation/input_processor_compiler/sanitizer.rb +0 -47
  50. data/lib/dry/validation/message.rb +0 -98
  51. data/lib/dry/validation/message_compiler.rb +0 -188
  52. data/lib/dry/validation/message_compiler/visitor_opts.rb +0 -37
  53. data/lib/dry/validation/message_set.rb +0 -122
  54. data/lib/dry/validation/messages/abstract.rb +0 -119
  55. data/lib/dry/validation/messages/i18n.rb +0 -47
  56. data/lib/dry/validation/messages/namespaced.rb +0 -39
  57. data/lib/dry/validation/messages/yaml.rb +0 -61
  58. data/lib/dry/validation/predicate_registry.rb +0 -115
  59. data/lib/dry/validation/predicates.rb +0 -19
  60. data/lib/dry/validation/schema.rb +0 -126
  61. data/lib/dry/validation/schema/check.rb +0 -37
  62. data/lib/dry/validation/schema/class_interface.rb +0 -190
  63. data/lib/dry/validation/schema/deprecated.rb +0 -30
  64. data/lib/dry/validation/schema/dsl.rb +0 -118
  65. data/lib/dry/validation/schema/form.rb +0 -9
  66. data/lib/dry/validation/schema/json.rb +0 -21
  67. data/lib/dry/validation/schema/key.rb +0 -71
  68. data/lib/dry/validation/schema/params.rb +0 -22
  69. data/lib/dry/validation/schema/rule.rb +0 -202
  70. data/lib/dry/validation/schema/value.rb +0 -211
  71. data/lib/dry/validation/schema_compiler.rb +0 -81
  72. data/lib/dry/validation/template.rb +0 -66
  73. data/lib/dry/validation/type_specs.rb +0 -70
  74. data/spec/extensions/monads/result_spec.rb +0 -40
  75. data/spec/extensions/struct/schema_spec.rb +0 -32
  76. data/spec/fixtures/locales/en.yml +0 -8
  77. data/spec/fixtures/locales/pl.yml +0 -22
  78. data/spec/integration/custom_error_messages_spec.rb +0 -54
  79. data/spec/integration/custom_predicates_spec.rb +0 -228
  80. data/spec/integration/hints_spec.rb +0 -170
  81. data/spec/integration/injecting_rules_spec.rb +0 -30
  82. data/spec/integration/json/defining_base_schema_spec.rb +0 -41
  83. data/spec/integration/localized_error_messages_spec.rb +0 -72
  84. data/spec/integration/message_compiler_spec.rb +0 -405
  85. data/spec/integration/messages/i18n_spec.rb +0 -104
  86. data/spec/integration/optional_keys_spec.rb +0 -28
  87. data/spec/integration/params/predicates/array_spec.rb +0 -287
  88. data/spec/integration/params/predicates/empty_spec.rb +0 -263
  89. data/spec/integration/params/predicates/eql_spec.rb +0 -327
  90. data/spec/integration/params/predicates/even_spec.rb +0 -455
  91. data/spec/integration/params/predicates/excluded_from_spec.rb +0 -455
  92. data/spec/integration/params/predicates/excludes_spec.rb +0 -391
  93. data/spec/integration/params/predicates/false_spec.rb +0 -455
  94. data/spec/integration/params/predicates/filled_spec.rb +0 -467
  95. data/spec/integration/params/predicates/format_spec.rb +0 -454
  96. data/spec/integration/params/predicates/gt_spec.rb +0 -519
  97. data/spec/integration/params/predicates/gteq_spec.rb +0 -519
  98. data/spec/integration/params/predicates/included_in_spec.rb +0 -455
  99. data/spec/integration/params/predicates/includes_spec.rb +0 -391
  100. data/spec/integration/params/predicates/key_spec.rb +0 -67
  101. data/spec/integration/params/predicates/lt_spec.rb +0 -519
  102. data/spec/integration/params/predicates/lteq_spec.rb +0 -519
  103. data/spec/integration/params/predicates/max_size_spec.rb +0 -391
  104. data/spec/integration/params/predicates/min_size_spec.rb +0 -391
  105. data/spec/integration/params/predicates/none_spec.rb +0 -265
  106. data/spec/integration/params/predicates/not_eql_spec.rb +0 -327
  107. data/spec/integration/params/predicates/odd_spec.rb +0 -455
  108. data/spec/integration/params/predicates/size/fixed_spec.rb +0 -393
  109. data/spec/integration/params/predicates/size/range_spec.rb +0 -396
  110. data/spec/integration/params/predicates/true_spec.rb +0 -455
  111. data/spec/integration/params/predicates/type_spec.rb +0 -391
  112. data/spec/integration/result_spec.rb +0 -81
  113. data/spec/integration/schema/array_schema_spec.rb +0 -59
  114. data/spec/integration/schema/check_rules_spec.rb +0 -119
  115. data/spec/integration/schema/check_with_nested_el_spec.rb +0 -37
  116. data/spec/integration/schema/check_with_nth_el_spec.rb +0 -25
  117. data/spec/integration/schema/default_settings_spec.rb +0 -11
  118. data/spec/integration/schema/defining_base_schema_spec.rb +0 -41
  119. data/spec/integration/schema/dynamic_predicate_args_spec.rb +0 -43
  120. data/spec/integration/schema/each_with_set_spec.rb +0 -70
  121. data/spec/integration/schema/extending_dsl_spec.rb +0 -27
  122. data/spec/integration/schema/form_spec.rb +0 -236
  123. data/spec/integration/schema/hash_schema_spec.rb +0 -47
  124. data/spec/integration/schema/inheriting_schema_spec.rb +0 -31
  125. data/spec/integration/schema/input_processor_spec.rb +0 -46
  126. data/spec/integration/schema/json/explicit_types_spec.rb +0 -157
  127. data/spec/integration/schema/json_spec.rb +0 -163
  128. data/spec/integration/schema/macros/confirmation_spec.rb +0 -35
  129. data/spec/integration/schema/macros/each_spec.rb +0 -268
  130. data/spec/integration/schema/macros/filled_spec.rb +0 -87
  131. data/spec/integration/schema/macros/input_spec.rb +0 -139
  132. data/spec/integration/schema/macros/maybe_spec.rb +0 -99
  133. data/spec/integration/schema/macros/rule_spec.rb +0 -75
  134. data/spec/integration/schema/macros/value_spec.rb +0 -119
  135. data/spec/integration/schema/macros/when_spec.rb +0 -62
  136. data/spec/integration/schema/nested_schemas_spec.rb +0 -236
  137. data/spec/integration/schema/nested_values_spec.rb +0 -46
  138. data/spec/integration/schema/not_spec.rb +0 -34
  139. data/spec/integration/schema/numbers_spec.rb +0 -19
  140. data/spec/integration/schema/option_with_default_spec.rb +0 -64
  141. data/spec/integration/schema/or_spec.rb +0 -87
  142. data/spec/integration/schema/params/defining_base_schema_spec.rb +0 -41
  143. data/spec/integration/schema/params/explicit_types_spec.rb +0 -195
  144. data/spec/integration/schema/params_spec.rb +0 -234
  145. data/spec/integration/schema/predicate_verification_spec.rb +0 -9
  146. data/spec/integration/schema/predicates/array_spec.rb +0 -295
  147. data/spec/integration/schema/predicates/custom_spec.rb +0 -103
  148. data/spec/integration/schema/predicates/empty_spec.rb +0 -263
  149. data/spec/integration/schema/predicates/eql_spec.rb +0 -327
  150. data/spec/integration/schema/predicates/even_spec.rb +0 -455
  151. data/spec/integration/schema/predicates/excluded_from/array_spec.rb +0 -459
  152. data/spec/integration/schema/predicates/excluded_from/range_spec.rb +0 -459
  153. data/spec/integration/schema/predicates/excludes_spec.rb +0 -391
  154. data/spec/integration/schema/predicates/filled_spec.rb +0 -467
  155. data/spec/integration/schema/predicates/format_spec.rb +0 -455
  156. data/spec/integration/schema/predicates/gt_spec.rb +0 -519
  157. data/spec/integration/schema/predicates/gteq_spec.rb +0 -519
  158. data/spec/integration/schema/predicates/hash_spec.rb +0 -69
  159. data/spec/integration/schema/predicates/included_in/array_spec.rb +0 -459
  160. data/spec/integration/schema/predicates/included_in/range_spec.rb +0 -459
  161. data/spec/integration/schema/predicates/includes_spec.rb +0 -391
  162. data/spec/integration/schema/predicates/key_spec.rb +0 -88
  163. data/spec/integration/schema/predicates/lt_spec.rb +0 -520
  164. data/spec/integration/schema/predicates/lteq_spec.rb +0 -519
  165. data/spec/integration/schema/predicates/max_size_spec.rb +0 -391
  166. data/spec/integration/schema/predicates/min_size_spec.rb +0 -391
  167. data/spec/integration/schema/predicates/none_spec.rb +0 -265
  168. data/spec/integration/schema/predicates/not_eql_spec.rb +0 -391
  169. data/spec/integration/schema/predicates/odd_spec.rb +0 -455
  170. data/spec/integration/schema/predicates/size/fixed_spec.rb +0 -398
  171. data/spec/integration/schema/predicates/size/range_spec.rb +0 -395
  172. data/spec/integration/schema/predicates/type_spec.rb +0 -413
  173. data/spec/integration/schema/reusing_schema_spec.rb +0 -33
  174. data/spec/integration/schema/using_types_spec.rb +0 -135
  175. data/spec/integration/schema/validate_spec.rb +0 -120
  176. data/spec/integration/schema/xor_spec.rb +0 -35
  177. data/spec/integration/schema_builders_spec.rb +0 -17
  178. data/spec/integration/schema_spec.rb +0 -173
  179. data/spec/shared/message_compiler.rb +0 -11
  180. data/spec/shared/predicate_helper.rb +0 -15
  181. data/spec/shared/rule_compiler.rb +0 -8
  182. data/spec/spec_helper.rb +0 -62
  183. data/spec/support/define_struct.rb +0 -25
  184. data/spec/support/matchers.rb +0 -38
  185. data/spec/support/mutant.rb +0 -9
  186. data/spec/support/predicates_integration.rb +0 -7
  187. data/spec/unit/input_processor_compiler/json_spec.rb +0 -283
  188. data/spec/unit/input_processor_compiler/params_spec.rb +0 -328
  189. data/spec/unit/message_compiler/visit_failure_spec.rb +0 -38
  190. data/spec/unit/message_compiler/visit_spec.rb +0 -16
  191. data/spec/unit/message_compiler_spec.rb +0 -7
  192. data/spec/unit/predicate_registry_spec.rb +0 -34
  193. data/spec/unit/schema/key_spec.rb +0 -38
  194. data/spec/unit/schema/rule_spec.rb +0 -42
  195. data/spec/unit/schema/value_spec.rb +0 -131
  196. data/spec/unit/schema_spec.rb +0 -35
@@ -1,9 +0,0 @@
1
- require 'dry/validation/schema'
2
- require 'dry/validation/schema/params'
3
- require 'dry/types/compat/form_types'
4
-
5
- module Dry
6
- module Validation
7
- Schema::Form = Schema::Params
8
- end
9
- end
@@ -1,21 +0,0 @@
1
- require 'dry/validation/schema'
2
-
3
- module Dry
4
- module Validation
5
- class Schema::JSON < Schema
6
- def self.configure(klass = nil, &block)
7
- if klass
8
- klass.configure do |config|
9
- config.input_processor = :json
10
- config.hash_type = :symbolized
11
- end
12
- klass
13
- else
14
- super(&block)
15
- end
16
- end
17
-
18
- configure(self)
19
- end
20
- end
21
- end
@@ -1,71 +0,0 @@
1
- require 'dry/validation/schema/dsl'
2
-
3
- module Dry
4
- module Validation
5
- class Schema
6
- class Key < DSL
7
- attr_reader :parent
8
-
9
- def self.type
10
- :key
11
- end
12
-
13
- def class
14
- Key
15
- end
16
-
17
- def type
18
- self.class.type
19
- end
20
-
21
- def to_ast
22
- [type, [name, super]]
23
- end
24
-
25
- def each(*predicates, &block)
26
- create_rule([type, [name, value.each(*predicates, &block).to_ast]])
27
- end
28
-
29
- def schema(other = nil, &block)
30
- create_rule([type, [name, value.schema(other, &block).to_ast]])
31
- end
32
-
33
- def hash?(&block)
34
- predicate = predicate(:hash?)
35
-
36
- if block
37
- val = value.instance_eval(&block)
38
-
39
- rule = create_rule(predicate)
40
- .and(create_rule([type, [name, val.to_ast]]))
41
-
42
- add_rule(rule)
43
- rule
44
- else
45
- add_rule(create_rule(predicate))
46
- end
47
- end
48
-
49
- def value
50
- Value[name, registry: registry]
51
- end
52
-
53
- private
54
-
55
- def method_missing(meth, *args, &block)
56
- registry.ensure_valid_predicate(meth, args)
57
- predicate = predicate(meth, args)
58
-
59
- if block
60
- val = value.instance_eval(&block)
61
- add_rule(create_rule([:and, [predicate, val.to_ast]]))
62
- else
63
- rule = create_rule([type, [name, predicate]])
64
- add_rule(rule)
65
- rule
66
- end
67
- end
68
- end
69
- end
70
- end
71
- end
@@ -1,22 +0,0 @@
1
- require 'dry/validation/schema'
2
- require 'dry/types/params'
3
-
4
- module Dry
5
- module Validation
6
- class Schema::Params < Schema
7
- def self.configure(klass = nil, &block)
8
- if klass
9
- klass.configure do |config|
10
- config.input_processor = :params
11
- config.hash_type = :symbolized
12
- end
13
- klass
14
- else
15
- super(&block)
16
- end
17
- end
18
-
19
- configure(self)
20
- end
21
- end
22
- end
@@ -1,202 +0,0 @@
1
- require 'dry/validation/deprecations'
2
-
3
- module Dry
4
- module Validation
5
- class Schema
6
- class Rule < BasicObject
7
- include ::Dry::Validation::Deprecations
8
-
9
- INVALID_PREDICATES = {
10
- value: [],
11
- maybe: [:empty?, :none?],
12
- filled: [:empty?, :filled?],
13
- }.freeze
14
-
15
- attr_reader :name, :node, :type, :target, :deps, :options
16
-
17
- def initialize(node, options = {})
18
- @node = node
19
- @type = options.fetch(:type, :and)
20
- @deps = options.fetch(:deps, [])
21
- @name = options.fetch(:name)
22
- @target = options.fetch(:target)
23
- @options = options
24
- end
25
-
26
- def inspect
27
- to_ast.inspect
28
- end
29
- alias_method :to_s, :inspect
30
-
31
- def schema(other = nil, &block)
32
- schema = Schema.create_class(target, other, &block)
33
-
34
- if schema.config.type_specs
35
- target.type_map[name] = schema.type_map
36
- end
37
-
38
- rule = __send__(type, key(:hash?).and(key(schema)))
39
- add_rule(rule)
40
- end
41
-
42
- def schema?
43
- target.schema?
44
- end
45
-
46
- def registry
47
- target.registry
48
- end
49
-
50
- def type_map
51
- target.type_map
52
- end
53
-
54
- def type_map?
55
- target.type_map?
56
- end
57
-
58
- def required(*predicates)
59
- warn 'required is deprecated - use filled instead.'
60
-
61
- filled(*predicates)
62
- end
63
-
64
- def filled(*predicates, &block)
65
- left = ([key(:filled?)] + infer_predicates(predicates, :filled)).reduce(:and)
66
-
67
- rule =
68
- if block
69
- left.and(Key[name, registry: registry].instance_eval(&block))
70
- else
71
- left
72
- end
73
-
74
- add_rule(__send__(type, rule))
75
- end
76
-
77
- def value(*predicates, &block)
78
- if predicates.empty? && !block
79
- ::Kernel.raise ::ArgumentError, "wrong number of arguments (given 0, expected at least 1)"
80
- end
81
-
82
- from_predicates = infer_predicates(predicates, :value).reduce(:and)
83
- from_block = block ? Key[name, registry: registry].instance_eval(&block) : nil
84
-
85
- rule = [from_predicates, from_block].compact.reduce(:and)
86
-
87
- add_rule(__send__(type, rule))
88
- end
89
-
90
- def maybe(*predicates, &block)
91
- left = key(:none?).not
92
-
93
- from_predicates = infer_predicates(predicates, :maybe).reduce(:and)
94
- from_block = block ? Key[name, registry: registry].instance_eval(&block) : nil
95
-
96
- right = [from_predicates, from_block].compact.reduce(:and) || key(:filled?)
97
-
98
- rule = left.then(right)
99
-
100
- add_rule(__send__(type, rule))
101
- end
102
-
103
- def each(*predicates, &block)
104
- rule = target.each(*predicates, &block)
105
- add_rule(__send__(type, new([target.type, [name, rule.to_ast]])))
106
- end
107
-
108
- def add_rule(rule)
109
- target.add_rule(rule)
110
- end
111
-
112
- def rules
113
- target.rules
114
- end
115
-
116
- def checks
117
- target.checks
118
- end
119
-
120
- def rule_ast
121
- rules.size > 0 ? target.rule_ast : [to_ast]
122
- end
123
-
124
- def to_ast
125
- rule_node = name ? [:rule, [name, node]] : node
126
-
127
- if deps.empty?
128
- rule_node
129
- else
130
- [:guard, [deps, rule_node]]
131
- end
132
- end
133
-
134
- def class
135
- Schema::Rule
136
- end
137
-
138
- def not
139
- new([:not, node])
140
- end
141
-
142
- def and(other)
143
- new([:and, [to_ast, other.to_ast]])
144
- end
145
- alias_method :&, :and
146
-
147
- def or(other)
148
- new([:or, [to_ast, other.to_ast]])
149
- end
150
- alias_method :|, :or
151
-
152
- def xor(other)
153
- new([:xor, [to_ast, other.to_ast]])
154
- end
155
- alias_method :^, :xor
156
-
157
- def then(other)
158
- new([:implication, [to_ast, other.to_ast]])
159
- end
160
- alias_method :>, :then
161
-
162
- def infer_predicates(predicates, macro = nil)
163
- predicates.flat_map(&::Kernel.method(:Array)).map do |predicate|
164
- name, *args = ::Kernel.Array(predicate)
165
-
166
- if macro && INVALID_PREDICATES[macro].include?(name)
167
- ::Kernel.raise InvalidSchemaError, "you can't use #{name} predicate with #{macro} macro"
168
- else
169
- key(name, args)
170
- end
171
- end
172
- end
173
-
174
- def with(new_options)
175
- self.class.new(node, options.merge(new_options))
176
- end
177
-
178
- def to_rule
179
- self
180
- end
181
-
182
- private
183
-
184
- def method_missing(meth, *args, &block)
185
- if target.predicate?(meth)
186
- target.__send__(meth, *args, &block)
187
- else
188
- super
189
- end
190
- end
191
-
192
- def key(predicate, args = [])
193
- new(target.node(predicate, *args))
194
- end
195
-
196
- def new(node)
197
- self.class.new(node, options)
198
- end
199
- end
200
- end
201
- end
202
- end
@@ -1,211 +0,0 @@
1
- require 'dry/validation/schema/dsl'
2
-
3
- module Dry
4
- module Validation
5
- class Schema
6
- class Value < DSL
7
- attr_reader :type, :schema_class, :type_map
8
-
9
- def initialize(options = {})
10
- super
11
- @type = options.fetch(:type, :key)
12
- @schema_class = options.fetch(:schema_class, ::Class.new(Schema))
13
- @options = options.merge(type: @type, schema_class: @schema_class)
14
- @type_map = parent && parent.root? ? parent.type_map : {}
15
- end
16
-
17
- def predicates(mod)
18
- @registry = options[:registry] = schema_class.predicates(mod)
19
- end
20
-
21
- def input(*predicates)
22
- schema_class.config.input = predicates
23
- self
24
- end
25
-
26
- def key(name, &block)
27
- warn 'key is deprecated - use required instead.'
28
-
29
- required(name, &block)
30
- end
31
-
32
- def required(name, type_spec = nil, &block)
33
- rule = define(name, Key, &block)
34
-
35
- if type_spec
36
- type_map[name] = type_spec
37
- end
38
-
39
- rule
40
- end
41
-
42
- def schema(other = nil, &block)
43
- @schema = Schema.create_class(self, other, &block)
44
- type_map.update(@schema.type_map)
45
- hash?.and(@schema)
46
- end
47
-
48
- def each(*predicates, &block)
49
- left = array?
50
-
51
- right =
52
- if predicates.size > 0
53
- create_rule([:each, infer_predicates(predicates, new).to_ast])
54
- else
55
- val = Value[
56
- name, registry: registry, schema_class: schema_class.clone
57
- ].instance_eval(&block)
58
-
59
- if val.type_map?
60
- if root?
61
- @type_map = [val.type_map]
62
- else
63
- type_map[name] = [val.type_map]
64
- end
65
- end
66
-
67
- create_rule([:each, val.to_ast])
68
- end
69
-
70
- rule = left.and(right)
71
-
72
- add_rule(rule) if root?
73
-
74
- rule
75
- end
76
-
77
- def when(*predicates, &block)
78
- left = infer_predicates(predicates, Check[path, type: type, registry: registry])
79
- right = Value.new(type: type, registry: registry).instance_eval(&block)
80
-
81
- add_check(left.then(right.to_rule))
82
-
83
- self
84
- end
85
-
86
- def rule(id = nil, **options, &block)
87
- if id
88
- val = Value[id, registry: registry, schema_class: schema_class]
89
- res = val.instance_exec(&block)
90
- else
91
- id, deps = options.to_a.first
92
- val = Value[id, registry: registry, schema_class: schema_class]
93
- res = val.instance_exec(*deps.map { |path| val.value(id, path: path) }, &block)
94
- end
95
-
96
- add_check(val.with(rules: [res.with(name: id, deps: deps || [])]))
97
- end
98
-
99
- def confirmation
100
- conf = :"#{name}_confirmation"
101
-
102
- parent.optional(conf).maybe
103
-
104
- rule(conf => [conf, name]) do |left, right|
105
- left.eql?(right)
106
- end
107
- end
108
-
109
- def value(path, opts = {})
110
- check(name || path, { registry: registry, rules: rules, path: path }.merge(opts))
111
- end
112
-
113
- def check(name, options = {})
114
- Check[name, options.merge(type: type)]
115
- end
116
-
117
- def validate(**opts, &block)
118
- id, *deps = opts.to_a.flatten
119
- name = deps.size > 1 ? id : deps.first
120
- rule = create_rule([:check, [deps, [:custom, [id, block]]]], name).with(deps: deps)
121
- add_check(rule)
122
- end
123
-
124
- def configure(&block)
125
- schema_class.class_eval(&block)
126
- @registry = schema_class.registry
127
- self
128
- end
129
-
130
- def root?
131
- name.nil?
132
- end
133
-
134
- def type_map?
135
- ! type_map.empty?
136
- end
137
-
138
- def schema?
139
- ! @schema.nil?
140
- end
141
-
142
- def class
143
- Value
144
- end
145
-
146
- def new
147
- self.class.new(registry: registry, schema_class: schema_class.clone)
148
- end
149
-
150
- def key?(name)
151
- create_rule(predicate(:key?, name))
152
- end
153
-
154
- def node(input, *args)
155
- if input.is_a?(::Symbol)
156
- registry.ensure_valid_predicate(input, args, schema_class)
157
- [type, [name, predicate(input, args)]]
158
- elsif input.respond_to?(:rule)
159
- [type, [name, [:type, input]]]
160
- elsif input.is_a?(Schema)
161
- [type, [name, schema(input).to_ast]]
162
- else
163
- [type, [name, input.to_ast]]
164
- end
165
- end
166
-
167
- def dyn_arg?(name)
168
- !name.to_s.end_with?('?') && schema_class.instance_methods.include?(name)
169
- end
170
-
171
- def respond_to?(name)
172
- self.class.public_methods.include?(name)
173
- end
174
-
175
- def infer_predicates(predicates, infer_on = self)
176
- predicates.flat_map(&::Kernel.method(:Array)).map { |predicate|
177
- name, *args = ::Kernel.Array(predicate)
178
-
179
- if name.is_a?(Schema)
180
- infer_on.schema(name)
181
- elsif name.respond_to?(:rule)
182
- create_rule(name.rule.to_ast)
183
- else
184
- infer_on.__send__(name, *args)
185
- end
186
- }.reduce(:and)
187
- end
188
-
189
- private
190
-
191
- def method_missing(meth, *args, &block)
192
- return schema_class.instance_method(meth) if dyn_arg?(meth)
193
-
194
- val_rule = create_rule(predicate(meth, args))
195
-
196
- if block
197
- val = new.instance_eval(&block)
198
-
199
- type_map.update(val.type_map) if val.type_map?
200
-
201
- new_rule = create_rule([:and, [val_rule.to_ast, val.to_ast]])
202
-
203
- add_rule(new_rule)
204
- else
205
- val_rule
206
- end
207
- end
208
- end
209
- end
210
- end
211
- end