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(component_class) ⇒ Validator

Returns a new instance of Validator.



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

def initialize(component_class)
  @component_class = component_class
end

Instance Attribute Details

#errors=(value) ⇒ Object

Sets the attribute errors

Parameters:

  • value

    the value to set the attribute errors to.



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

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

#allow_undefined_props=(allow) ⇒ Object



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

def allow_undefined_props=(allow)
  @allow_undefined_props = allow
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



51
52
53
54
55
# File 'lib/react/validator.rb', line 51

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

#undefined_props(props) ⇒ Object



34
35
36
37
# File 'lib/react/validator.rb', line 34

def undefined_props(props)
  self.allow_undefined_props = true
  props.reject { |name, value| rules[name] }
end

#validate(props) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/react/validator.rb', line 39

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