Class: Dry::Validation::Schema
- Inherits:
-
Object
- Object
- Dry::Validation::Schema
show all
- Extended by:
- Configurable, Definition
- Defined in:
- lib/dry/validation/schema.rb,
lib/dry/validation/schema/key.rb,
lib/dry/validation/schema/rule.rb,
lib/dry/validation/schema/value.rb,
lib/dry/validation/schema/definition.rb
Direct Known Subclasses
Form
Defined Under Namespace
Modules: Definition
Classes: Form, Key, Result, Rule, Value
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Definition
confirmation, key, optional, rule, schema
Constructor Details
#initialize(error_compiler = self.class.error_compiler, hint_compiler = self.class.hint_compiler) ⇒ Schema
Returns a new instance of Schema.
76
77
78
79
80
81
82
83
84
|
# File 'lib/dry/validation/schema.rb', line 76
def initialize(error_compiler = self.class.error_compiler, hint_compiler = self.class.hint_compiler)
compiler = RuleCompiler.new(self)
@rules = compiler.(self.class.rules.map(&:to_ary))
@checks = self.class.checks
@groups = compiler.(self.class.groups.map(&:to_ary))
@schemas = self.class.schemas.map(&:new)
@error_compiler = error_compiler
@hint_compiler = hint_compiler
end
|
Instance Attribute Details
#checks ⇒ Object
Returns the value of attribute checks.
70
71
72
|
# File 'lib/dry/validation/schema.rb', line 70
def checks
@checks
end
|
#error_compiler ⇒ Object
Returns the value of attribute error_compiler.
72
73
74
|
# File 'lib/dry/validation/schema.rb', line 72
def error_compiler
@error_compiler
end
|
#groups ⇒ Object
Returns the value of attribute groups.
70
71
72
|
# File 'lib/dry/validation/schema.rb', line 70
def groups
@groups
end
|
#hint_compiler ⇒ Object
Returns the value of attribute hint_compiler.
74
75
76
|
# File 'lib/dry/validation/schema.rb', line 74
def hint_compiler
@hint_compiler
end
|
#rules ⇒ Object
Returns the value of attribute rules.
70
71
72
|
# File 'lib/dry/validation/schema.rb', line 70
def rules
@rules
end
|
#schemas ⇒ Object
Returns the value of attribute schemas.
70
71
72
|
# File 'lib/dry/validation/schema.rb', line 70
def schemas
@schemas
end
|
Class Method Details
.checks ⇒ Object
66
67
68
|
# File 'lib/dry/validation/schema.rb', line 66
def self.checks
@__checks__ ||= []
end
|
.error_compiler ⇒ Object
26
27
28
|
# File 'lib/dry/validation/schema.rb', line 26
def self.error_compiler
ErrorCompiler.new(messages)
end
|
.groups ⇒ Object
62
63
64
|
# File 'lib/dry/validation/schema.rb', line 62
def self.groups
@__groups__ ||= []
end
|
.hint_compiler ⇒ Object
30
31
32
|
# File 'lib/dry/validation/schema.rb', line 30
def self.hint_compiler
HintCompiler.new(messages, rules: rules.map(&:to_ary))
end
|
.messages ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/dry/validation/schema.rb', line 34
def self.messages
default =
case config.messages
when :yaml then Messages.default
when :i18n then Messages::I18n.new
else
raise RuntimeError, "+#{config.messages}+ is not a valid messages identifier"
end
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
|
.predicates ⇒ Object
22
23
24
|
# File 'lib/dry/validation/schema.rb', line 22
def self.predicates
config.predicates
end
|
.rules ⇒ Object
54
55
56
|
# File 'lib/dry/validation/schema.rb', line 54
def self.rules
@__rules__ ||= []
end
|
.schemas ⇒ Object
58
59
60
|
# File 'lib/dry/validation/schema.rb', line 58
def self.schemas
@__schemas__ ||= []
end
|
Instance Method Details
#[](name) ⇒ Object
112
113
114
115
116
117
118
119
120
|
# File 'lib/dry/validation/schema.rb', line 112
def [](name)
if predicates.key?(name)
predicates[name]
elsif respond_to?(name)
Predicate.new(name, &method(name))
else
raise ArgumentError, "+#{name}+ is not a valid predicate name"
end
end
|
#call(input) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/dry/validation/schema.rb', line 86
def call(input)
result = Validation::Result.new(rules.map { |rule| rule.(input) })
schemas.each do |schema|
result.merge!(schema.(input).result)
end
if checks.size > 0
compiled_checks = RuleCompiler.new(result.to_h).(checks)
compiled_checks.each do |rule|
result << rule.()
end
end
groups.each do |group|
result.with_values(group.rules) do |values|
result << group.(*values)
end
end
errors = Error::Set.new(result.failures.map { |failure| Error.new(failure) })
Schema::Result.new(input, result, errors, error_compiler, hint_compiler)
end
|
#predicates ⇒ Object
122
123
124
|
# File 'lib/dry/validation/schema.rb', line 122
def predicates
self.class.predicates
end
|