Module: ValidateMyRoutes::Validate::Macros
- Included in:
- ValidationRule
- Defined in:
- lib/validate_my_routes/validate/mixins/macros.rb
Overview
Helper functions to provide a DSL for creating validation rules
Instance Method Summary collapse
-
#description(&block) ⇒ Object
Customize validation rule description.
-
#failure_code(&block) ⇒ Object
Customize http status code of the failure.
-
#failure_message(&block) ⇒ Object
Customize message returned when validation fails for all parameters.
-
#failure_message_when_negated(&block) ⇒ Object
Customize message returned when opposite rule validation fails (not of type A).
-
#validate(&block) ⇒ Object
Define all parameters validation.
Instance Method Details
#description(&block) ⇒ Object
Customize validation rule description
description do
'this is my custom validation rule description'
end
19 20 21 |
# File 'lib/validate_my_routes/validate/mixins/macros.rb', line 19 def description(&block) define_method(:description, &block) end |
#failure_code(&block) ⇒ Object
Customize http status code of the failure
failure_code do |in_path|
in_path ? 404 : 400
end
46 47 48 |
# File 'lib/validate_my_routes/validate/mixins/macros.rb', line 46 def failure_code(&block) define_method(:failure_code, &block) end |
#failure_message(&block) ⇒ Object
Customize message returned when validation fails for all parameters
failure_message do |params|
"oh no! validation failed for #{params}"
end
28 29 30 |
# File 'lib/validate_my_routes/validate/mixins/macros.rb', line 28 def (&block) define_method(:failure_message, &block) end |
#failure_message_when_negated(&block) ⇒ Object
Customize message returned when opposite rule validation fails (not of type A)
failure_message_when_negated do |params|
"oh no! validation failed for #{params}, but it was not expected"
end
37 38 39 |
# File 'lib/validate_my_routes/validate/mixins/macros.rb', line 37 def (&block) define_method(:failure_message_when_negated, &block) end |
#validate(&block) ⇒ Object
Define all parameters validation
validate do |params|
params.key? 'some_parameter_name'
end
10 11 12 |
# File 'lib/validate_my_routes/validate/mixins/macros.rb', line 10 def validate(&block) define_method(:validate, &block) end |