Class: NxtSchema::Node::Collection
- Includes:
- HasSubNodes
- Defined in:
- lib/nxt_schema/node/collection.rb
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::Array, parent_node:, **options, &block) ⇒ Collection
constructor
A new instance of Collection.
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, #optional, #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::Array, parent_node:, **options, &block) ⇒ Collection
Returns a new instance of Collection.
4 5 6 7 |
# File 'lib/nxt_schema/node/collection.rb', line 4 def initialize(name:, type: NxtSchema::Types::Strict::Array, 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/nxt_schema/node/collection.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 = 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) current_node_store = {} # if value.empty? # message = ErrorMessages.resolve(locale, :emptiness, value: value) # add_error(message) # end value.each_with_index do |item, index| item_schema_errors = schema_errors[index] ||= { schema_errors_key => [] } validation_errors[index] ||= { schema_errors_key => [] } template_store.each do |node_name, node| current_node = node.dup current_node_store[node_name] = current_node current_node.apply(item, parent_node: self, context: context) value_store[index] = current_node.value unless current_node.schema_errors? current_node_store.each do |node_name, node| node.schema_errors = { } node.validation_errors = { } item_schema_errors = schema_errors[index][node_name] = node.schema_errors validation_errors[index][node_name] = node.validation_errors end break else schema_errors[index][node_name] = current_node.schema_errors validation_errors[index][node_name] = current_node.validation_errors end end item_schema_errors.reject! { |_, v| v.empty? } end # Once we collected all values ensure type by casting again 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 |