Class: Dry::Schema::Macros::DSL

Inherits:
Core
  • Object
show all
Includes:
Logic::Operators
Defined in:
lib/dry/schema/macros/dsl.rb

Overview

Macro specialization used within the DSL

Direct Known Subclasses

Array, Each, Key, Maybe, Value

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Core

#new, #operation, #path, #to_ast, #to_rule

Instance Attribute Details

#chainBoolean (readonly)

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.

Indicate if the macro should append its rules to the provided trace

Returns:

  • (Boolean)


20
# File 'lib/dry/schema/macros/dsl.rb', line 20

option :chain, default: -> { true }

#predicate_inferrerPredicateInferrer (readonly)

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.

PredicateInferrer is used to infer predicate type-check from a type spec

Returns:



26
# File 'lib/dry/schema/macros/dsl.rb', line 26

option :predicate_inferrer, default: proc { PredicateInferrer.new(compiler.predicates) }

#primitive_inferrerPrimitiveInferrer (readonly)

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.

PrimitiveInferrer used to get a list of primitive classes from configured type

Returns:



32
# File 'lib/dry/schema/macros/dsl.rb', line 32

option :primitive_inferrer, default: proc { PrimitiveInferrer.new }

Instance Method Details

#arrayMacros::Core

Like ‘each` but sets `array?` type-check

Examples:

a list of strings

required(:tags).array(:str?)

a list of hashes

required(:tags).array(:hash) do
  required(:name).filled(:string)
end

Returns:



176
177
178
179
180
# File 'lib/dry/schema/macros/dsl.rb', line 176

def array(...)
  append_macro(Macros::Array) do |macro|
    macro.value(...)
  end
end

#custom_type?Boolean

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.

Returns:

  • (Boolean)


198
199
200
# File 'lib/dry/schema/macros/dsl.rb', line 198

def custom_type?
  schema_dsl.custom_type?(name)
end

#eachMacros::Core

Specify predicates that should be applied to each element of an array

This is a simpler building block than ‘array` macro, use it when you want to provide `array?` type-check with other rules manually.

Examples:

a list of strings

required(:tags).value(:array, min_size?: 2).each(:str?)

a list of hashes

required(:tags).value(:array, min_size?: 2).each(:hash) do
  required(:name).filled(:string)
end

Returns:



157
158
159
160
161
# File 'lib/dry/schema/macros/dsl.rb', line 157

def each(...)
  append_macro(Macros::Each) do |macro|
    macro.value(...)
  end
end

#filled(*args, **opts, &block) ⇒ Macros::Core

Prepends ‘:filled?` predicate

Examples:

with a type spec

required(:name).filled(:string)

with a type spec and a predicate

required(:name).filled(:string, format?: /\w+/)

Returns:



80
81
82
83
84
85
86
# File 'lib/dry/schema/macros/dsl.rb', line 80

def filled(*args, **opts, &block)
  extract_type_spec(args) do |*predicates, type_spec:, type_rule:|
    append_macro(Macros::Filled) do |macro|
      macro.call(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block)
    end
  end
end

#hashObject

Specify a nested hash with enforced ‘hash?` type-check

Examples:

required(:tags).hash do
  required(:name).value(:string)
end


134
135
136
137
138
# File 'lib/dry/schema/macros/dsl.rb', line 134

def hash(...)
  append_macro(Macros::Hash) do |macro|
    macro.call(...)
  end
end

#maybe(*args, **opts, &block) ⇒ Macros::Key

Set type specification and predicates for a maybe value

Examples:

required(:name).maybe(:string)

Returns:

See Also:

  • Macros::Key#value


98
99
100
101
102
103
104
# File 'lib/dry/schema/macros/dsl.rb', line 98

def maybe(*args, **opts, &block)
  extract_type_spec(args, nullable: true) do |*predicates, type_spec:, type_rule:|
    append_macro(Macros::Maybe) do |macro|
      macro.call(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block)
    end
  end
end

#schemaMacros::Core

Specify a nested hash without enforced ‘hash?` type-check

This is a simpler building block than ‘hash` macro, use it when you want to provide `hash?` type-check with other rules manually.

Examples:

required(:tags).value(:hash, min_size?: 1).schema do
  required(:name).value(:string)
end

Returns:



120
121
122
123
124
# File 'lib/dry/schema/macros/dsl.rb', line 120

def schema(...)
  append_macro(Macros::Schema) do |macro|
    macro.call(...)
  end
end

#type(spec) ⇒ Macros::Key

Set type spec

Examples:

required(:name).type(:string).value(min_size?: 2)

Parameters:

  • spec (Symbol, Array, Dry::Types::Type)

Returns:



192
193
194
195
# File 'lib/dry/schema/macros/dsl.rb', line 192

def type(spec)
  schema_dsl.set_type(name, spec)
  self
end

#value(*predicates, **predicate_opts) ⇒ Macros::Core

Set predicates without and with arguments

Examples:

with a predicate

required(:name).value(:filled?)

with a predicate with arguments

required(:name).value(min_size?: 2)

with a predicate with and without arguments

required(:name).value(:filled?, min_size?: 2)

with a block

required(:name).value { filled? & min_size?(2) }

Parameters:

  • predicates (Array<Symbol>)
  • predicate_opts (Hash)

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dry/schema/macros/dsl.rb', line 55

def value(*args, **opts, &block)
  if (type_spec_from_opts = opts[:type_spec])
    append_macro(Macros::Value) do |macro|
      macro.call(*args, type_spec: type_spec_from_opts, **opts, &block)
    end
  else
    extract_type_spec(args) do |*predicates, type_spec:, type_rule:|
      append_macro(Macros::Value) do |macro|
        macro.call(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block)
      end
    end
  end
end