diff --git a/.ruby-version b/.ruby-version index eca07e4..cd57a8b 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.1.2 +2.1.5 diff --git a/lib/errapi/plugins/error_codes.rb b/lib/errapi/plugins/error_codes.rb index 4971c34..fe191a1 100644 --- a/lib/errapi/plugins/error_codes.rb +++ b/lib/errapi/plugins/error_codes.rb @@ -1,9 +1,11 @@ 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' @@ -11,7 +13,7 @@ class Errapi::Plugins::ErrorCodes } 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 diff --git a/lib/errapi/plugins/messages.rb b/lib/errapi/plugins/messages.rb index 96e9ff8..6e37669 100644 --- a/lib/errapi/plugins/messages.rb +++ b/lib/errapi/plugins/messages.rb @@ -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 diff --git a/lib/errapi/validators/presence.rb b/lib/errapi/validators/presence.rb index ede0503..9c59607 100644 --- a/lib/errapi/validators/presence.rb +++ b/lib/errapi/validators/presence.rb @@ -4,8 +4,8 @@ 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 @@ -13,15 +13,23 @@ def validate value, context, options = {} 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 diff --git a/lib/errapi/validators/length.rb b/lib/errapi/validators/string_length.rb similarity index 79% rename from lib/errapi/validators/length.rb rename to lib/errapi/validators/string_length.rb index 78fd86e..f57e83e 100644 --- a/lib/errapi/validators/length.rb +++ b/lib/errapi/validators/string_length.rb @@ -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 @@ -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