Module: Appfuel::Validation
- Defined in:
- lib/appfuel/validation.rb,
lib/appfuel/validation/validator.rb,
lib/appfuel/validation/validator_pipe.rb
Defined Under Namespace
Classes: Validator, ValidatorPipe
Class Method Summary collapse
-
.build_validator(name, opts = {}, &block) ⇒ Object
Turns the block of code given into a Dry::Validation schema or formi which is then used to create our validator.
-
.create_dry_validator(type, &block) ⇒ Object
Factory method create Dry::Validation::Schema or Dry::Validation::Schema::Form objects.
-
.define(name, opts = {}, &block) ⇒ Object
Dsl used create and register validators in the app container.
Class Method Details
.build_validator(name, opts = {}, &block) ⇒ Object
Turns the block of code given into a Dry::Validation schema or formi which is then used to create our validator.
41 42 43 44 45 46 47 |
# File 'lib/appfuel/validation.rb', line 41 def build_validator(name, opts = {}, &block) fail_fast = opts[:fail_fast] == true ? true : false schema_type = (opts.fetch(:type) { 'form' }).to_s.downcase schema = create_dry_validator(schema_type, &block) Validator.new(name, schema, fail_fast: fail_fast) end |
.create_dry_validator(type, &block) ⇒ Object
Factory method create Dry::Validation::Schema or Dry::Validation::Schema::Form objects
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/appfuel/validation.rb', line 53 def create_dry_validator(type, &block) unless ['form', 'schema'].include?(type) fail "validator type must 'form' or 'schema' (#{type}) given" end fail "block is required to build a validator" unless block_given? method = type.capitalize Dry::Validation.send(method, &block) end |
.define(name, opts = {}, &block) ⇒ Object
Dsl used create and register validators in the app container. The key needs to be the fully qualified feature or global.
end
this will add a validator in 'global.validators.foo'
end
this will add a validator in 'features.auth.validators.foo'
26 27 28 29 30 31 |
# File 'lib/appfuel/validation.rb', line 26 def define(name, opts = {}, &block) key, basename = build_validator_key(name) container = Appfuel.app_container validator = build_validator(basename, opts, &block) container.register(key, validator) end |