module ROM::Model::Validator::ClassMethods

Public Instance Methods

>>(other) click to toggle source

Compose a validation with a command

The command will be called if validations succeed

@example

validated_command = (UserValidator >> users.create)
validated_command.call(attributes)

@return [Composite]

@api public

# File lib/rom/model/validator.rb, line 182
def >>(other)
  Composite.new(self, other)
end
call(attributes) click to toggle source

Trigger validation for specific attributes

@param [Model::Attributes] attributes The attributes for validation

@raises [ValidationError]

@return [Model::Attributes]

# File lib/rom/model/validator.rb, line 166
def call(attributes)
  validator = new(attributes)
  validator.call
end
embedded(name, options = {}, &block) click to toggle source

Specify an embedded validator for nested structures

@example

class UserValidator
  include ROM::Model::Validator

  set_model_name 'User'

  embedded :address do
    validates :city, :street, :zipcode, presence: true
  end

  emebdded :tasks do
    validates :title, presence: true
  end
end

validator = UserAttributes.new(address: {}, tasks: {})

validator.valid? # false
validator.errors[:address].first # errors for address
validator.errors[:tasks] # errors for tasks

@api public

# File lib/rom/model/validator.rb, line 210
def embedded(name, options = {}, &block)
  presence = options.fetch(:presence, true)

  validator_class = Class.new {
    include ROM::Model::Validator
  }

  validator_class.set_model_name(name.to_s.classify)
  validator_class.class_eval(&block)

  embedded_validators[name] = validator_class

  validates name, presence: true if presence

  validate do
    value = attributes[name]

    if value.present?
      Array([value]).flatten.each do |object|
        validator = validator_class.new(object, root, attributes)
        validator.validate

        if validator.errors.any?
          errors.add(name, validator.errors)
        else
          errors.add(name, [])
        end
      end
    end
  end
end
relation(name = nil) click to toggle source

Set relation name for a validator

This is needed for validators that require database access

@example

class UserValidator
  include ROM::Model::Validator

  relation :users

  validates :name, uniqueness: true
end

@return [Symbol]

@api public

# File lib/rom/model/validator.rb, line 145
def relation(name = nil)
  @relation = name if name
  @relation
end
set_model_name(name) click to toggle source

@api private

# File lib/rom/model/validator.rb, line 151
        def set_model_name(name)
          class_eval <<-RUBY
            def self.model_name
              @model_name ||= ActiveModel::Name.new(self, nil, #{name.inspect})
            end
          RUBY
        end