Class: NxtSchema::Node::Schema
- Includes:
- HasSubNodes
- Defined in:
- lib/nxt_schema/node/schema.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes included from HasSubNodes
Attributes inherited from Base
#additional_keys_strategy, #all_nodes, #applied, #context, #errors, #input, #level, #locale, #name, #namespace, #options, #parent_node, #root, #schema_errors, #schema_errors_key, #type, #type_system, #validation_errors, #validations, #value
Instance Method Summary collapse
- #apply(input, parent_node: self.parent_node, context: nil) ⇒ Object
-
#initialize(name:, type: NxtSchema::Types::Strict::Hash, parent_node:, **options, &block) ⇒ Schema
constructor
A new instance of Schema.
- #optional(name, type, **options, &block) ⇒ Object
- #present(name, type, **options, &block) ⇒ Object
Methods included from HasSubNodes
#dup, #node, #nodes, #required, #schema, #struct
Methods inherited from Base
#add_error, #add_validators, #apply_validations, #build_validations, #default, #leaf?, #maybe, #meta, #parent, #presence, #presence?, #root?, #schema_errors?, #valid?, #validate, #validate_all_nodes, #validate_with, #validation_errors?, #validator, #value_or_default_value
Constructor Details
#initialize(name:, type: NxtSchema::Types::Strict::Hash, parent_node:, **options, &block) ⇒ Schema
4 5 6 7 |
# File 'lib/nxt_schema/node/schema.rb', line 4 def initialize(name:, type: NxtSchema::Types::Strict::Hash, parent_node:, **, &block) @template_store = TemplateStore.new super end |
Instance Method Details
#apply(input, parent_node: self.parent_node, context: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/nxt_schema/node/schema.rb', line 9 def apply(input, parent_node: self.parent_node, context: nil) self.input = input register_node(context) self.parent_node = parent_node self.schema_errors = { schema_errors_key => [] } self.validation_errors = { schema_errors_key => [] } self.value_store = {} self.value = transform_keys(input) if maybe_criteria_applies?(value) self.value_store = value else self.value = value_or_default_value(value) unless maybe_criteria_applies?(value) self.value = coerce_value(value) # TODO: We should not allow additional keys to be present per default?! # TODO: Handle this here sanitized_keys.each do |key| node = template_store[key] if allowed_additional_key?(key) value_store[key] = input[key] elsif node.presence? || input.key?(key) node.apply(input[key], parent_node: self, context: context).schema_errors? value_store[key] = node.value schema_errors[key] = node.schema_errors validation_errors[key] = node.validation_errors else evaluate_optional_option(node, input, key) end end self.value_store = coerce_value(value_store) self.value = value_store end end self_without_empty_schema_errors rescue Dry::Types::ConstraintError, Dry::Types::CoercionError => error add_schema_error(error.) self_without_empty_schema_errors ensure mark_as_applied end |
#optional(name, type, **options, &block) ⇒ Object
60 61 62 63 64 |
# File 'lib/nxt_schema/node/schema.rb', line 60 def optional(name, type, **, &block) if [:presence] node(name, type, .merge(optional: true), &block) end |
#present(name, type, **options, &block) ⇒ Object
66 67 68 69 70 |
# File 'lib/nxt_schema/node/schema.rb', line 66 def present(name, type, **, &block) if [:optional] node(name, type, .merge(presence: true), &block) end |