Class: Respect::Validator

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

Overview

A schema validator.

Validator are an extensible way to validate certain properties of a schema. There are many validators available in this library (see all the sub-classes of this class).

You can attach a validator to any schema through its options parameters when initializing it. Any schema including the HasConstraints module will execute them when its Schema#validate method is called.

Example:

# Will call GreaterThanValidator.new(42).validate(-1)
IntegerSchema.define(greater_than: 42).validate?(-1)     #=> true

The validator API is experimental so it is not recommended to write your own.

Direct Known Subclasses

DivisibleByValidator, EqualToValidator, FormatValidator, GreaterThanOrEqualToValidator, GreaterThanValidator, InValidator, LessThanOrEqualToValidator, LessThanValidator, MatchValidator, MaxLengthValidator, MinLengthValidator, MultipleOfValidator

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) constraint_name

Turn this validator class name into a constraint name.



23
24
25
# File 'lib/respect/validator.rb', line 23

def constraint_name
  self.name.sub(/^.*::/, '').sub(/Validator$/, '').underscore
end

Instance Method Details

- (Object) to_h(format = :org3)

Convert this validator to a Hash using the given format.



33
34
35
36
37
38
39
40
# File 'lib/respect/validator.rb', line 33

def to_h(format = :org3)
  case format
  when :org3
    to_h_org3
  else
    raise ArgumentError, "unknown format '#{format}'"
  end
end

- (Object) validate(value)



28
29
30
# File 'lib/respect/validator.rb', line 28

def validate(value)
  true
end