Sha256: d834fa443a662f2350f751292a4b89fed2c2bc6f59045ca054ed95520d9c071e
Contents?: true
Size: 1.17 KB
Versions: 40
Compression:
Stored size: 1.17 KB
Contents
module Schemacop module V2 class HashValidator < NodeSupportingField register symbols: :hash, klasses: Hash option :allow_obsolete_keys def validate(data, collector) super if data.is_a? ActiveSupport::HashWithIndifferentAccess allowed_fields = @fields.keys.map { |k| k.is_a?(String) ? k.to_sym : k } data_keys = data.keys.map { |k| k.is_a?(String) ? k.to_sym : k } # If the same key is specified in the schema as string and symbol, we # definitely expect a Ruby hash and not one with indifferent access if @fields.keys.length != Set.new(allowed_fields).length fail Exceptions::ValidationError, 'Hash expected, but got ActiveSupport::HashWithIndifferentAccess.' end else allowed_fields = @fields.keys data_keys = data.keys end obsolete_keys = data_keys - allowed_fields if !option?(:allow_obsolete_keys) && obsolete_keys.any? collector.error "Obsolete keys: #{obsolete_keys.inspect}." end @fields.each_value do |field| field.validate(data, collector) end end end end end
Version data entries
40 entries across 40 versions & 1 rubygems