Class: Dry::Validation::Schema
- Inherits:
-
Object
- Object
- Dry::Validation::Schema
show all
- Extended by:
- Configurable
- Defined in:
- lib/dry/validation/schema.rb,
lib/dry/validation/schema/dsl.rb,
lib/dry/validation/schema/key.rb,
lib/dry/validation/schema/attr.rb,
lib/dry/validation/schema/rule.rb,
lib/dry/validation/schema/check.rb,
lib/dry/validation/schema/value.rb
Direct Known Subclasses
Form
Defined Under Namespace
Classes: Attr, Check, DSL, Form, Key, Rule, Value
Constant Summary
collapse
- NOOP_INPUT_PROCESSOR =
-> input { input }
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(rules, options) ⇒ Schema
Returns a new instance of Schema.
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/dry/validation/schema.rb', line 147
def initialize(rules, options)
@rule_compiler = SchemaCompiler.new(self)
@error_compiler = options.fetch(:error_compiler)
@hint_compiler = options.fetch(:hint_compiler)
@predicates = options.fetch(:predicates)
@input_processor = options.fetch(:input_processor, NOOP_INPUT_PROCESSOR)
initialize_options(options)
initialize_rules(rules)
initialize_checks(options.fetch(:checks, []))
freeze
end
|
Instance Attribute Details
#checks ⇒ Object
Returns the value of attribute checks.
133
134
135
|
# File 'lib/dry/validation/schema.rb', line 133
def checks
@checks
end
|
#error_compiler ⇒ Object
Returns the value of attribute error_compiler.
141
142
143
|
# File 'lib/dry/validation/schema.rb', line 141
def error_compiler
@error_compiler
end
|
#hint_compiler ⇒ Object
Returns the value of attribute hint_compiler.
143
144
145
|
# File 'lib/dry/validation/schema.rb', line 143
def hint_compiler
@hint_compiler
end
|
Returns the value of attribute input_processor.
137
138
139
|
# File 'lib/dry/validation/schema.rb', line 137
def input_processor
@input_processor
end
|
#options ⇒ Object
Returns the value of attribute options.
145
146
147
|
# File 'lib/dry/validation/schema.rb', line 145
def options
@options
end
|
#predicates ⇒ Object
Returns the value of attribute predicates.
135
136
137
|
# File 'lib/dry/validation/schema.rb', line 135
def predicates
@predicates
end
|
#rule_compiler ⇒ Object
Returns the value of attribute rule_compiler.
139
140
141
|
# File 'lib/dry/validation/schema.rb', line 139
def rule_compiler
@rule_compiler
end
|
#rules ⇒ Object
Returns the value of attribute rules.
131
132
133
|
# File 'lib/dry/validation/schema.rb', line 131
def rules
@rules
end
|
Class Method Details
.default_messages ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/dry/validation/schema.rb', line 83
def self.default_messages
case config.messages
when :yaml then Messages.default
when :i18n then Messages::I18n.new
else
raise "+#{config.messages}+ is not a valid messages identifier"
end
end
|
.default_options ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/dry/validation/schema.rb', line 123
def self.default_options
{ predicates: predicates,
error_compiler: error_compiler,
hint_compiler: hint_compiler,
input_processor: input_processor,
checks: config.checks }
end
|
.error_compiler ⇒ Object
92
93
94
|
# File 'lib/dry/validation/schema.rb', line 92
def self.error_compiler
@error_compiler ||= ErrorCompiler.new(messages)
end
|
.hint_compiler ⇒ Object
96
97
98
|
# File 'lib/dry/validation/schema.rb', line 96
def self.hint_compiler
@hint_compiler ||= HintCompiler.new(messages, rules: rule_ast)
end
|
.inherited(klass) ⇒ Object
39
40
41
42
|
# File 'lib/dry/validation/schema.rb', line 39
def self.inherited(klass)
super
klass.setting :options, {}
end
|
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/dry/validation/schema.rb', line 100
def self.input_processor
@input_processor ||=
begin
if input_processor_compiler
input_processor_compiler.(rule_ast)
else
NOOP_INPUT_PROCESSOR
end
end
end
|
111
112
113
|
# File 'lib/dry/validation/schema.rb', line 111
def self.input_processor_ast(type)
config.input_processor_map.fetch(type).schema_ast(rule_ast)
end
|
115
116
117
|
# File 'lib/dry/validation/schema.rb', line 115
def self.input_processor_compiler
@input_processor_comp ||= config.input_processor_map[config.input_processor]
end
|
.messages ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/dry/validation/schema.rb', line 69
def self.messages
default = default_messages
if config.messages_file && config.namespace
default.merge(config.messages_file).namespaced(config.namespace)
elsif config.messages_file
default.merge(config.messages_file)
elsif config.namespace
default.namespaced(config.namespace)
else
default
end
end
|
.new(rules = config.rules, **options) ⇒ Object
44
45
46
|
# File 'lib/dry/validation/schema.rb', line 44
def self.new(rules = config.rules, **options)
super(rules, default_options.merge(options))
end
|
.option(name, default = nil) ⇒ Object
48
49
50
51
|
# File 'lib/dry/validation/schema.rb', line 48
def self.option(name, default = nil)
attr_reader(*name)
options.update(name => default)
end
|
.options ⇒ Object
65
66
67
|
# File 'lib/dry/validation/schema.rb', line 65
def self.options
config.options
end
|
.predicates ⇒ Object
61
62
63
|
# File 'lib/dry/validation/schema.rb', line 61
def self.predicates
config.predicates
end
|
.rule_ast ⇒ Object
119
120
121
|
# File 'lib/dry/validation/schema.rb', line 119
def self.rule_ast
@rule_ast ||= config.rules.flat_map(&:rules).map(&:to_ast)
end
|
.rules ⇒ Object
57
58
59
|
# File 'lib/dry/validation/schema.rb', line 57
def self.rules
config.rules
end
|
.to_ast ⇒ Object
53
54
55
|
# File 'lib/dry/validation/schema.rb', line 53
def self.to_ast
[:schema, self]
end
|
Instance Method Details
#[](name) ⇒ Object
170
171
172
173
174
175
176
177
178
|
# File 'lib/dry/validation/schema.rb', line 170
def [](name)
if predicates.key?(name)
predicates[name]
elsif respond_to?(name)
Logic::Predicate.new(name, &method(name))
else
raise ArgumentError, "+#{name}+ is not a valid predicate name"
end
end
|
#call(input) ⇒ Object
165
166
167
168
|
# File 'lib/dry/validation/schema.rb', line 165
def call(input)
processed_input = input_processor[input]
Result.new(processed_input, apply(processed_input), error_compiler, hint_compiler)
end
|
#with(new_options) ⇒ Object
161
162
163
|
# File 'lib/dry/validation/schema.rb', line 161
def with(new_options)
self.class.new(self.class.rules, options.merge(new_options))
end
|