Class: Alchemy::IngredientValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- Alchemy::IngredientValidator
- Defined in:
- app/models/alchemy/ingredient_validator.rb
Overview
Ingredient Validations:
Ingredient validations can be set inside the config/elements.yml
file.
Supported validations are:
-
presence
-
uniqueness
-
format
*) format needs to come with a regex or a predefined matcher string as its value.
There are already predefined format matchers listed in the config/alchemy/config.yml
file. It is also possible to add own format matchers there.
Example of format matchers in config/alchemy/config.yml
:
format_matchers:
email: !ruby/regexp '/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/'
url: !ruby/regexp '/\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\z/ix'
ssl: !ruby/regexp '/https:\/\/[\S]+/'
Example of an element definition with ingredient validations:
- name: person
ingredients:
- role: name
type: Text
validate: [presence]
- role: email
type: Text
validate: [format: 'email']
- role: homepage
type: Text
validate: [format: !ruby/regexp '^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$']
Example of an element definition with chained validations.
- name: person
ingredients:
- role: name
type: Text
validate: [presence, uniqueness, format: 'name']
Instance Method Summary collapse
Instance Method Details
#validate(ingredient) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/alchemy/ingredient_validator.rb', line 49 def validate(ingredient) @ingredient = ingredient validations.each do |validation| if validation.respond_to?(:keys) validation.map do |key, value| send(:"validate_#{key}", value) end else send(:"validate_#{validation}") end end end |