module Fend::Plugins::ValidationOptions

Instead of calling ValidationHelpers::ParamMethods separately, you can use `validation_options` plugin in order to specify all validations as options and pass them to `Param#validate` method.

plugin :validation_options

validate do |i|
  i.params(:email) do |email|
    email.validate(presence: true, type: String, format: EMAIL_REGEX)
  end
end

## Custom error messages

Custom error messages can be defined with `:message` option:

email.validate(presence: { message: "cannot be blank"})

## Mandatory arguments

For ValidationHelpers::ParamMethods that expect mandatory arguments, there are predefined option keys that you can use. To see them all check MANDATORY_ARG_KEYS constant.

email.validate type: { of: String, message: "is not a string" }, format: { with: EMAIL_REGEX }
account_type.validate inclusion: { in: %w(admin, moderator) }

You can also use the DEFAULT_ARG_KEY (`:value`) if you find it hard to remember the specific ones.

email.validate type: { value: String }, format: { value: EMAIL_REGEX }

## Allowing nil and blank values

You can skip validation if param value is `nil` or blank by passing `:allow_nil` or `:allow_blank` options:

# will skip type validation if name.value.nil?
name.validate(type: String, allow_nil: true)

# will skip type validation if email.blank?
email.validate(type: String, allow_blank: true)

To see what values are considered as blank, check ValueHelpers::ParamMethods#blank?.

`validation_options` supports ExternalValidation plugin:

plugin :external_validation

# ...

email.validate(with: CustomEmailValidator)

Constants

ARRAY_ARG_METHODS
DEFAULT_ARG_KEY
MANDATORY_ARG_KEYS

List of keys to use when specifying mandatory validation arguments

NO_ARG_METHODS

Public Class Methods

load_dependencies(validation, *args, &block) click to toggle source

Depends on ValidationHelpers plugin

# File lib/fend/plugins/validation_options.rb, line 85
def self.load_dependencies(validation, *args, &block)
  validation.plugin(:validation_helpers)
end