Class: Dry::Schema::Macros::Key

Inherits:
DSL
  • Object
show all
Defined in:
lib/dry/schema/macros/key.rb

Overview

Base macro for specifying rules applied to a value found under a key

Direct Known Subclasses

Optional, Required

Instance Attribute Summary collapse

Attributes inherited from DSL

#chain, #predicate_inferrer, #primitive_inferrer

Instance Method Summary collapse

Methods inherited from DSL

#array, #each, #hash, #schema, #type

Methods inherited from Core

#new, #operation

Instance Attribute Details

#filter_schema_dslSchema::DSL (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.

Returns:



18
# File 'lib/dry/schema/macros/key.rb', line 18

option :filter_schema_dsl, default: proc { schema_dsl&.filter_schema_dsl }

Instance Method Details

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

Set type specification and predicates for a filled value

Examples:

required(:name).filled(:string)

Returns:

See Also:



72
73
74
75
76
# File 'lib/dry/schema/macros/key.rb', line 72

def filled(*args, **opts, &block)
  extract_type_spec(*args) do |*predicates, type_spec:|
    super(*predicates, type_spec: type_spec, **opts, &block)
  end
end

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

Specify predicates that should be applied before coercion

Examples:

check format before coercing to a date

required(:publish_date).filter(format?: /\d{4}-\d{2}-\d{2}).value(:date)

Returns:

See Also:



30
31
32
33
# File 'lib/dry/schema/macros/key.rb', line 30

def filter(*args, &block)
  (filter_schema_dsl[name] || filter_schema_dsl.optional(name)).value(*args, &block)
  self
end

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

Set type specification and predicates for a maybe value

Examples:

required(:name).maybe(:string)

Returns:

See Also:



88
89
90
91
92
93
94
# File 'lib/dry/schema/macros/key.rb', line 88

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

#to_astObject

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.



110
111
112
# File 'lib/dry/schema/macros/key.rb', line 110

def to_ast
  [:predicate, [:key?, [[:name, name], [:input, Undefined]]]]
end

#to_ruleDry::Logic::Rule

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.

Coerce macro to a rule

Returns:

  • (Dry::Logic::Rule)


101
102
103
104
105
106
107
# File 'lib/dry/schema/macros/key.rb', line 101

def to_rule
  if trace.captures.empty?
    super
  else
    [super, trace.to_rule(name)].reduce(operation)
  end
end

#value(type_spec, *predicates, **predicate_opts) ⇒ Macros::Key

Set type specification and predicates

Examples:

with a predicate

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

with a predicate with arguments

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

with a block

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

Parameters:

  • type_spec (Symbol, Types::Type, Array)
  • predicates (Array<Symbol>)
  • predicate_opts (Hash)

Returns:

See Also:



56
57
58
59
60
# File 'lib/dry/schema/macros/key.rb', line 56

def value(*args, **opts, &block)
  extract_type_spec(*args) do |*predicates, type_spec:|
    super(*predicates, type_spec: type_spec, **opts, &block)
  end
end