-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
380 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Grape | ||
module Validations | ||
module Validators | ||
class LengthValidator < Base | ||
def initialize(attrs, options, required, scope, **opts) | ||
@min = options[:min] | ||
@max = options[:max] | ||
|
||
super | ||
|
||
raise ArgumentError, 'min must be an integer greater than or equal to zero' if !@min.nil? && (!@min.is_a?(Integer) || @min.negative?) | ||
raise ArgumentError, 'max must be an integer greater than or equal to zero' if !@max.nil? && (!@max.is_a?(Integer) || @max.negative?) | ||
raise ArgumentError, "min #{@min} cannot be greater than max #{@max}" if !@min.nil? && !@max.nil? && @min > @max | ||
end | ||
|
||
def validate_param!(attr_name, params) | ||
param = params[attr_name] | ||
|
||
raise ArgumentError, "parameter #{param} does not support #length" unless param.respond_to?(:length) | ||
|
||
return unless (!@min.nil? && param.length < @min) || (!@max.nil? && param.length > @max) | ||
|
||
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: build_message) | ||
end | ||
|
||
def build_message | ||
if options_key?(:message) | ||
@option[:message] | ||
elsif @min && @max | ||
format I18n.t(:length, scope: 'grape.errors.messages'), min: @min, max: @max | ||
elsif @min | ||
format I18n.t(:length_min, scope: 'grape.errors.messages'), min: @min | ||
else | ||
format I18n.t(:length_max, scope: 'grape.errors.messages'), max: @max | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.