Module: Hanami::Validations::ClassMethods
- Defined in:
- lib/hanami/validations.rb
Overview
Validations DSL
Instance Method Summary collapse
-
#messages(type) ⇒ Object
Define the type of engine for error messages.
-
#messages_path(path) ⇒ Object
Define the path where to find translation file.
-
#namespace(name = nil) ⇒ Object
Namespace for error messages.
-
#predicate(name, message: 'is invalid', &blk) ⇒ Object
Define an inline predicate.
-
#predicates(mod) ⇒ Object
Assign a set of shared predicates wrapped in a module.
-
#validations(&blk) ⇒ Object
Define validation rules from the given block.
Instance Method Details
#messages(type) ⇒ Object
Define the type of engine for error messages.
Accepted values are ‘:yaml` (default), `:i18n`.
191 192 193 |
# File 'lib/hanami/validations.rb', line 191 def (type) self. = type end |
#messages_path(path) ⇒ Object
Define the path where to find translation file
209 210 211 |
# File 'lib/hanami/validations.rb', line 209 def (path) self. = path end |
#namespace(name = nil) ⇒ Object
Namespace for error messages.
236 237 238 239 240 241 242 |
# File 'lib/hanami/validations.rb', line 236 def namespace(name = nil) if name.nil? Namespace.new(_namespace, self) else self._namespace = name.to_s end end |
#predicate(name, message: 'is invalid', &blk) ⇒ Object
Define an inline predicate
140 141 142 |
# File 'lib/hanami/validations.rb', line 140 def predicate(name, message: 'is invalid', &blk) _predicates << InlinePredicate.new(name, , &blk) end |
#predicates(mod) ⇒ Object
Assign a set of shared predicates wrapped in a module
171 172 173 |
# File 'lib/hanami/validations.rb', line 171 def predicates(mod) self._predicates_module = mod end |
#validations(&blk) ⇒ Object
Define validation rules from the given block.
rubocop:disable Metrics/AbcSize
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hanami/validations.rb', line 82 def validations(&blk) schema_predicates = _predicates_module || __predicates base = _build(predicates: schema_predicates, &_base_rules) schema = _build(predicates: schema_predicates, rules: base.rules, &blk) schema.configure(&_schema_config) schema.configure(&_schema_predicates) schema.extend() unless _predicates.empty? self.schema = schema.new end |