-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(validation): fixing validation when cpf or cnpj is empty
- Loading branch information
Ben-hur Santos Ott
committed
Dec 12, 2017
1 parent
b486262
commit 56de29c
Showing
8 changed files
with
439 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,73 @@ | ||
import { CnpjMask } from '../lib/masks'; | ||
import { CnpjMask } from '../lib/masks' | ||
|
||
test('getType results cnpj', () => { | ||
var expected = 'cnpj'; | ||
var received = CnpjMask.getType(); | ||
var expected = 'cnpj' | ||
var received = CnpjMask.getType() | ||
|
||
expect(received).toBe(expected); | ||
}); | ||
expect(received).toBe(expected) | ||
}) | ||
|
||
test('79885262000130 results 79.885.262/0001-30', () => { | ||
var mask = new CnpjMask(); | ||
var expected = '79.885.262/0001-30'; | ||
var received = mask.getValue('79885262000130'); | ||
var mask = new CnpjMask() | ||
var expected = '79.885.262/0001-30' | ||
var received = mask.getValue('79885262000130') | ||
|
||
expect(received).toBe(expected); | ||
}); | ||
expect(received).toBe(expected) | ||
}) | ||
|
||
test('798852 results 79.885.2', () => { | ||
var mask = new CnpjMask(); | ||
var expected = '79.885.2'; | ||
var received = mask.getValue('798852'); | ||
var mask = new CnpjMask() | ||
var expected = '79.885.2' | ||
var received = mask.getValue('798852') | ||
|
||
expect(received).toBe(expected); | ||
}); | ||
expect(received).toBe(expected) | ||
}) | ||
|
||
test('79885262000130 results 79.885.262/0001-30 and is valid', () => { | ||
var mask = new CnpjMask(); | ||
var expected = '79.885.262/0001-30'; | ||
var received = mask.getValue('79885262000130'); | ||
var isValid = mask.validate(received); | ||
var mask = new CnpjMask() | ||
var expected = '79.885.262/0001-30' | ||
var received = mask.getValue('79885262000130') | ||
var isValid = mask.validate(received) | ||
|
||
expect(received).toBe(expected); | ||
expect(isValid).toBe(true); | ||
}); | ||
expect(received).toBe(expected) | ||
expect(isValid).toBe(true) | ||
}) | ||
|
||
test('79885262000140 results 79.885.262/0001-40 and is not valid', () => { | ||
var mask = new CnpjMask(); | ||
var expected = '79.885.262/0001-40'; | ||
var received = mask.getValue('79885262000140'); | ||
var isValid = mask.validate(received); | ||
var mask = new CnpjMask() | ||
var expected = '79.885.262/0001-40' | ||
var received = mask.getValue('79885262000140') | ||
var isValid = mask.validate(received) | ||
|
||
expect(received).toBe(expected); | ||
expect(isValid).toBe(false); | ||
}); | ||
expect(received).toBe(expected) | ||
expect(isValid).toBe(false) | ||
}) | ||
|
||
test('7988526200013 results 79.885.262/0001-3 and is not valid', () => { | ||
var mask = new CnpjMask(); | ||
var expected = '79.885.262/0001-3'; | ||
var received = mask.getValue('7988526200013'); | ||
var isValid = mask.validate(received); | ||
var mask = new CnpjMask() | ||
var expected = '79.885.262/0001-3' | ||
var received = mask.getValue('7988526200013') | ||
var isValid = mask.validate(received) | ||
|
||
expect(received).toBe(expected); | ||
expect(isValid).toBe(false); | ||
}); | ||
expect(received).toBe(expected) | ||
expect(isValid).toBe(false) | ||
}) | ||
|
||
test('79885262000130 results 79.885.262/0001-30 and raw value 79885262000130', () => { | ||
var mask = new CnpjMask(); | ||
var expected = '79.885.262/0001-30'; | ||
var received = mask.getValue('79885262000130'); | ||
var mask = new CnpjMask() | ||
var expected = '79.885.262/0001-30' | ||
var received = mask.getValue('79885262000130') | ||
|
||
var expectedRawValue = '79885262000130'; | ||
var receivedRawValue = mask.getRawValue(received); | ||
var expectedRawValue = '79885262000130' | ||
var receivedRawValue = mask.getRawValue(received) | ||
|
||
expect(received).toBe(expected); | ||
expect(receivedRawValue).toBe(expectedRawValue); | ||
}); | ||
expect(received).toBe(expected) | ||
expect(receivedRawValue).toBe(expectedRawValue) | ||
}) | ||
|
||
test('empty cnpj is invalid', () => { | ||
var mask = new CnpjMask() | ||
var received = mask.validate('', {}) | ||
|
||
expect(received).toBeFalsy() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,73 @@ | ||
import { CpfMask } from '../lib/masks'; | ||
import { CpfMask } from '../lib/masks' | ||
|
||
test('getType results cpf', () => { | ||
var expected = 'cpf'; | ||
var received = CpfMask.getType(); | ||
var expected = 'cpf' | ||
var received = CpfMask.getType() | ||
|
||
expect(received).toBe(expected); | ||
}); | ||
expect(received).toBe(expected) | ||
}) | ||
|
||
test('12312312356 results 123.123.123-56', () => { | ||
var mask = new CpfMask(); | ||
var expected = '123.123.123-56'; | ||
var received = mask.getValue('12312312356'); | ||
var mask = new CpfMask() | ||
var expected = '123.123.123-56' | ||
var received = mask.getValue('12312312356') | ||
|
||
expect(received).toBe(expected); | ||
}); | ||
expect(received).toBe(expected) | ||
}) | ||
|
||
test('123123 results 123.123', () => { | ||
var mask = new CpfMask(); | ||
var expected = '123.123'; | ||
var received = mask.getValue('123123'); | ||
var mask = new CpfMask() | ||
var expected = '123.123' | ||
var received = mask.getValue('123123') | ||
|
||
expect(received).toBe(expected); | ||
}); | ||
expect(received).toBe(expected) | ||
}) | ||
|
||
test('07833823678 results 078.338.236-78 and is valid', () => { | ||
var mask = new CpfMask(); | ||
var expected = '078.338.236-78'; | ||
var received = mask.getValue('07833823678'); | ||
var isValid = mask.validate(received); | ||
var mask = new CpfMask() | ||
var expected = '078.338.236-78' | ||
var received = mask.getValue('07833823678') | ||
var isValid = mask.validate(received) | ||
|
||
expect(received).toBe(expected); | ||
expect(isValid).toBe(true); | ||
}); | ||
expect(received).toBe(expected) | ||
expect(isValid).toBe(true) | ||
}) | ||
|
||
test('11111111111 results 111.111.111-11 and is not valid', () => { | ||
var mask = new CpfMask(); | ||
var expected = '111.111.111-11'; | ||
var received = mask.getValue('11111111111'); | ||
var isValid = mask.validate(received); | ||
var mask = new CpfMask() | ||
var expected = '111.111.111-11' | ||
var received = mask.getValue('11111111111') | ||
var isValid = mask.validate(received) | ||
|
||
expect(received).toBe(expected); | ||
expect(isValid).toBe(false); | ||
}); | ||
expect(received).toBe(expected) | ||
expect(isValid).toBe(false) | ||
}) | ||
|
||
test('1234567890 results 123.456.789-0 and is not valid', () => { | ||
var mask = new CpfMask(); | ||
var expected = '123.456.789-0'; | ||
var received = mask.getValue('1234567890'); | ||
var isValid = mask.validate(received); | ||
var mask = new CpfMask() | ||
var expected = '123.456.789-0' | ||
var received = mask.getValue('1234567890') | ||
var isValid = mask.validate(received) | ||
|
||
expect(received).toBe(expected); | ||
expect(isValid).toBe(false); | ||
}); | ||
expect(received).toBe(expected) | ||
expect(isValid).toBe(false) | ||
}) | ||
|
||
test('12312312356 results 123.123.123-56 and raw value 12312312356', () => { | ||
var mask = new CpfMask(); | ||
var expected = '123.123.123-56'; | ||
var received = mask.getValue('12312312356'); | ||
var mask = new CpfMask() | ||
var expected = '123.123.123-56' | ||
var received = mask.getValue('12312312356') | ||
|
||
var expectedRawValue = '12312312356'; | ||
var receivedRawValue = mask.getRawValue(received); | ||
var expectedRawValue = '12312312356' | ||
var receivedRawValue = mask.getRawValue(received) | ||
|
||
expect(received).toBe(expected); | ||
expect(receivedRawValue).toBe(expectedRawValue); | ||
}); | ||
expect(received).toBe(expected) | ||
expect(receivedRawValue).toBe(expectedRawValue) | ||
}) | ||
|
||
test('empty cpf is invalid', () => { | ||
var mask = new CpfMask() | ||
var received = mask.validate('', {}) | ||
|
||
expect(received).toBeFalsy() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,42 @@ | ||
import BaseMask from './_base.mask'; | ||
import BaseMask from './_base.mask' | ||
|
||
const CNPJ_MASK = '99.999.999/9999-99'; | ||
const CNPJ_MASK = '99.999.999/9999-99' | ||
|
||
const validateCnpj = cnpj => { | ||
var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2); | ||
var dig1= new Number; | ||
var dig2= new Number; | ||
var i = 0; | ||
|
||
var exp = /\.|\-|\//g; | ||
cnpj = cnpj.toString().replace( exp, "" ); | ||
var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13))); | ||
|
||
for(i = 0; i<valida.length; i++){ | ||
dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0); | ||
dig2 += cnpj.charAt(i)*valida[i]; | ||
} | ||
dig1 = (((dig1%11)<2)? 0:(11-(dig1%11))); | ||
dig2 = (((dig2%11)<2)? 0:(11-(dig2%11))); | ||
|
||
return (((dig1*10)+dig2) == digito); | ||
var valida = new Array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2) | ||
var dig1 = new Number() | ||
var dig2 = new Number() | ||
var i = 0 | ||
|
||
var exp = /\.|\-|\//g | ||
cnpj = cnpj.toString().replace(exp, '') | ||
var digito = new Number(eval(cnpj.charAt(12) + cnpj.charAt(13))) | ||
|
||
for (i = 0; i < valida.length; i++) { | ||
dig1 += i > 0 ? cnpj.charAt(i - 1) * valida[i] : 0 | ||
dig2 += cnpj.charAt(i) * valida[i] | ||
} | ||
dig1 = dig1 % 11 < 2 ? 0 : 11 - dig1 % 11 | ||
dig2 = dig2 % 11 < 2 ? 0 : 11 - dig2 % 11 | ||
|
||
return dig1 * 10 + dig2 == digito | ||
} | ||
|
||
export default class CnpjMask extends BaseMask { | ||
static getType() { | ||
return 'cnpj'; | ||
} | ||
static getType() { | ||
return 'cnpj' | ||
} | ||
|
||
getValue(value, settings) { | ||
return this.getVMasker().toPattern(value, CNPJ_MASK); | ||
} | ||
getValue(value, settings) { | ||
return this.getVMasker().toPattern(value, CNPJ_MASK) | ||
} | ||
|
||
getRawValue(maskedValue, settings) { | ||
return super.removeNotNumbers(maskedValue); | ||
getRawValue(maskedValue, settings) { | ||
return super.removeNotNumbers(maskedValue) | ||
} | ||
|
||
validate(value, settings) { | ||
return validateCnpj(value); | ||
} | ||
} | ||
validate(value, settings) { | ||
var isEmpty = (value || '').trim().length === 0 | ||
return !isEmpty && validateCnpj(value) | ||
} | ||
} |
Oops, something went wrong.