Module: Appfuel::Validation
- Extended by:
- Application::FeatureHelper
- 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.
- .load_schema(key) ⇒ Object
Methods included from Application::FeatureHelper
extract_feature_name, feature_initialized?, initialize_feature
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.
57 58 59 60 61 62 63 |
# File 'lib/appfuel/validation.rb', line 57 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
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/appfuel/validation.rb', line 69 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'
28 29 30 31 32 33 |
# File 'lib/appfuel/validation.rb', line 28 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 |
.load_schema(key) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/appfuel/validation.rb', line 35 def load_schema(key) feature = extract_feature_name(key) container = Appfuel.app_container key, _basename = build_validator_key(key) unless feature_initialized?(feature) initialize_feature(feature) end unless container.key?(key) fail "Could not load validator key #{key}" end container[key] end |