Skip to content

Commit

Permalink
fix(validation): fixing validation when cpf or cnpj is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-hur Santos Ott committed Dec 12, 2017
1 parent b486262 commit 56de29c
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 389 deletions.
322 changes: 167 additions & 155 deletions README.md

Large diffs are not rendered by default.

95 changes: 51 additions & 44 deletions __tests__/cnpj.mask.test.js
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()
})
95 changes: 51 additions & 44 deletions __tests__/cpf.mask.test.js
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()
})
30 changes: 20 additions & 10 deletions dist/lib/masks/money.mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ MoneyMask=function(_BaseMask){_inherits(MoneyMask,_BaseMask);function MoneyMask(
value,settings,oldValue){
var mergedSettings=_get(MoneyMask.prototype.__proto__||Object.getPrototypeOf(MoneyMask.prototype),'mergeSettings',this).call(this,MONEY_MASK_SETTINGS,settings);

if(mergedSettings.suffixUnit&&oldValue&&value){
var sanitized=this._sanitize(value,mergedSettings);



if(value.length==oldValue.length-1){
var cleared=this.removeNotNumbers(value);
value=cleared.substr(0,cleared.length-1);
if(mergedSettings.suffixUnit&&oldValue&&sanitized){
if(sanitized.length==oldValue.length-1){
var cleared=this.removeNotNumbers(sanitized);
sanitized=cleared.substr(0,cleared.length-1);
}
}

var masked=this.getVMasker().toMoney(value,mergedSettings);
var masked=this.getVMasker().toMoney(sanitized,mergedSettings);

return masked;
}},{key:'getRawValue',value:function getRawValue(
Expand All @@ -44,13 +43,24 @@ return Number(normalized);

value,settings){
return true;
}},{key:'_sanitize',value:function _sanitize(

value,settings){
if(typeof value==='number'){
return value.toFixed(settings.precision);
}

return value;
}},{key:'_insert',value:function _insert(

text,index,string){
if(index>0){
return text.substring(0,index)+string+text.substring(index,text.length);
}else
{
return(
text.substring(0,index)+
string+
text.substring(index,text.length));

}else{
return string+text;
}
}}],[{key:'getType',value:function getType(){return'money';}}]);return MoneyMask;}(_base2.default);exports.default=MoneyMask;
63 changes: 32 additions & 31 deletions lib/masks/cnpj.mask.js
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)
}
}
Loading

0 comments on commit 56de29c

Please sign in to comment.