Class: Ciesta::Validator

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

Overview

Validatior class for form

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidator

Constructor



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

def initialize
  @errors = []
end

Instance Attribute Details

#errorsHash (readonly)

Array with errors

Returns:

  • (Hash)

    the current value of errors



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

def errors
  @errors
end

Instance Method Details

#use(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set schema for validation

Parameters:

  • block (Block)

    Block wich returns the schema



19
20
21
# File 'lib/ciesta/validator.rb', line 19

def use(&block)
  @schema = Dry::Validation.Params(&block)
end

#valid?(attributes) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if schema is valid

Parameters:

  • attributes (Hash<Symbol, any>)

    Attributes to check

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/ciesta/validator.rb', line 29

def valid?(attributes)
  return true if schema.nil?

  @errors = schema.call(attributes).errors
  errors.empty?
end