class Dry::Schema::KeyValidator
@api private
def call(result)
- Api: - private
def call(result) input = result.to_h input_paths = key_paths(input) key_paths = key_map.to_dot_notation input_paths.each do |path| error_path = if path[INDEX_REGEX] key = path.gsub(INDEX_REGEX, BRACKETS) unless key_paths.include?(key) arr = path.gsub(INDEX_REGEX) { |m| ".#{m[1]}" } arr.split(DOT).map { |s| DIGIT_REGEX.match?(s) ? s.to_i : s.to_sym } end elsif !key_paths.include?(path) path end next unless error_path result.add_error([:unexpected_key, [error_path, input]]) end result end
def key_paths(hash)
- Api: - private
def key_paths(hash) hash.flat_map { |key, _| case (value = hash[key]) when Hash [key].product(key_paths(hash[key])).map { |keys| keys.join(DOT) } when Array value.flat_map.with_index { |el, idx| key_paths(el).map { |path| ["#{key}[#{idx}]", *path].join(DOT) } } else key.to_s end } end