diff --git a/README.md b/README.md index 1c01bec..8d98b65 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/spanish_vat_validators.rb b/lib/spanish_vat_validators.rb index 0263397..b0a5c52 100644 --- a/lib/spanish_vat_validators.rb +++ b/lib/spanish_vat_validators.rb @@ -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 diff --git a/spec/spanish_vat_validators_spec.rb b/spec/spanish_vat_validators_spec.rb index 0a6ad48..8e4d6f2 100644 --- a/spec/spanish_vat_validators_spec.rb +++ b/spec/spanish_vat_validators_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)