Skip to content

Commit

Permalink
Improved messages and error code plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaHydrae committed Jan 19, 2015
1 parent 5e372a5 commit 7a70ad8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.2
2.1.5
8 changes: 5 additions & 3 deletions lib/errapi/plugins/error_codes.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
class Errapi::Plugins::ErrorCodes
CODES = {
presence: {
blank: 'blank'
nil: 'presence.nil',
empty: 'presence.empty',
blank: 'presence.blank'
},
length: {
string_length: {
wrong_length: 'length.invalid',
too_short: 'length.tooShort',
too_long: 'length.tooLong'
}
}

def build_error error, context
if !error.code && CODES.key?(error.validator_name) && code = CODES[error.validator_name][error.message]
if !error.code && CODES.key?(error.validator_name) && code = CODES[error.validator_name][error.cause]
error.code = code
end
end
Expand Down
19 changes: 14 additions & 5 deletions lib/errapi/plugins/messages.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
class Errapi::Plugins::Messages
MESSAGES = {
presence: {
nil: 'This value cannot be null.',
empty: 'This value cannot be empty.',
blank: 'This value cannot be blank.'
},
length: {
wrong_length: 'This value is not the right length.',
too_short: 'This value is too short.',
too_long: 'This value is too long.'
string_length: {
wrong_length: 'This string must be exactly %{constraint_length} characters long but has %{constrained_value}.',
too_short: 'This string must be at least %{constraint_length} characters long but has only %{constrained_value}.',
too_long: 'This string must be at most %{constraint_length} characters long but has %{constrained_value}.'
}
}

def build_error error, context
if error.message.kind_of?(Symbol) && MESSAGES.key?(error.validator_name) && message = MESSAGES[error.validator_name][error.message]
if !error.message && MESSAGES.key?(error.validator_name) && message = MESSAGES[error.validator_name][error.cause]

%w(constraint_length constrained_value).each do |interpolated|
if error.respond_to? interpolated
message = message.gsub /\%\{#{interpolated}\}/, interpolated
end
end

error.message = message
end
end
Expand Down
18 changes: 13 additions & 5 deletions lib/errapi/validators/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,32 @@ def initialize options = {}
end

def validate value, context, options = {}
if value_blank? value
context.add_error message: :blank
if cause = check(value)
context.add_error cause: cause
end
end

private

BLANK_REGEXP = /\A[[:space:]]*\z/

def check value
if value.nil?
:nil
elsif value.respond_to?(:empty?) && value.empty?
:empty
elsif value_blank? value
:blank
end
end

def value_blank? value
if value.respond_to? :blank?
value.blank?
elsif value.kind_of? String
BLANK_REGEXP === value
elsif value.respond_to? :empty?
value.empty?
else
!value
false
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Errapi::Validators::Length
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze
MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze
CAUSES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze

def initialize options = {}
@constraints = actual_constraints options
Expand All @@ -14,7 +14,7 @@ def validate value, context, options = {}
CHECKS.each_pair do |key,check|
next unless check_value = @constraints[key]
next if actual_length.send check, check_value
context.add_error message: MESSAGES[key], check_value: check_value
context.add_error cause: CAUSES[key], constraint_value: check_value, constrained_value: actual_length
end
end

Expand Down

0 comments on commit 7a70ad8

Please sign in to comment.