Module: Veto::Validator

Defined in:
lib/veto/validator.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
	base.extend ClassMethods
end

Instance Method Details

#clear_errorsObject

Sets errors to nil.



122
123
124
# File 'lib/veto/validator.rb', line 122

def clear_errors
	@errors = nil
end

#entityObject

Returns validating entity instance

Returns:

  • (Object)


110
111
112
# File 'lib/veto/validator.rb', line 110

def entity
	@entity
end

#errorsVeto::Errors

Returns:



117
118
119
# File 'lib/veto/validator.rb', line 117

def errors
	@errors ||= ::Veto::Errors.new
end

#initialize(entity) ⇒ Object

Initializes validator

Parameters:

  • entity (Object)

    the entity instance to validate.



104
105
106
# File 'lib/veto/validator.rb', line 104

def initialize entity
	@entity = entity
end

#valid?Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/veto/validator.rb', line 129

def valid?
	execute
	errors.empty?
end

#validate!Object

Raises exception if entity is invalid

Examples:

person = Person.new
validator = PersonValidator.new(person)
validator.validate! # => Veto::InvalidEntity, ["first name is not present", "..."]    

Raises:



142
143
144
# File 'lib/veto/validator.rb', line 142

def validate!
	raise(::Veto::InvalidEntity, errors.full_messages) unless valid?
end