module ActiveModel::Validations::ClassMethods

Public Instance Methods

validates_cnpj(*attr_names)
validates_cnpj_format_of(*attr_names) click to toggle source

Validates whether or not the specified CNPJ is valid.

class User < ActiveRecord::Base
  validates_cnpj_format_of :document
end
# File lib/validators/validates_cnpj_format_of.rb, line 27
def validates_cnpj_format_of(*attr_names)
  Validators.require_dependency! "cpf_cnpj"
  require "cnpj"
  validates_with CnpjValidator, _merge_attributes(attr_names)
end
Also aliased as: validates_cnpj
validates_cpf(*attr_names)
validates_cpf_format_of(*attr_names) click to toggle source

Validates whether or not the specified CPF is valid.

class User < ActiveRecord::Base
  validates_cpf_format_of :document
end
# File lib/validators/validates_cpf_format_of.rb, line 27
def validates_cpf_format_of(*attr_names)
  Validators.require_dependency! "cpf_cnpj"
  require "cpf"
  validates_with CpfValidator, _merge_attributes(attr_names)
end
Also aliased as: validates_cpf
validates_datetime(*attr_names) click to toggle source

Validates whether or not the specified e-mail address is valid.

class User < ActiveRecord::Base
  validates_datetime :birth
end

Other usages:

validates_datetime :starts_at, after: 2.years.ago
validates_datetime :starts_at, before: 2.years.ago
validates_datetime :starts_at, before: :today
validates_datetime :starts_at, before: :now
validates_datetime :starts_at, before: :ends_at
validates_datetime :ends_at, after: :starts_at
# File lib/validators/validates_datetime.rb, line 102
def validates_datetime(*attr_names)
  validates_with DatetimeValidator, _merge_attributes(attr_names)
end
validates_email(*attr_names)
validates_email_format_of(*attr_names) click to toggle source

Validates whether or not the specified e-mail address is valid.

class User < ActiveRecord::Base
  validates_email_format_of :email
end
# File lib/validators/validates_email_format_of.rb, line 79
def validates_email_format_of(*attr_names)
  Validators.require_dependency! "root_domain"
  Validators.require_dependency! "email_data"
  validates_with EmailValidator, _merge_attributes(attr_names)
end
Also aliased as: validates_email
validates_hostname(*attr_names)
validates_hostname_format_of(*attr_names) click to toggle source

Validates whether or not the specified URL is valid.

class User < ActiveRecord::Base
  validates_hostname_format_of :site

  # Validates against a list of valid TLD.
  validates_hostname_format_of :site, tld: true
end
# File lib/validators/validates_hostname_format_of.rb, line 31
def validates_hostname_format_of(*attr_names)
  validates_with HostnameValidator, _merge_attributes(attr_names)
end
Also aliased as: validates_hostname
validates_ip_address(*attr_names) click to toggle source

Validates whether or not the specified URL is valid.

validates_ip_address :ip  #=> accepts both v4 and v6
validates_ip_address :ip, only: :v4
validates_ip_address :ip, only: :v6
# File lib/validators/validates_ip_address.rb, line 50
def validates_ip_address(*attr_names)
  validates_with IpAddressValidator, _merge_attributes(attr_names)
end
validates_ownership_of(*attr_names) click to toggle source

Validates whether the owner of the specified attribute is the same from the current object.

class Task < ActiveRecord::Base
  belongs_to :user
  belongs_to :category

  validates_ownership_of :category, with: :user
end

user = User.find(1)
another_user = User.find(2)

user_category = user.categories.first
another_user_category = another_user.categories.first

task = user.tasks.create(category: user_category)
task.valid?
#=> true

task = user.tasks.create(category: another_user_category)
task.valid?
#=> false
# File lib/validators/validates_ownership_of.rb, line 52
def validates_ownership_of(*attr_names)
  validates_with OwnershipValidator, _merge_attributes(attr_names)
end
validates_ssh_private_key(*attr_names) click to toggle source

Validates whether or not the specified CNPJ is valid.

class User < ActiveRecord::Base
  validates_ssh_private_key :key
end
# File lib/validators/validates_ssh_private_key.rb, line 58
def validates_ssh_private_key(*attr_names)
  Validators.require_dependency! "sshkey"
  validates_with SshPrivateKeyValidator, _merge_attributes(attr_names)
end
validates_ssh_public_key(*attr_names) click to toggle source

Validates whether or not the specified CNPJ is valid.

class User < ActiveRecord::Base
  validates_ssh_public_key :key
end
# File lib/validators/validates_ssh_public_key.rb, line 27
def validates_ssh_public_key(*attr_names)
  Validators.require_dependency! "sshkey"
  validates_with SshPublicKeyValidator, _merge_attributes(attr_names)
end
validates_subdomain(*attr_names) click to toggle source

Validates whether or not the specified host label is valid. The ‘in: array` can have strings and patterns. A pattern is everything that starts with `/` and will be parsed as a regular expression.

Notice that subdomains will be normalized; it’ll be downcased and have its underscores and hyphens stripped before validating.

class User < ActiveRecord::Base
  # Validates format and rejects reserved subdomains.
  validates_subdomain :subdomain

  # Validates against a custom list.
  validates_subdomain :subdomain, in: %w[www]

  # Rejects reserved domains validation.
  validates_subdomain :subdomain, reserved: false
end
# File lib/validators/validates_subdomain.rb, line 63
def validates_subdomain(*attr_names)
  options = _merge_attributes(attr_names).merge(error_name: :subdomain)
  validates_with SubdomainValidator, options
end
validates_url(*attr_names)
validates_url_format_of(*attr_names) click to toggle source

Validates whether or not the specified URL is valid.

class User < ActiveRecord::Base
  validates_url_format_of :site

  # Validates against a list of valid TLD.
  validates_url_format_of :site, tld: true
end
# File lib/validators/validates_url_format_of.rb, line 51
def validates_url_format_of(*attr_names)
  validates_with UrlValidator, _merge_attributes(attr_names)
end
Also aliased as: validates_url
validates_username(*attr_names) click to toggle source
# File lib/validators/validates_username.rb, line 9
def validates_username(*attr_names)
  options = _merge_attributes(attr_names).merge(error_name: :username)
  validates_with UsernameValidator, options
end