lib/validate_params/param_validator.rb in validate-params-0.11.0 vs lib/validate_params/param_validator.rb in validate-params-0.12.0
- old
+ new
@@ -7,27 +7,28 @@
class ParamValidator
def self.call(**args)
new(**args).call
end
- def initialize(type:, field:, value:, errors:, options: {})
- @type = type
- @field = field
+ attr_reader :validation
+
+ def initialize(validation:, value:, errors:)
+ @validation = validation
@value = value
@errors = errors
- @options = options
+ @options = validation.options.presence || {}
end
def call
return if @value.blank? && !@options[:required]
if @value.blank? && @options[:required]
@errors << { message: required_error_message }
return
end
- send(@type.to_s.underscore)
+ send(validation.type.to_s.underscore)
end
private
def array
return if Types::Array.valid?(@value, **@options)
@@ -127,21 +128,18 @@
max: @options[:max]
}
end
def error_param_name
- case @field
- when Array
- "#{@field[0]}[#{@field[1]}]"
- when Hash
- @field.map { |k, v| "#{k}[#{v}]" }.first
+ if validation.parent
+ "#{validation.parent.field}[#{validation.field}]"
else
- @field
+ validation.field
end
end
def error_message
- I18n.t("validate_params.invalid_type", param: error_param_name, type: @type)
+ I18n.t("validate_params.invalid_type", param: error_param_name, type: validation.type)
end
def required_error_message
I18n.t("validate_params.required", param: error_param_name)
end