Module: Appfuel::Handler::ValidatorDsl
- Included in:
- Base
- Defined in:
- lib/appfuel/handler/validator_dsl.rb
Overview
1) single block validator. A basic validator that is only used by
that particular interactor
validator('foo', fail_fast: true) do
end
2) single validator from the features validators located in the app
container under the key "features.<feature-name>.validators.<validator-name>"
validator 'foo'
3) single validator from the global validators located in the app
container under the key "global.validators.<validator-name>"
validator 'global.foo'
4) muliple validators, all are located in the feature namespace
validators 'foo', 'bar', 'baz'
5) multiple validators, some in features other in global
validators 'foo', 'global.bar', 'baz'
6) a pipe is a closure that lives before a given validator in order
to manipulate the inputs to fit the next validator. it does not validate
validator_pipe do |inputs, data|
end
7) using a pipe when using muliple validators
validators 'foo', 'pipe.bar', 'base'
8) using global pipe in multiple validator declaration
validators 'foo', 'global-pipe.bar', 'base'
9) validator_schema is used to use Dry::Validations with out our block
processing
validation_schema 'foo', Dry::Validation.Schama do
end
validation_schema Dry::Validation.Schema do
end
validation_schema 'foo', fail_fast: true, Dry::Validation.Schema do
end
validator
name: to identify it in errors and as a key to register it with container
fail_fast: when true runner will bail on first error
pipe?: false
code: validator schema to run
call: run the validator schema
validator-pipe
name: to identify the pipe in errors and register it with container
code: lambda to run
call: run the lamda
Instance Method Summary collapse
-
#default_validator_name ⇒ String
When no name for a validator is given then the default name will be the name of the concrete handler class.
-
#load_validator(key, opts = {}) ⇒ Object
load a validator with the given key from the app container.
-
#resolve_inputs(inputs = {}) ⇒ Object
Validate all inputs using the list of validators that were assigned using the dsl methods.
-
#skip_validation! ⇒ Object
Dsl method to allow a handler to tell the system not to validate anything and use the raw inputs.
-
#skip_validation? ⇒ Boolean
Used when resolving inputs to determine if we should apply any validation.
-
#validator(key = nil, opts = {}, &block) ⇒ Nil
Converts a given block to a validator or load the validator from either global or feature validators.
- #validators(*args) ⇒ Object
-
#validators? ⇒ Boolean
return [Bool].
Instance Method Details
#default_validator_name ⇒ String
When no name for a validator is given then the default name will be the name of the concrete handler class
84 85 86 |
# File 'lib/appfuel/handler/validator_dsl.rb', line 84 def default_validator_name self.to_s.split('::').last.underscore end |
#load_validator(key, opts = {}) ⇒ Object
the key is encode and will be decoded first @see ValidatorDsl#convert_to_container_key for details
load a validator with the given key from the app container.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/appfuel/handler/validator_dsl.rb', line 109 def load_validator(key, opts = {}) fail "validator must have a key" if key.nil? container_key = convert_to_container_key(key) container = Appfuel.app_container unless container.key?(container_key) fail "Could not locate validator with (#{container_key})" end container[container_key] end |
#resolve_inputs(inputs = {}) ⇒ Object
Validate all inputs using the list of validators that were assigned using the dsl methods.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/appfuel/handler/validator_dsl.rb', line 147 def resolve_inputs(inputs = {}) return ok(inputs) if skip_validation? return ok({}) unless validators? response = nil has_failed = false validators.each do |validator| if validator.pipe? result = handle_validator_pipe(validator, inputs) inputs = result unless result == false next end result = validator.call(inputs) if result.success? && !has_failed response = handle_successful_inputs(result, response) next end return error(result.errors(full: true)) if validator.fail_fast? has_failed = true response = handle_error_inputs(result, response) end fail "multi validators can not be only Procs" if response.nil? response end |
#skip_validation! ⇒ Object
Dsl method to allow a handler to tell the system not to validate anything and use the raw inputs
return [Bool]
138 139 140 |
# File 'lib/appfuel/handler/validator_dsl.rb', line 138 def skip_validation! @skip_validation = true end |
#skip_validation? ⇒ Boolean
Used when resolving inputs to determine if we should apply any validation
return [Bool]
130 131 132 |
# File 'lib/appfuel/handler/validator_dsl.rb', line 130 def skip_validation? @skip_validation == true end |
#validator(key = nil, opts = {}, &block) ⇒ Nil
Converts a given block to a validator or load the validator from either global or feature validators
95 96 97 98 99 |
# File 'lib/appfuel/handler/validator_dsl.rb', line 95 def validator(key = nil, opts = {}, &block) key = default_validator_name if key.nil? validators << build_validator(key, opts, &block) nil end |
#validators(*args) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/appfuel/handler/validator_dsl.rb', line 71 def validators(*args) @validators ||= [] return @validators if args.empty? args.each do |arg| @validators << load_validator(arg) end end |
#validators? ⇒ Boolean
return [Bool]
122 123 124 |
# File 'lib/appfuel/handler/validator_dsl.rb', line 122 def validators? !validators.empty? end |