Sha256: eac41412c771bcdc7da9eb831c512bfd21c418e32c937cfe25e56ef23ee62661

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

module Dry
  module Validation
    class Schema
      module Definition
        def schema(name, &block)
          schema = Class.new(superclass)
          schema.key(name, &block)
          schemas << schema
          self
        end

        def key(name, &block)
          Key.new(name, rules).key?(&block)
        end

        def optional(name, &block)
          Key.new(name, rules).optional(&block)
        end

        def rule(name, **options, &block)
          if options.any?
            predicate, rule_names = options.to_a.first
            identifier = { name => rule_names }

            groups << [:group, [identifier, [:predicate, predicate]]]
          else
            if block
              checks << Schema::Rule.new(name, [:check, [name, yield.to_ary]])
            else
              rule_by_name(name).to_check
            end
          end
        end

        def confirmation(name)
          identifier = :"#{name}_confirmation"

          key(name, &:filled?)
          key(identifier, &:filled?)

          rule(identifier, eql?: [name, identifier])
        end

        private

        def rule_by_name(name)
          rules.detect { |rule| rule.name == name }
        end
      end
    end
  end
end

require 'dry/validation/schema/rule'
require 'dry/validation/schema/value'
require 'dry/validation/schema/key'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-validation-0.5.0 lib/dry/validation/schema/definition.rb
dry-validation-0.4.1 lib/dry/validation/schema/definition.rb
dry-validation-0.4.0 lib/dry/validation/schema/definition.rb