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

Attributes inherited from DSL

#chain, #predicate_inferrer

Instance Method Summary collapse

Methods inherited from DSL

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

Methods inherited from Core

#new, #operation

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:



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

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:



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

def filter(*args, &block)
  filter_schema.optional(name).value(*args, &block)
  self
end

#filter_schemaSchema::DSL

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:



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

option :filter_schema, optional: true, default: proc { schema_dsl&.new }

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

Set type specification and predicates for a maybe value

Examples:

required(:name).maybe(:string)

Returns:

See Also:



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

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.



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

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)


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

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:



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

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