Sha256: e043dea9d1fce48c953e3e560161222801e80517804d8dd2da1a1655bada61b5

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require 'dry-equalizer'
require 'dry-configurable'
require 'dry-container'

require 'dry/validation/schema'
require 'dry/validation/schema/form'
require 'dry/validation/schema/json'

module Dry
  module Validation
    MissingMessageError = Class.new(StandardError)
    InvalidSchemaError = Class.new(StandardError)

    def self.messages_paths
      Messages::Abstract.config.paths
    end

    def self.Schema(base = Schema, **options, &block)
      schema_class = Class.new(base.is_a?(Schema) ? base.class : base)

      dsl_opts = {
        schema_class: schema_class,
        registry: schema_class.registry,
        parent: options[:parent]
      }

      dsl_ext = schema_class.config.dsl_extensions

      dsl = Schema::Value.new(dsl_opts)
      dsl_ext.__send__(:extend_object, dsl) if dsl_ext
      dsl.predicates(options[:predicates]) if options.key?(:predicates)
      dsl.instance_exec(&block) if block

      klass = dsl.schema_class

      base_rules = klass.config.rules + (options.fetch(:rules, []) + dsl.rules)

      rules =
        if klass.config.input
          input_rule = dsl.__send__(klass.config.input)
          [input_rule.and(dsl.with(rules: base_rules))]
        else
          base_rules
        end

      klass.configure do |config|
        config.rules = rules
        config.checks = config.checks + dsl.checks
        config.path = dsl.path
        config.type_map = klass.build_type_map(dsl.type_map) if config.type_specs
      end

      if options[:build] == false
        klass
      else
        klass.new
      end
    end

    def self.Form(options = {}, &block)
      Validation.Schema(Schema::Form, options, &block)
    end

    def self.JSON(options = {}, &block)
      Validation.Schema(Schema::JSON, options, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-validation-0.8.0 lib/dry/validation.rb