Sha256: 649c0c01626f6793584afbc0a27dcb6663212ed3b7128ff6eb70ae23acd23c49

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module NxtSchema
  module Node
    class Leaf < Node::Base
      def initialize(name:, type:, parent_node:, **options, &block)
        super
        @type = resolve_type(type)
      end

      def leaf?
        true
      end

      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 => [] }

        if maybe_criteria_applies?(input)
          self.value = input
        else
          self.value = value_or_default_value(input)
          self.value = coerce_value(value) unless maybe_criteria_applies?(value)
        end

        self_without_empty_schema_errors
      rescue Dry::Types::ConstraintError, Dry::Types::CoercionError => error
        add_schema_error(error.message)
        self_without_empty_schema_errors
      ensure
        mark_as_applied
      end

      private

      def resolve_type(name_or_type)
        root.send(:type_resolver).resolve(type_system, name_or_type)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nxt_schema-0.1.2 lib/nxt_schema/node/leaf.rb
nxt_schema-0.1.1 lib/nxt_schema/node/leaf.rb
nxt_schema-0.1.0 lib/nxt_schema/node/leaf.rb