ball-hayden / uk_account_validator

Validate UK Account Numbers and Sort Codes
BSD 2-Clause "Simplified" License
9 stars 12 forks source link

TypeError - nil can't be coerced into Integer #8

Open dbourguignon opened 5 years ago

dbourguignon commented 5 years ago

Hi,

With last version, trying to validate:

UkAccountValidator::Validator.new("6833861", "779136").valid?

Give this error:

Exception: TypeError: nil can't be coerced into Integer
--
0: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validators/standard_modulus.rb:18:in `*'
1: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validators/standard_modulus.rb:18:in `block (2 levels) in valid?'
2: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validators/standard_modulus.rb:15:in `each_pair'
3: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validators/standard_modulus.rb:15:in `each'
4: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validators/standard_modulus.rb:15:in `reduce'
5: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validators/standard_modulus.rb:15:in `block in valid?'
6: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validators/base_validator.rb:20:in `applying_exceptions'
7: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validators/standard_modulus.rb:14:in `valid?'
8: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validator.rb:46:in `block in valid?'
9: /Users/davidbourguignon/.rvm/gems/ruby-2.5.3@drivy-rails/gems/uk_account_validator-0.0.8/lib/uk_account_validator/validator.rb:39:in `each'

I looked a bit into it, but I don't know enough how this is suppose to work. I checked on https://www.sortcodes.co.uk/ and apparently this is an invalid code.

Any clue?

ball-hayden commented 5 years ago

Hi @dbourguignon - sorry for the slow reply.

I'm afraid I can't really tell you much either - I wrote this a long time ago, and haven't actively used it since.

My guess is the problem here is the account number being 7 digits long - I would usually expect it to be 8, although I do recall there being some exceptions. If the sort code was invalid, the code might not take the correct exception (an overloaded word here... 🙄), so we've ended up trying to multiply by the 8th account number (which is nil).

If you fancy putting together a PR that tests for this case, I believe the correct behaviour would be to prefix the account number with a 0 (but it may be that this is already correctly handled when the sort code is correct - I'm afraid I forget).

I don't think I'd be comfortable merging anything without also testing a valid 7 digit account number though - I believe these are typically Lloyds or TSB.

dbourguignon commented 5 years ago

@ball-hayden thanks for answering … I'll see if I can get more time for digging into it then