Class: React::Validator

Inherits:
Object show all
Defined in:
lib/react/validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props_wrapper = Class.new(Component::PropsWrapper)) ⇒ Validator

Returns a new instance of Validator.



7
8
9
# File 'lib/react/validator.rb', line 7

def initialize(props_wrapper = Class.new(Component::PropsWrapper))
  @props_wrapper = props_wrapper
end

Instance Attribute Details

#errors=(value) ⇒ Object

Sets the attribute errors

Parameters:

  • value

    the value to set the attribute errors to.



3
4
5
# File 'lib/react/validator.rb', line 3

def errors=(value)
  @errors = value
end

Class Method Details

.build(&block) ⇒ Object



11
12
13
# File 'lib/react/validator.rb', line 11

def self.build(&block)
  self.new.build(&block)
end

Instance Method Details

#all_other_params(name) ⇒ Object



30
31
32
33
# File 'lib/react/validator.rb', line 30

def all_other_params(name)
  @allow_undefined_props = true
  props_wrapper.define_all_others(name) { |props| props.reject { |name, value| rules[name] } }
end

#build(&block) ⇒ Object



15
16
17
18
# File 'lib/react/validator.rb', line 15

def build(&block)
  instance_eval(&block)
  self
end

#default_propsObject



47
48
49
50
51
# File 'lib/react/validator.rb', line 47

def default_props
  rules
    .select {|key, value| value.keys.include?("default") }
    .inject({}) {|memo, (k,v)| memo[k] = v[:default]; memo}
end

#optional(name, options = {}) ⇒ Object



25
26
27
28
# File 'lib/react/validator.rb', line 25

def optional(name, options = {})
  options[:required] = false
  define_rule(name, options)
end

#requires(name, options = {}) ⇒ Object



20
21
22
23
# File 'lib/react/validator.rb', line 20

def requires(name, options = {})
  options[:required] = true
  define_rule(name, options)
end

#validate(props) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/react/validator.rb', line 35

def validate(props)
  self.errors = []
  validate_undefined(props) unless allow_undefined_props?
  props = coerce_native_hash_values(defined_props(props))
  validate_required(props)
  props.each do |name, value|
    validate_types(name, value)
    validate_allowed(name, value)
  end
  errors
end