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, #custom_type?, #each, #filled, #hash, #maybe, #schema, #type, #value

Methods inherited from Core

#new, #operation, #path

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:



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

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

Instance Method Details

#filterMacros::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:

  • Macros::Key#value


25
26
27
28
# File 'lib/dry/schema/macros/key.rb', line 25

def filter(...)
  (filter_schema_dsl[name] || filter_schema_dsl.optional(name)).value(...)
  self
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.



44
45
46
# File 'lib/dry/schema/macros/key.rb', line 44

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)


35
36
37
38
39
40
41
# File 'lib/dry/schema/macros/key.rb', line 35

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