Module: Dry::Schema

Extended by:
Core::Extensions
Includes:
Core::Constants
Defined in:
lib/dry/schema.rb,
lib/dry/schema/dsl.rb,
lib/dry/schema/key.rb,
lib/dry/schema/json.rb,
lib/dry/schema/path.rb,
lib/dry/schema/step.rb,
lib/dry/schema/trace.rb,
lib/dry/schema/types.rb,
lib/dry/schema/config.rb,
lib/dry/schema/params.rb,
lib/dry/schema/result.rb,
lib/dry/schema/key_map.rb,
lib/dry/schema/message.rb,
lib/dry/schema/version.rb,
lib/dry/schema/compiler.rb,
lib/dry/schema/messages.rb,
lib/dry/schema/constants.rb,
lib/dry/schema/predicate.rb,
lib/dry/schema/processor.rb,
lib/dry/schema/macros/dsl.rb,
lib/dry/schema/macros/key.rb,
lib/dry/schema/message/or.rb,
lib/dry/schema/key_coercer.rb,
lib/dry/schema/macros/core.rb,
lib/dry/schema/macros/each.rb,
lib/dry/schema/macros/hash.rb,
lib/dry/schema/message_set.rb,
lib/dry/schema/macros/array.rb,
lib/dry/schema/macros/maybe.rb,
lib/dry/schema/macros/value.rb,
lib/dry/schema/rule_applier.rb,
lib/dry/schema/key_validator.rb,
lib/dry/schema/macros/filled.rb,
lib/dry/schema/macros/schema.rb,
lib/dry/schema/messages/i18n.rb,
lib/dry/schema/messages/yaml.rb,
lib/dry/schema/type_registry.rb,
lib/dry/schema/value_coercer.rb,
lib/dry/schema/type_container.rb,
lib/dry/schema/extensions/info.rb,
lib/dry/schema/macros/optional.rb,
lib/dry/schema/macros/required.rb,
lib/dry/schema/namespaced_rule.rb,
lib/dry/schema/processor_steps.rb,
lib/dry/schema/extensions/hints.rb,
lib/dry/schema/message_compiler.rb,
lib/dry/schema/extensions/monads.rb,
lib/dry/schema/extensions/struct.rb,
lib/dry/schema/messages/abstract.rb,
lib/dry/schema/messages/template.rb,
lib/dry/schema/predicate_inferrer.rb,
lib/dry/schema/predicate_registry.rb,
lib/dry/schema/primitive_inferrer.rb,
lib/dry/schema/message/or/abstract.rb,
lib/dry/schema/messages/namespaced.rb,
lib/dry/schema/message/or/multi_path.rb,
lib/dry/schema/extensions/json_schema.rb,
lib/dry/schema/message/or/single_path.rb,
lib/dry/schema/message_compiler/visitor_opts.rb,
lib/dry/schema/extensions/hints/result_methods.rb,
lib/dry/schema/extensions/info/schema_compiler.rb,
lib/dry/schema/extensions/hints/compiler_methods.rb,
lib/dry/schema/extensions/hints/message_set_methods.rb,
lib/dry/schema/extensions/json_schema/schema_compiler.rb,
lib/dry/schema/extensions/hints/message_compiler_methods.rb

Overview

Common constants used across the library

Defined Under Namespace

Modules: Extensions, Info, JSONSchema, Macros, Messages, Types Classes: Compiler, Config, DSL, Hint, JSON, Key, KeyCoercer, KeyMap, KeyValidator, Message, MessageCompiler, MessageSet, NamespacedRule, Params, Path, Predicate, PredicateInferrer, PredicateRegistry, PrimitiveInferrer, Processor, ProcessorSteps, Result, RuleApplier, Step, Trace, TypeContainer, TypeRegistry, ValueCoercer

Constant Summary collapse

VERSION =
"1.9.2"
LIST_SEPARATOR =
", "
QUESTION_MARK =
"?"
DOT =
"."
STEPS_IN_ORDER =

core processor steps in the default execution order

%i[
  key_validator
  key_coercer
  filter_schema
  value_coercer
  rule_applier
].freeze
DEFAULT_MESSAGES_PATH =

Path to the default set of localized messages bundled within the gem

Pathname(__dir__).join("../../../config/errors.yml").realpath.freeze
DEFAULT_MESSAGES_ROOT =

Default namespace used for localized messages in YAML files

"dry_schema"
InvalidSchemaError =

An error raised when DSL is used in an incorrect way

Class.new(StandardError)
MissingMessageError =

An error raised when a localized message cannot be found

Class.new(StandardError) do
  # @api private
  def initialize(path, paths = [])
    *rest, rule = path
    super(<<~STR)
      Message template for #{rule.inspect} under #{rest.join(DOT).inspect} was not found. Searched in:
      #{paths.map { |string| "\"#{string}\"" }.join("\n")}
    STR
  end
end

Class Method Summary collapse

Class Method Details

.configConfig

Configuration

Examples:

Dry::Schema.config.messages.backend = :i18n

Returns:



26
27
28
# File 'lib/dry/schema.rb', line 26

def self.config
  @config ||= Config.new
end

.define(**options, &block) ⇒ Processor

Define a schema

Examples:

Dry::Schema.define do
  required(:name).filled(:string)
  required(:age).value(:integer, gt?: 0)
end

Parameters:

  • options (Hash)

Returns:

See Also:



45
46
47
# File 'lib/dry/schema.rb', line 45

def self.define(**options, &block)
  DSL.new(**options, &block).call
end

.JSON(**options, &block) ⇒ Params

Define a schema suitable for JSON data

This schema type uses `Types::JSON` for coercion by default

Examples:

Dry::Schema.JSON do
  required(:name).filled(:string)
  required(:age).value(:integer, gt?: 0)
end

Returns:

See Also:



84
85
86
# File 'lib/dry/schema.rb', line 84

def self.JSON(**options, &block)
  define(**options, processor_type: JSON, &block)
end

.Params(**options, &block) ⇒ Params

Define a schema suitable for HTTP params

This schema type uses `Types::Params` for coercion by default

Examples:

Dry::Schema.Params do
  required(:name).filled(:string)
  required(:age).value(:integer, gt?: 0)
end

Returns:

See Also:



64
65
66
# File 'lib/dry/schema.rb', line 64

def self.Params(**options, &block)
  define(**options, processor_type: Params, &block)
end