Module: Dry::Validation::TypeSpecs
- Included in:
- Schema
- Defined in:
- lib/dry/validation/type_specs.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build_array_type(spec, category) ⇒ Object
- #build_hash_type(spec = type_map) ⇒ Object
- #build_sum_type(spec, category) ⇒ Object
- #build_type_map(type_specs, category = config.input_processor) ⇒ Object
- #lookup_type(name, category) ⇒ Object
- #resolve_spec(spec, category) ⇒ Object
Class Method Details
.extended(klass) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/dry/validation/type_specs.rb', line 4 def self.extended(klass) super klass.class_eval do setting :input_processor, :noop setting :hash_type, :weak setting :type_map, {} end end |
Instance Method Details
#build_array_type(spec, category) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/dry/validation/type_specs.rb', line 28 def build_array_type(spec, category) member_schema = build_type_map(spec, category) member_type = lookup_type("hash", category) .public_send(config.hash_type, member_schema) lookup_type("array", category).member(member_type) end |
#build_hash_type(spec = type_map) ⇒ Object
23 24 25 26 |
# File 'lib/dry/validation/type_specs.rb', line 23 def build_hash_type(spec = type_map) lookup_type("hash", config.input_processor) .public_send(config.hash_type, spec) end |
#build_sum_type(spec, category) ⇒ Object
36 37 38 39 40 |
# File 'lib/dry/validation/type_specs.rb', line 36 def build_sum_type(spec, category) spec .map { |id| id.is_a?(Symbol) ? lookup_type(id, category) : id } .reduce(:|) end |
#build_type_map(type_specs, category = config.input_processor) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/dry/validation/type_specs.rb', line 13 def build_type_map(type_specs, category = config.input_processor) if type_specs.is_a?(Array) build_array_type(type_specs[0], category) else type_specs.reduce({}) { |res, (name, spec)| res.update(name => resolve_spec(spec, category)) } end end |
#lookup_type(name, category) ⇒ Object
64 65 66 67 |
# File 'lib/dry/validation/type_specs.rb', line 64 def lookup_type(name, category) id = "#{category}.#{name}" Types.type_keys.include?(id) ? Types[id] : Types[name.to_s] end |
#resolve_spec(spec, category) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dry/validation/type_specs.rb', line 42 def resolve_spec(spec, category) case spec when Symbol lookup_type(spec, category) when Hash build_hash_type(spec) when Array if spec.size == 1 if spec[0].is_a?(Hash) build_array_type(spec[0], category) else array_member = lookup_type(spec[0], category) lookup_type("array", category).member(array_member) end else build_sum_type(spec, category) end else spec end end |