-
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
ca351ae
commit f73a788
Showing
4 changed files
with
172 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,54 @@ | ||
module Errapi::Validations | ||
class Type | ||
class Type < Base | ||
|
||
def initialize options = {} | ||
unless key = exactly_one_option?(OPTIONS, options) | ||
raise ArgumentError, "One option among :instance_of, :kind_of, :is_a or :is_an must be supplied (but only one)." | ||
end | ||
|
||
keys = options.keys.select{ |k,v| OPTIONS.include? k } | ||
raise ArgumentError, "One option among :instance_of, :kind_of, :is_a or :is_an must be given (but only one)." if keys.length != 1 | ||
|
||
if options.key? :instance_of | ||
@instance_of = check_type! options[:instance_of] | ||
if key == :instance_of | ||
@instance_of = check_types! options[key] | ||
raise ArgumentError, "Type aliases cannot be used with the :instance_of option. Use :kind_of, :is_a or :is_an." if options[key].kind_of? Symbol | ||
else | ||
@kind_of = check_type! options[keys.first] | ||
@kind_of = check_types! options[key] | ||
end | ||
end | ||
|
||
def validate value, context, options = {} | ||
if @instance_of && !value.instance_of?(@instance_of) | ||
if @instance_of && @instance_of.none?{ |type| value.instance_of? type } | ||
context.add_error reason: :wrong_type, check_value: @instance_of, checked_value: value.class | ||
elsif @kind_of && !value.kind_of?(@kind_of) | ||
elsif @kind_of && @kind_of.none?{ |type| value.kind_of? type } | ||
context.add_error reason: :wrong_type, check_value: @kind_of, checked_value: value.class | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_type! type | ||
raise ArgumentError, "A class or module is required, but a #{type.class} was given." unless TYPE_CLASSES.include? type.class | ||
type | ||
def check_types! types | ||
if !types.kind_of?(Array) | ||
types = [ types ] | ||
elsif types.empty? | ||
raise ArgumentError, "At least one class or module is required, but an empty array was given." | ||
end | ||
|
||
types.each do |type| | ||
unless TYPE_ALIASES.key?(type) || type.class == Class || type.class == Module | ||
raise ArgumentError, "A class or module (or an array of classes or modules, or a type alias) is required, but a #{type.class} was given." | ||
end | ||
end | ||
|
||
types.collect{ |type| TYPE_ALIASES[type] || type }.flatten.uniq | ||
end | ||
|
||
OPTIONS = %i(instance_of kind_of is_a is_an) | ||
TYPE_CLASSES = [ Class, Module ] | ||
TYPE_ALIASES = { | ||
string: [ String ], | ||
number: [ Numeric ], | ||
integer: [ Integer ], | ||
boolean: [ TrueClass, FalseClass ], | ||
object: [ Hash ], | ||
array: [ Array ], | ||
null: [ NilClass ] | ||
} | ||
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
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