Class: Bluepine::Validator

Inherits:
Attributes::Visitor show all
Includes:
Functions, Resolvable
Defined in:
lib/bluepine/validator.rb

Overview

A validator (can validate any kind of Attribute).

Examples:

Validate simple Attribute

attribute = Attributes.create(:string, :username, required: true)
payload   = { username: nil }

validator = Validator.new(resolver)
validator.validate(attribute, payload) # => { username: ["can't be blank"] }

Validate compound type (e.g. Object, Array).

attribute = Attributes.create(:object, :user) do
  string :username, min: 5
  array  :pets, of: :string
end
payload   = { username: "john", pets: ["jay", 1] }

validator.validate(attribute, payload) # =>
# {
#   username: ["is too short (minimum is 5 characters)"]
#   pets: { 1: ["is not string"] }
# }

Constant Summary

Constants included from Resolvable

Resolvable::ResolverRequired

Constants inherited from Attributes::Visitor

Attributes::Visitor::MethodNotFound

Instance Method Summary collapse

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

Returns:



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|

      # when of: is not specified, item can be any type
      next result(item) unless attribute.of

      # validate each item
      visit(attribute.of, item, options)
    })
  ).(value)
end

#visit_attribute(attribute, value, options = {}) ⇒ Object

catch-all



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|

      # validate each attribute
      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