-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
de1546a
commit b212c02
Showing
6 changed files
with
66 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Errapi::Validations::ArrayLength | ||
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze | ||
CAUSES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze | ||
|
||
def initialize options = {} | ||
@constraints = actual_constraints options | ||
end | ||
|
||
def validate value, context, options = {} | ||
return unless value.respond_to? :length | ||
|
||
actual_length = value.length | ||
|
||
CHECKS.each_pair do |key,check| | ||
next unless check_value = @constraints[key] | ||
next if actual_length.send check, check_value | ||
context.add_error cause: CAUSES[key], constraint_value: check_value, constrained_value: actual_length | ||
end | ||
end | ||
|
||
private | ||
|
||
def actual_constraints options = {} | ||
options.dup.tap do |actual| | ||
if range = actual.delete(:within) | ||
raise ArgumentError unless range.kind_of? Range | ||
actual[:minimum], actual[:maximum] = range.min, range.max | ||
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
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,12 @@ | ||
class Errapi::Validations::Type | ||
|
||
def initialize options = {} | ||
@type = options[:kind_of] | ||
end | ||
|
||
def validate value, context, options = {} | ||
unless value.kind_of? @type | ||
context.add_error cause: :wrong_type | ||
end | ||
end | ||
end |