Skip to content

Commit

Permalink
Array length and type validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaHydrae committed Jan 22, 2015
1 parent de1546a commit b212c02
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/errapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ def self.default_config
Configuration.new.tap do |config|
config.plugins << Errapi::Plugins::ErrorCodes.new
config.plugins << Errapi::Plugins::Messages.new
config.register_validation :length, Errapi::Validations::Length
config.register_validation :array_length, Errapi::Validations::ArrayLength
config.register_validation :string_length, Errapi::Validations::StringLength
config.register_validation :presence, Errapi::Validations::Presence
config.register_validation :type, Errapi::Validations::Type
config.register_condition Errapi::Condition::SimpleCheck
config.register_condition Errapi::Condition::ErrorCheck
config.register_location :dotted, Errapi::Locations::Dotted
Expand Down
8 changes: 8 additions & 0 deletions lib/errapi/plugins/error_codes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
class Errapi::Plugins::ErrorCodes
CODES = {
array_length: {
wrong_length: 'length.invalid',
too_short: 'length.tooShort',
too_long: 'length.tooLong'
},
presence: {
nil: 'presence.nil',
empty: 'presence.empty',
Expand All @@ -9,6 +14,9 @@ class Errapi::Plugins::ErrorCodes
wrong_length: 'length.invalid',
too_short: 'length.tooShort',
too_long: 'length.tooLong'
},
type: {
wrong_type: 'type.invalid'
}
}

Expand Down
14 changes: 11 additions & 3 deletions lib/errapi/plugins/messages.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
class Errapi::Plugins::Messages
MESSAGES = {
array_length: {
wrong_length: 'This array must contain exactly %{check_value} elements but has %{checked_value} elements.',
too_short: 'This array must contain at least %{check_value} elements but has only %{checked_value} elements.',
too_long: 'This array must contain at most %{check_value} elements but has %{checked_value} elements.'
},
presence: {
nil: 'This value cannot be null.',
empty: 'This value cannot be empty.',
blank: 'This value cannot be blank.'
},
string_length: {
wrong_length: 'This string must be exactly %{check_value} characters long but has %{checked_value}.',
too_short: 'This string must be at least %{check_value} characters long but has only %{checked_value}.',
too_long: 'This string must be at most %{check_value} characters long but has %{checked_value}.'
wrong_length: 'This string must be exactly %{check_value} characters long but has %{checked_value} characters.',
too_short: 'This string must be at least %{check_value} characters long but has only %{checked_value} characters.',
too_long: 'This string must be at most %{check_value} characters long but has %{checked_value} characters.'
},
type: {
wrong_type: 'This value is of the wrong type.'
}
}

Expand Down
31 changes: 31 additions & 0 deletions lib/errapi/validations/array_length.rb
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
2 changes: 1 addition & 1 deletion lib/errapi/validations/string_length.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Errapi::Validations::Length
class Errapi::Validations::StringLength
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze
CAUSES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze

Expand Down
12 changes: 12 additions & 0 deletions lib/errapi/validations/type.rb
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

0 comments on commit b212c02

Please sign in to comment.