Class: Dry::Schema::DSL
- Inherits:
-
Object
- Object
- Dry::Schema::DSL
- Extended by:
- Initializer
- Defined in:
- lib/dry/schema/dsl.rb
Overview
The schema definition DSL class
The DSL is exposed by:
- `Schema.define`
- `Schema.Params`
- `Schema.JSON`
- `Schema::Params.define` - use with sub-classes
- `Schema::JSON.define` - use with sub-classes
Constant Summary collapse
Class Method Summary collapse
-
.new(**options, &block) ⇒ DSL
Build a new DSL object and evaluate provided block.
Instance Method Summary collapse
-
#[](name) ⇒ Macros::Core
Return a macro with the provided name.
-
#array ⇒ Dry::Types::Array::Member
A shortcut for defining an array type with a member.
-
#call ⇒ Processor, ...
private
Build a processor based on DSL’s definitions.
-
#compiler ⇒ Compiler
The rule compiler object.
-
#config ⇒ Config
Configuration object exposed via ‘#configure` method.
-
#configure(&block) ⇒ DSL
Provide customized configuration for your schema.
-
#filter_rules? ⇒ Boolean
private
Check if any filter rules were defined.
- #filter_schema ⇒ Object private
-
#filter_schema_dsl ⇒ Object
private
Build an input schema DSL used by ‘filter` API.
-
#key(name, macro:, &block) ⇒ Macros::Key
A generic method for defining keys.
-
#macros ⇒ Array
An array with macros defined within the DSL.
-
#new(options = EMPTY_HASH, &block) ⇒ Dry::Types::Safe
private
Return a new DSL instance using the same processor type.
-
#optional(name, &block) ⇒ Macros::Optional
Define an optional key.
-
#parent ⇒ DSL
An optional parent DSL object that will be used to merge keys and rules.
-
#processor_type ⇒ Compiler
The type of the processor (Params, JSON, or a custom sub-class).
-
#required(name, &block) ⇒ Macros::Required
Define a required key.
-
#resolve_type(spec) ⇒ Dry::Types::Type
private
Resolve type object from the provided spec.
-
#set_type(name, spec) ⇒ Dry::Types::Safe
private
Set a type for the given key name.
-
#to_rule ⇒ RuleApplier
Cast this DSL into a rule object.
-
#type_schema ⇒ Dry::Types::Safe
private
Return type schema used by the value coercer.
-
#types ⇒ Compiler
A key=>type map defined within the DSL.
Class Method Details
.new(**options, &block) ⇒ DSL
Build a new DSL object and evaluate provided block
89 90 91 92 93 |
# File 'lib/dry/schema/dsl.rb', line 89 def self.new(**, &block) dsl = super dsl.instance_eval(&block) if block dsl end |
Instance Method Details
#[](name) ⇒ Macros::Core
Return a macro with the provided name
121 122 123 |
# File 'lib/dry/schema/dsl.rb', line 121 def [](name) macros.detect { |macro| macro.name.equal?(name) } end |
#array ⇒ Dry::Types::Array::Member
A shortcut for defining an array type with a member
214 215 216 |
# File 'lib/dry/schema/dsl.rb', line 214 def array -> member_type { type_registry['array'].of(resolve_type(member_type)) } end |
#call ⇒ Processor, ...
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.
Build a processor based on DSL’s definitions
191 192 193 194 195 196 197 |
# File 'lib/dry/schema/dsl.rb', line 191 def call steps = [key_coercer] steps << filter_schema.rule_applier if filter_rules? steps << value_coercer << rule_applier processor_type.new(schema_dsl: self, steps: steps) end |
#compiler ⇒ Compiler
Returns The rule compiler object.
56 |
# File 'lib/dry/schema/dsl.rb', line 56 option :compiler, default: -> { Compiler.new } |
#config ⇒ Config
Returns Configuration object exposed via ‘#configure` method.
71 |
# File 'lib/dry/schema/dsl.rb', line 71 option :config, optional: true, default: proc { parent ? parent.config.dup : Config.new } |
#configure(&block) ⇒ DSL
Provide customized configuration for your schema
109 110 111 112 |
# File 'lib/dry/schema/dsl.rb', line 109 def configure(&block) config.configure(&block) self end |
#filter_rules? ⇒ 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.
Check if any filter rules were defined
285 286 287 |
# File 'lib/dry/schema/dsl.rb', line 285 def filter_rules? (instance_variable_defined?('@filter_schema_dsl') && !filter_schema_dsl.macros.empty?) || parent&.filter_rules? end |
#filter_schema ⇒ Object
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.
269 270 271 |
# File 'lib/dry/schema/dsl.rb', line 269 def filter_schema filter_schema_dsl.call end |
#filter_schema_dsl ⇒ Object
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.
Build an input schema DSL used by ‘filter` API
278 279 280 |
# File 'lib/dry/schema/dsl.rb', line 278 def filter_schema_dsl @filter_schema_dsl ||= new(parent: parent_filter_schema) end |
#key(name, macro:, &block) ⇒ Macros::Key
A generic method for defining keys
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/dry/schema/dsl.rb', line 169 def key(name, macro:, &block) raise ArgumentError, "Key +#{name}+ is not a symbol" unless name.is_a?(::Symbol) set_type(name, Types::Any) macro = macro.new( name: name, compiler: compiler, schema_dsl: self, filter_schema_dsl: filter_schema_dsl ) macro.value(&block) if block macros << macro macro end |
#macros ⇒ Array
Returns An array with macros defined within the DSL.
62 |
# File 'lib/dry/schema/dsl.rb', line 62 option :macros, default: -> { EMPTY_ARRAY.dup } |
#new(options = EMPTY_HASH, &block) ⇒ Dry::Types::Safe
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.
Return a new DSL instance using the same processor type
233 234 235 |
# File 'lib/dry/schema/dsl.rb', line 233 def new( = EMPTY_HASH, &block) self.class.new(.merge(processor_type: processor_type, config: config), &block) end |
#optional(name, &block) ⇒ Macros::Optional
Define an optional key
This works exactly the same as ‘required` except that if a key is not present rules will not be applied
157 158 159 |
# File 'lib/dry/schema/dsl.rb', line 157 def optional(name, &block) key(name, macro: Macros::Optional, &block) end |
#parent ⇒ DSL
Returns An optional parent DSL object that will be used to merge keys and rules.
68 |
# File 'lib/dry/schema/dsl.rb', line 68 option :parent, optional: true |
#processor_type ⇒ Compiler
Returns The type of the processor (Params, JSON, or a custom sub-class).
59 |
# File 'lib/dry/schema/dsl.rb', line 59 option :processor_type, default: -> { Processor } |
#required(name, &block) ⇒ Macros::Required
Define a required key
141 142 143 |
# File 'lib/dry/schema/dsl.rb', line 141 def required(name, &block) key(name, macro: Macros::Required, &block) end |
#resolve_type(spec) ⇒ Dry::Types::Type
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.
Resolve type object from the provided spec
259 260 261 262 263 264 265 266 |
# File 'lib/dry/schema/dsl.rb', line 259 def resolve_type(spec) case spec when ::Dry::Types::Type then spec when ::Array then spec.map { |s| resolve_type(s) }.reduce(:|) else type_registry[spec] end end |
#set_type(name, spec) ⇒ Dry::Types::Safe
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.
Set a type for the given key name
245 246 247 248 249 250 |
# File 'lib/dry/schema/dsl.rb', line 245 def set_type(name, spec) type = resolve_type(spec) = { required: false, maybe: type.optional? } types[name] = type.() end |
#to_rule ⇒ RuleApplier
Cast this DSL into a rule object
202 203 204 |
# File 'lib/dry/schema/dsl.rb', line 202 def to_rule call.to_rule end |
#type_schema ⇒ Dry::Types::Safe
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.
Return type schema used by the value coercer
223 224 225 226 |
# File 'lib/dry/schema/dsl.rb', line 223 def type_schema schema = type_registry['hash'].schema(types).lax parent ? parent.type_schema.schema(schema.to_a) : schema end |
#types ⇒ Compiler
Returns A key=>type map defined within the DSL.
65 |
# File 'lib/dry/schema/dsl.rb', line 65 option :types, default: -> { EMPTY_HASH.dup } |