DNI validation fixed

This commit is contained in:
Juan 2018-03-12 14:13:36 +01:00
parent 7825350884
commit ada0f40ece
3 changed files with 36 additions and 28 deletions

View File

@ -50,10 +50,22 @@ module.exports = function(Self) {
allowBlank: true allowBlank: true
}); });
let validateDni = require('../validations/validateDni'); Self.validateAsync('fi', fiIsValid, {
Self.validateBinded('fi', validateDni, {
message: 'DNI Incorrecto' message: 'DNI Incorrecto'
}); });
let validateDni = require('../validations/validateDni');
async function fiIsValid(err, done) {
let filter = {
fields: ['code'],
where: {id: this.countryFk}
};
let country = await Self.app.models.Country.findOne(filter);
let code = country ? country.code.toLowerCase() : null;
if (!validateDni(this.fi, code))
err();
done();
}
Self.validate('payMethod', hasSalesMan, { Self.validate('payMethod', hasSalesMan, {
message: 'No se puede cambiar la forma de pago si no hay comercial asignado' message: 'No se puede cambiar la forma de pago si no hay comercial asignado'

View File

@ -14,7 +14,7 @@ describe('DNI validation', () => {
expect(isValid).toBeTruthy(); expect(isValid).toBeTruthy();
}); });
it('should return true for spanish DNI with exceeded digits', () => { it('should return false for spanish DNI with exceeded digits', () => {
let isValid = validateDni('208497563239A'); let isValid = validateDni('208497563239A');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
@ -41,19 +41,19 @@ describe('DNI validation', () => {
describe('French', () => { describe('French', () => {
it('should return true for valid french DNI', () => { it('should return true for valid french DNI', () => {
let isValid = validateDni('FR1B123456789'); let isValid = validateDni('1B123456789', 'fr');
expect(isValid).toBeTruthy(); expect(isValid).toBeTruthy();
}); });
it('should return true for french DNI with exceeded digits', () => { it('should return false for french DNI with exceeded digits', () => {
let isValid = validateDni('FR1B12345678910'); let isValid = validateDni('1B12345678910', 'fr');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });
it('should return true for french DNI with bad syntax', () => { it('should return false for french DNI with bad syntax', () => {
let isValid = validateDni('FR1B12345678A'); let isValid = validateDni('1B12345678A', 'fr');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });
@ -61,19 +61,19 @@ describe('DNI validation', () => {
describe('Italian', () => { describe('Italian', () => {
it('should return true for valid italian DNI', () => { it('should return true for valid italian DNI', () => {
let isValid = validateDni('IT12345678911'); let isValid = validateDni('12345678911', 'it');
expect(isValid).toBeTruthy(); expect(isValid).toBeTruthy();
}); });
it('should return true for italian DNI with exceeded digits', () => { it('should return false for italian DNI with exceeded digits', () => {
let isValid = validateDni('IT123456789112'); let isValid = validateDni('123456789112', 'it');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });
it('should return true for italian DNI with bad syntax', () => { it('should return false for italian DNI with bad syntax', () => {
let isValid = validateDni('IT1234567891A'); let isValid = validateDni('1234567891A', 'it');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });
@ -81,19 +81,19 @@ describe('DNI validation', () => {
describe('Portuguese', () => { describe('Portuguese', () => {
it('should return true for valid portuguese DNI', () => { it('should return true for valid portuguese DNI', () => {
let isValid = validateDni('PT123456789'); let isValid = validateDni('123456789', 'pt');
expect(isValid).toBeTruthy(); expect(isValid).toBeTruthy();
}); });
it('should return true for portuguese DNI with exceeded digits', () => { it('should return false for portuguese DNI with exceeded digits', () => {
let isValid = validateDni('PT12345678910'); let isValid = validateDni('12345678910', 'pt');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });
it('should return true for portuguese DNI with bad syntax', () => { it('should return false for portuguese DNI with bad syntax', () => {
let isValid = validateDni('PT12345678A'); let isValid = validateDni('12345678A', 'pt');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });

View File

@ -1,14 +1,10 @@
module.exports = function(fiWithCountry) { module.exports = function(fi, country) {
if (fiWithCountry == null) return true; if (fi == null) return true;
if (typeof fiWithCountry != 'string') return false; if (typeof fi != 'string') return false;
fiWithCountry = fiWithCountry.toUpperCase(); fi = fi.toUpperCase();
country = country ? country.toLowerCase() : 'es';
if (!/^[A-Z]{2}/.test(fiWithCountry))
fiWithCountry = `ES${fiWithCountry}`;
let country = fiWithCountry.substring(0, 2).toLowerCase();
let fi = fiWithCountry.substring(2);
let len = fi.length; let len = fi.length;
let validators = { let validators = {
@ -38,7 +34,7 @@ module.exports = function(fiWithCountry) {
let sum = (pairSum + oddSum).toString(); let sum = (pairSum + oddSum).toString();
let units = parseInt(sum.charAt(sum.length - 1)); let units = parseInt(sum.charAt(sum.length - 1));
let control = units != 0 ? 10 - units : 0; let control = units == 0 ? 0 : 10 - units;
let index = 'JABCDEFGHI'.indexOf(lastDigit); let index = 'JABCDEFGHI'.indexOf(lastDigit);
computedDigit = index == -1 ? control.toString() : index; computedDigit = index == -1 ? control.toString() : index;
} else { } else {