module Jennifer::Validations::Macros

Direct including types

Defined in:

jennifer/validations/macros.cr

Macro Summary

Macro Detail

macro validates_absence(field, if if_value = nil) #

Validates that the specified attribute is absent.

class Article < Jennifer::Model::Base
  mapping( # ...
)

  validates_absence :title
end

[View source]
macro validates_acceptance(field, accept = nil, if if_value = nil) #

Encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement).

This check is performed only if field is not nil.

class User < Jennifer::Model::Base
  mapping( # ...
)

  property terms_of_service = false
  property eula : String?

  validates_acceptance :terms_of_service
  validates_acceptance :eula, accept: %w(true accept yes)
end

[View source]
macro validates_confirmation(field, case_sensitive = true, if if_value = nil) #

Encapsulates the pattern of wanting to validate a password or email address field with a confirmation.

class User < Jennifer::Model::Base
  mapping(
    # ...
    email: String?,
    address: String?
  )

  property email_confirmation : String?, address_confirmation : String?

  validates_confirmation :email
  validates_confirmation :address, case_insensitive: true
end

[View source]
macro validates_exclusion(field, in in_value, allow_blank = false, if if_value = nil) #

Validates that the value of the specified attribute is not in the given enumerable object.

class Country < Jennifer::Base::Model
  mapping(
    # ...
    code: String
  )

  validates_exclusion :code, in: %w(AA DD)
end

[View source]
macro validates_format(field, value, allow_blank = false, if if_value = nil) #

Validates whether the value of the specified attribute field is of the correct form by matching it against the regular expression value.

class Contact < Jennifer::Model::Base
  mapping(
    # ...
    street: String
  )

  validates_format :street, /st\.|street/i
end

[View source]
macro validates_inclusion(field, in in_value, allow_blank = false, if if_value = nil) #

Validate whether the value of the specified attribute is included in the given enumerable object.

class User < Jennifer::Base::Model
  mapping(
    # ...
    country_code: String
  )

  validates_inclusion :code, in: Country::KNOWN_COUNTRIES
end

[View source]
macro validates_length(field, if if_value = nil, **options) #

Validates that the specified attribute matches the length restrictions supplied.

Only one option can be used at a time:

  • minimum
  • maximum
  • is
  • in
class User < Jennifer::Model::Base
  mapping( # ...
)

  validates_length :name, minimum: 2
  validates_length :login, in: 4..16
  validates_length :uid, is: 16
end

[View source]
macro validates_numericality(field, if if_value = nil, **options) #

Validates whether the value of the specified attribute satisfies given comparison condition.

Configuration options:

  • greater_than
  • greater_than_or_equal_to
  • equal_to
  • less_than
  • less_than_or_equal_to
  • odd
  • even
class Player < Jennifer::Model::Base
  mapping(
    # ...
    health: Float64,
  )

  validates_numericality :health, greater_than: 0
end

[View source]
macro validates_presence(field, if if_value = nil) #

Validates that the specified attributes are not blank.

class User < Jennifer::Model::Base
  mapping(
    # ...
    email: String?
  )

  validates_presence :email
end

[View source]
macro validates_uniqueness(*fields, allow_blank allow_blank_value = false, if if_value = nil) #

Validates whether the value of the specified attributes are unique across the system.

Because this check is performed outside the database there is still a chance that duplicate values will be inserted in two parallel transactions. To guarantee against this you should create a unique index on the field.

class Country < Jennifer::Model::Base
  mapping(
    # ...
    code: String
  )

  validate_uniqueness :code
end

[View source]
macro validates_with(klass, *args, if if_value = nil, **options) #

Passes the record off to an instance of the class specified and allows them to add errors based on more complex conditions.

class EnnValidator < Jennifer::Validations::Validator
  def validate(record : Passport)
    record.errors.add(:enn, "Invalid enn") if record.enn!.size < 4
  end
end

class Passport < Jennifer::Model::Base
  mapping(
    enn: {type: String, primary: true}
  )

  validates_with EnnValidator
end

[View source]
macro validates_with_method(*names, if if_value = nil) #

Adds a validation method to the class.

class User < Jennifer::Model::Base
  # ...

  validates_with_method :thirteen

  def thirteen
    errors.add(:id, "Can't be 13") if id == 13
  end
end

[View source]