Sha256: ff63e56872a0b3755de8df1139d25f1195699f994e55cf42dc2c3b67c9db3f03
Contents?: true
Size: 1.77 KB
Versions: 4
Compression:
Stored size: 1.77 KB
Contents
# frozen_string_literal: true module ValidateParams module Validatable class ParamBuilder Validation = Struct.new(:field, :type, :options, :children, :parent) do def valid?(value, errors) ParamValidator.call( validation: self, value: value, errors: errors ) if children.any? case type.to_s when "Hash" # Skip in case hash is configured and string is passed if !value.is_a?(String) children.each { |c| c.valid?(value&.[](c.field), errors) } end when "Array" values = value ? Array.wrap(value) : [nil] values.each do |item| children.each do |child| child_value = item[child.field] if item.is_a?(Hash) || item.is_a?(ActionController::Parameters) || item.is_a?(Array) child.valid?(child_value, errors) end end else raise "Unexpected type: #{type}" end end errors.empty? end end def initialize(parent: nil, validations: []) @parent = parent @validations = validations end def param(field, type, options = {}) validation = Validation.new(field, type, options, [], @parent) if block_given? if ![Array, Hash].include?(type) raise "#{type} type cannot have nested definitions, only Array or Hash are supported" end yield ParamBuilder.new(parent: validation) end if @parent @parent.children << validation else @validations << validation end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems