Sha256: c26d260815b9dcde6e4944b57f0e92aa9baf5cdbd82a7e2f4c4e68b4f188a7c5

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

module Schemacop
  module V3
    class OneOfNode < CombinationNode
      def type
        :oneOf
      end

      def self.allowed_options
        super + %i[treat_blank_as_nil]
      end

      def cast(value)
        item = match(value)

        unless item
          if options[:treat_blank_as_nil] && value.blank? && !value.is_a?(FalseClass)
            return nil
          else
            return value
          end
        end

        return item.cast(value)
      end

      def _validate(data, result:)
        if options[:treat_blank_as_nil] && data.blank? && !data.is_a?(FalseClass)
          data = nil
        end

        super_data = super(data, result: result)
        return if super_data.nil?

        matches = matches(super_data)

        if matches.size == 1
          matches.first._validate(super_data, result: result)
        else
          result.error "Matches #{matches.size} definitions but should match exactly 1."
        end
      end

      def validate_self
        if @items.size < 2
          fail 'Node "one_of" makes only sense with at least 2 items.'
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
schemacop-3.0.19 lib/schemacop/v3/one_of_node.rb
schemacop-3.0.18 lib/schemacop/v3/one_of_node.rb
schemacop-3.0.17 lib/schemacop/v3/one_of_node.rb
schemacop-3.0.16 lib/schemacop/v3/one_of_node.rb
schemacop-3.0.15 lib/schemacop/v3/one_of_node.rb
schemacop-3.0.14 lib/schemacop/v3/one_of_node.rb
schemacop-3.0.13 lib/schemacop/v3/one_of_node.rb
schemacop-3.0.12 lib/schemacop/v3/one_of_node.rb
schemacop-3.0.11 lib/schemacop/v3/one_of_node.rb