Sha256: c4bd8998a675676e81853176f5bb9aeba0e3c1a5f863996b9164207da5a665ca

Contents?: true

Size: 1.08 KB

Versions: 36

Compression:

Stored size: 1.08 KB

Contents

module Schemacop::V2
  class FieldNode < NodeSupportingType
    attr_reader :name

    def initialize(name, required, options = {}, &block)
      if options.any?
        fail Exceptions::InvalidSchemaError, 'Node does not support options.'
      end

      super({}, &block)

      @name = name
      @required = required
    end

    def validate(data, collector)
      if !data.key?(name) && @required
        collector.error "Missing key #{name.inspect}."
      end

      collector.path "/#{name}", name, :hash do
        value, default_applied = apply_default!(data[name], collector)

        unless data.key?(name) || default_applied
          next
        end

        super(value, collector)
      end
    end

    private

    def apply_default!(data, collector)
      return data, false unless data.nil?

      @types.each do |type|
        next unless type.option?(:default)

        default = type.option(:default)
        default = default.call if default.is_a?(Proc)
        collector.override_value(default)
        return default, true
      end

      return nil, false
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
schemacop-3.0.29 lib/schemacop/v2/field_node.rb
schemacop-3.0.28 lib/schemacop/v2/field_node.rb
schemacop-3.0.27 lib/schemacop/v2/field_node.rb
schemacop-3.0.26 lib/schemacop/v2/field_node.rb
schemacop-3.0.25 lib/schemacop/v2/field_node.rb
schemacop-3.0.24 lib/schemacop/v2/field_node.rb
schemacop-3.0.23 lib/schemacop/v2/field_node.rb
schemacop-3.0.22 lib/schemacop/v2/field_node.rb
schemacop-3.0.21 lib/schemacop/v2/field_node.rb
schemacop-3.0.20 lib/schemacop/v2/field_node.rb
schemacop-3.0.19 lib/schemacop/v2/field_node.rb
schemacop-3.0.18 lib/schemacop/v2/field_node.rb
schemacop-3.0.17 lib/schemacop/v2/field_node.rb
schemacop-3.0.16 lib/schemacop/v2/field_node.rb
schemacop-3.0.15 lib/schemacop/v2/field_node.rb
schemacop-3.0.14 lib/schemacop/v2/field_node.rb
schemacop-3.0.13 lib/schemacop/v2/field_node.rb
schemacop-3.0.12 lib/schemacop/v2/field_node.rb
schemacop-3.0.11 lib/schemacop/v2/field_node.rb
schemacop-3.0.10 lib/schemacop/v2/field_node.rb