Class: Bluepine::Validator
Overview
A validator (can validate any kind of Attribute).
Constant Summary
Constants included
from Resolvable
Resolvable::ResolverRequired
Attributes::Visitor::MethodNotFound
Instance Method Summary
collapse
-
#initialize(resolver = nil) ⇒ Validator
constructor
A new instance of Validator.
-
#normalize_attribute(attribute, value, options = {}) ⇒ Object
Overrides to make it accepts 3 arguments.
-
#visit(attribute, value, options = {}) ⇒ Result
(also: #validate)
Overrides to make it normalizes value before validating attribute.
-
#visit_array(attribute, value, options = {}) ⇒ Object
-
#visit_attribute(attribute, value, options = {}) ⇒ Object
-
#visit_boolean(attribute, value, options = {}) ⇒ Object
-
#visit_float(attribute, value, options = {}) ⇒ Object
-
#visit_integer(attribute, value, options = {}) ⇒ Object
-
#visit_number(attribute, value, options = {}) ⇒ Object
-
#visit_object(attribute, value, options = {}) ⇒ Object
-
#visit_schema(attribute, value, options = {}) ⇒ Object
-
#visit_string(attribute, value, options = {}) ⇒ Object
Methods included from Functions
#compose, #compose_result, #curry, #result
Methods included from Resolvable
#resolver
Constructor Details
#initialize(resolver = nil) ⇒ Validator
Returns a new instance of Validator.
29
30
31
|
# File 'lib/bluepine/validator.rb', line 29
def initialize(resolver = nil)
@resolver = resolver
end
|
Instance Method Details
#normalize_attribute(attribute, value, options = {}) ⇒ Object
Overrides to make it accepts 3 arguments
34
35
36
|
# File 'lib/bluepine/validator.rb', line 34
def normalize_attribute(attribute, value, options = {})
super(attribute, options)
end
|
#visit(attribute, value, options = {}) ⇒ Result
Also known as:
validate
Overrides to make it normalizes value before validating attribute
41
42
43
44
45
46
|
# File 'lib/bluepine/validator.rb', line 41
def visit(attribute, value, options = {})
method, attribute = find_method!(attribute, value, options)
value = attribute.normalize(attribute.value(value))
send(method, attribute, value, options)
end
|
#visit_array(attribute, value, options = {}) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/bluepine/validator.rb', line 90
def visit_array(attribute, value, options = {})
compose(
curry(:is_valid?, :array),
curry(:iterate, value || [], lambda { |(item, v), i|
next result(item) unless attribute.of
visit(attribute.of, item, options)
})
).(value)
end
|
#visit_attribute(attribute, value, options = {}) ⇒ Object
51
52
53
|
# File 'lib/bluepine/validator.rb', line 51
def visit_attribute(attribute, value, options = {})
run(attribute, options, value)
end
|
#visit_boolean(attribute, value, options = {}) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/bluepine/validator.rb', line 62
def visit_boolean(attribute, value, options = {})
compose(
curry(:is_valid?, :boolean),
curry(:run, attribute, options),
).(value)
end
|
#visit_float(attribute, value, options = {}) ⇒ Object
83
84
85
86
87
88
|
# File 'lib/bluepine/validator.rb', line 83
def visit_float(attribute, value, options = {})
compose(
curry(:is_valid?, :float),
curry(:run, attribute, options)
).(value)
end
|
#visit_integer(attribute, value, options = {}) ⇒ Object
76
77
78
79
80
81
|
# File 'lib/bluepine/validator.rb', line 76
def visit_integer(attribute, value, options = {})
compose(
curry(:is_valid?, :integer),
curry(:run, attribute, options)
).(value)
end
|
#visit_number(attribute, value, options = {}) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/bluepine/validator.rb', line 69
def visit_number(attribute, value, options = {})
compose(
curry(:is_valid?, :number),
curry(:run, attribute, options)
).(value)
end
|
#visit_object(attribute, value, options = {}) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/bluepine/validator.rb', line 104
def visit_object(attribute, value, options = {})
compose(
curry(:is_valid?, :object),
curry(:iterate, attribute.attributes, lambda { |(key, attr), i|
data = get(value, attr.method)
options[:context] = value
visit(attr, data, options)
})
).(value)
end
|
#visit_schema(attribute, value, options = {}) ⇒ Object
118
119
120
121
122
|
# File 'lib/bluepine/validator.rb', line 118
def visit_schema(attribute, value, options = {})
attr = resolver.schema(attribute.of)
visit_object(attr, value, options)
end
|
#visit_string(attribute, value, options = {}) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/bluepine/validator.rb', line 55
def visit_string(attribute, value, options = {})
compose(
curry(:is_valid?, :string),
curry(:run, attribute, options)
).(value)
end
|