class Dry::Schema::Macros::Key

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

@api public

Public Instance Methods

filled(*args, **opts, &block) click to toggle source

Set type specification and predicates for a filled value

@example

required(:name).filled(:string)

@see Macros::Key#value

@return [Macros::Key]

@api public

Calls superclass method Dry::Schema::Macros::DSL#filled
# File lib/dry/schema/macros/key.rb, line 72
def filled(*args, **opts, &block)
  extract_type_spec(*args) do |*predicates, type_spec:, type_rule:|
    super(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block)
  end
end
filter(*args, &block) click to toggle source

Specify predicates that should be applied before coercion

@example check format before coercing to a date

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

@see Macros::Key#value

@return [Macros::Key]

@api public

# File lib/dry/schema/macros/key.rb, line 29
def filter(*args, &block)
  (filter_schema_dsl[name] || filter_schema_dsl.optional(name)).value(*args, &block)
  self
end
maybe(*args, **opts, &block) click to toggle source

Set type specification and predicates for a maybe value

@example

required(:name).maybe(:string)

@see Macros::Key#value

@return [Macros::Key]

@api public

# File lib/dry/schema/macros/key.rb, line 88
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
to_ast() click to toggle source

@api private

# File lib/dry/schema/macros/key.rb, line 110
def to_ast
  [:predicate, [:key?, [[:name, name], [:input, Undefined]]]]
end
to_rule() click to toggle source

Coerce macro to a rule

@return [Dry::Logic::Rule]

@api private

Calls superclass method Dry::Schema::Macros::Core#to_rule
# 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(*args, **opts, &block) click to toggle source

@overload value(type_spec, *predicates, **predicate_opts)

Set type specification and predicates

@param [Symbol,Types::Type,Array] type_spec
@param [Array<Symbol>] predicates
@param [Hash] predicate_opts

@example with a predicate
  required(:name).value(:string, :filled?)

@example with a predicate with arguments
  required(:name).value(:string, min_size?: 2)

@example with a block
  required(:name).value(:string) { filled? & min_size?(2) }

@return [Macros::Key]

@see Macros::DSL#value

@api public

Calls superclass method Dry::Schema::Macros::DSL#value
# File lib/dry/schema/macros/key.rb, line 56
def value(*args, **opts, &block)
  extract_type_spec(*args) do |*predicates, type_spec:, type_rule:|
    super(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block)
  end
end