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

Instance Method Details

#default_validator_nameString

When no name for a validator is given then the default name will be the name of the concrete handler class

Returns:

  • (String)


88
89
90
# File 'lib/appfuel/handler/validator_dsl.rb', line 88

def default_validator_name
  self.to_s.split('::').last.underscore
end

#load_validator(key, opts = {}) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/appfuel/handler/validator_dsl.rb', line 175

def load_validator(key, opts = {})
  unless opts[:no_context] == true
    key = convert_to_container_key(key)
  end

  container = app_container
  feature_name = extract_feature_name(key)
  unless feature_initialized?(feature_name)
    initialize_feature(feature_name)
  end

  unless container.key?(key)
    fail "Could not locate validator with (#{key})"
  end
  container[key]
end

#load_validatorsObject



131
132
133
134
135
136
137
138
# File 'lib/appfuel/handler/validator_dsl.rb', line 131

def load_validators
  list = validators
  return list unless list.empty?
  validator_keys.each do |key|
    @validators << load_validator(key, no_context: true)
  end
  validators
end

#resolve_inputs(inputs = {}) ⇒ Object

Validate all inputs using the list of validators that were assigned using the dsl methods.

Parameters:

  • inputs (Hash) (defaults to: {})

Returns:

  • Appfuel::Response



145
146
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
# File 'lib/appfuel/handler/validator_dsl.rb', line 145

def resolve_inputs(inputs = {})
  return ok(inputs) if skip_validation?
  return ok({}) unless validators?

  inputs.deep_symbolize_keys!
  response = nil
  has_failed = false
  load_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]



127
128
129
# File 'lib/appfuel/handler/validator_dsl.rb', line 127

def skip_validation!
  @skip_validation = true
end

#skip_validation?Boolean

Used when resolving inputs to determine if we should apply any validation

return [Bool]

Returns:

  • (Boolean)


119
120
121
# File 'lib/appfuel/handler/validator_dsl.rb', line 119

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

Parameters:

  • key (String) (defaults to: nil)

    name of the validator

  • opts (Hash) (defaults to: {})

    options for creating a validator

  • fail_fast (Hash)

    a customizable set of options

Returns:

  • (Nil)


99
100
101
102
103
104
105
106
107
108
# File 'lib/appfuel/handler/validator_dsl.rb', line 99

def validator(key = nil, opts = {}, &block)
  key  = default_validator_name if key.nil?
  unless block_given?
    validator_keys << convert_to_container_key(key)
    return
  end

  validators << Appfuel::Validation.build_validator(key, opts, &block)
  nil
end

#validator_keysObject



71
72
73
# File 'lib/appfuel/handler/validator_dsl.rb', line 71

def validator_keys
  @validator_keys ||= []
end

#validators(*args) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/appfuel/handler/validator_dsl.rb', line 75

def validators(*args)
  @validators ||= []
  return @validators if args.empty?

  args.each do |arg|
    validator_keys << convert_to_container_key(arg)
  end
end

#validators?Boolean

return [Bool]

Returns:

  • (Boolean)


111
112
113
# File 'lib/appfuel/handler/validator_dsl.rb', line 111

def validators?
  !(validators.empty? && validator_keys.empty?)
end