Skip to content

Commit

Permalink
Support message option in all validators
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed May 11, 2017
1 parent 7694342 commit 2ec83e5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Just use any of the following validators.
validates :nif, :valid_spanish_vat => true
end

You can override the error message using the `message` option.

class Person < ActiveRecord::Base
validates :dni, valid_nif: { message: 'invalid' }
end

## Contributing

1. Fork it
Expand Down
2 changes: 1 addition & 1 deletion lib/spanish_vat_validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module ActiveModel::Validations

module SpanishVatValidatorsHelpers
def message(kind='spanish_vat')
I18n.translate!("errors.messages.not_valid_#{kind}") rescue 'is invalid'
options[:message] || (I18n.translate!("errors.messages.not_valid_#{kind}") rescue 'is invalid')
end

# Validates NIF
Expand Down
43 changes: 40 additions & 3 deletions spec/spanish_vat_validators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
should_be_invalid(record)
end
end

it 'adds errors with an invalid number and custom message' do
%w[Y8527549 S618-5663I 000 Y1527549Z invalid_zip].each do |identification_number|
record = build_record(identification_number)
should_be_invalid(record, 'invalid')
end
end
end
end

Expand All @@ -34,6 +41,13 @@
should_be_invalid(record)
end
end

it 'adds errors with an invalid number and custom message' do
%w[1234 96380632 284-59349T 5696134L S6185663I invalid_zip].each do |identification_number|
record = build_record(identification_number)
should_be_invalid(record, 'invalid')
end
end
end
end

Expand All @@ -52,6 +66,13 @@
should_be_invalid(record)
end
end

it 'adds errors with an invalid number and custom message' do
%w[1234 96380632 284-59349T 5696134L S6185663I Y8527549Z invalid_zip].each do |identification_number|
record = build_record(identification_number)
should_be_invalid(record, 'invalid')
end
end
end
end

Expand All @@ -70,6 +91,13 @@
should_be_invalid(record)
end
end

it 'adds errors with an invalid number and custom message' do
%w[1234 2871341J 284-59349T 22472947S Y8527549Z invalid_zip].each do |identification_number|
record = build_record(identification_number)
should_be_invalid(record, 'invalid')
end
end
end
end

Expand All @@ -88,6 +116,13 @@
should_be_invalid(record)
end
end

it 'adds errors with an invalid number and custom message' do
%w[8527549Z 22472947S 000 S6185663I invalid_zip].each do |identification_number|
record = build_record(identification_number)
should_be_invalid(record, 'invalid')
end
end
end
end

Expand All @@ -96,10 +131,12 @@ def should_be_valid(record)
expect(record.errors).to be_empty
end

def should_be_invalid(record)
described_class.new(attributes: :identification_number).validate(record)
def should_be_invalid(record, message = nil)
params = { attributes: :identification_number }
params[:message] = message if message
described_class.new(params).validate(record)
expect(record.errors.size).to eq 1
expect(record.errors.messages[:identification_number]).to include 'is invalid'
expect(record.errors.messages[:identification_number]).to include(message || 'is invalid')
end

def build_record(identification_number)
Expand Down

0 comments on commit 2ec83e5

Please sign in to comment.