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/types_merger.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, TypesMerger, ValueCoercer

Constant Summary collapse

VERSION =
"1.13.4"
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:



48
49
50
# File 'lib/dry/schema.rb', line 48

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:



67
68
69
# File 'lib/dry/schema.rb', line 67

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:



106
107
108
# File 'lib/dry/schema.rb', line 106

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

.loaderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dry/schema.rb', line 18

def self.loader
  @loader ||= Zeitwerk::Loader.new.tap do |loader|
    root = File.expand_path("..", __dir__)
    loader.tag = "dry-schema"
    loader.inflector = Zeitwerk::GemInflector.new("#{root}/dry-schema.rb")
    loader.inflector.inflect(
      "dsl" => "DSL",
      "yaml" => "YAML",
      "json" => "JSON",
      "i18n" => "I18n"
    )
    loader.push_dir(root)
    loader.ignore(
      "#{root}/dry-schema.rb",
      "#{root}/dry/schema/{constants,errors,version,extensions}.rb",
      "#{root}/dry/schema/extensions"
    )
    loader.do_not_eager_load("#{root}/dry/schema/messages/i18n.rb")
    loader.inflector.inflect("dsl" => "DSL")
  end
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:



86
87
88
# File 'lib/dry/schema.rb', line 86

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