Sha256: 12ce2b76d14dd8d669413ee0174012df09a698a3cd4fbaad662f04d64d888277

Contents?: true

Size: 1.85 KB

Versions: 19

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require "dry/core/extensions"

require "dry/schema/config"
require "dry/schema/constants"
require "dry/schema/dsl"
require "dry/schema/params"
require "dry/schema/json"

module Dry
  # Main interface
  #
  # @api public
  module Schema
    extend Dry::Core::Extensions

    # Configuration
    #
    # @example
    #   Dry::Schema.config.messages.backend = :i18n
    #
    # @return [Config]
    #
    # @api public
    def self.config
      @config ||= Config.new
    end

    # Define a schema
    #
    # @example
    #   Dry::Schema.define do
    #     required(:name).filled(:string)
    #     required(:age).value(:integer, gt?: 0)
    #   end
    #
    # @param [Hash] options
    #
    # @return [Processor]
    #
    # @see DSL.new
    #
    # @api public
    def self.define(**options, &block)
      DSL.new(**options, &block).call
    end

    # Define a schema suitable for HTTP params
    #
    # This schema type uses `Types::Params` for coercion by default
    #
    # @example
    #   Dry::Schema.Params do
    #     required(:name).filled(:string)
    #     required(:age).value(:integer, gt?: 0)
    #   end
    #
    # @return [Params]
    #
    # @see Schema#define
    #
    # @api public
    def self.Params(**options, &block)
      define(**options, processor_type: Params, &block)
    end
    singleton_class.send(:alias_method, :Form, :Params)

    # Define a schema suitable for JSON data
    #
    # This schema type uses `Types::JSON` for coercion by default
    #
    # @example
    #   Dry::Schema.JSON do
    #     required(:name).filled(:string)
    #     required(:age).value(:integer, gt?: 0)
    #   end
    #
    # @return [Params]
    #
    # @see Schema#define
    #
    # @api public
    def self.JSON(**options, &block)
      define(**options, processor_type: JSON, &block)
    end
  end
end

require "dry/schema/extensions"

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
dry-schema-1.10.6 lib/dry/schema.rb
dry-schema-1.10.5 lib/dry/schema.rb
dry-schema-1.9.3 lib/dry/schema.rb
dry-schema-1.9.2 lib/dry/schema.rb
dry-schema-1.9.1 lib/dry/schema.rb
dry-schema-1.9.0 lib/dry/schema.rb
dry-schema-1.8.0 lib/dry/schema.rb
dry-schema-1.7.1 lib/dry/schema.rb
dry-schema-1.7.0 lib/dry/schema.rb
dry-schema-1.6.2 lib/dry/schema.rb
dry-schema-1.6.1 lib/dry/schema.rb
dry-schema-1.6.0 lib/dry/schema.rb
dry-schema-1.5.6 lib/dry/schema.rb
dry-schema-1.5.5 lib/dry/schema.rb
dry-schema-1.5.4 lib/dry/schema.rb
dry-schema-1.5.3 lib/dry/schema.rb
dry-schema-1.5.2 lib/dry/schema.rb
dry-schema-1.5.1 lib/dry/schema.rb
dry-schema-1.5.0 lib/dry/schema.rb