Skip to content

Commit

Permalink
Merge pull request #69 from koblas/ro_cnp
Browse files Browse the repository at this point in the history
fix: improved the RO/CNP validator with regional info
  • Loading branch information
koblas authored Jul 4, 2023
2 parents 7b62c1f + e8e3c50 commit 9c1cb9b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/ro/cnp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe('ro/cnp', () => {
expect(result.error).toBeInstanceOf(InvalidComponent);
});

it('validate:0800101228842', () => {
const result = validate('0800101228842');

expect(result.error).toBeInstanceOf(InvalidComponent);
});

it('validate:1632215123457', () => {
const result = validate('1632215123457');

Expand Down
65 changes: 60 additions & 5 deletions src/ro/cnp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,61 @@ const century = {
'9': '19',
};

function checkDate(value: string): boolean {
const [first, dvalue] = strings.splitAt(value, 1, 7);
// The Romanian counties
const COUNTIES = {
'01': 'Alba',
'02': 'Arad',
'03': 'Arges',
'04': 'Bacau',
'05': 'Bihor',
'06': 'Bistrita-Nasaud',
'07': 'Botosani',
'08': 'Brasov',
'09': 'Braila',
'10': 'Buzau',
'11': 'Caras-Severin',
'12': 'Cluj',
'13': 'Constanta',
'14': 'Covasna',
'15': 'Dambovita',
'16': 'Dolj',
'17': 'Galati',
'18': 'Gorj',
'19': 'Harghita',
'20': 'Hunedoara',
'21': 'Ialomita',
'22': 'Iasi',
'23': 'Ilfov',
'24': 'Maramures',
'25': 'Mehedinti',
'26': 'Mures',
'27': 'Neamt',
'28': 'Olt',
'29': 'Prahova',
'30': 'Satu Mare',
'31': 'Salaj',
'32': 'Sibiu',
'33': 'Suceava',
'34': 'Teleorman',
'35': 'Timis',
'36': 'Tulcea',
'37': 'Vaslui',
'38': 'Valcea',
'39': 'Vrancea',
'40': 'Bucuresti',
'41': 'Bucuresti - Sector 1',
'42': 'Bucuresti - Sector 2',
'43': 'Bucuresti - Sector 3',
'44': 'Bucuresti - Sector 4',
'45': 'Bucuresti - Sector 5',
'46': 'Bucuresti - Sector 6',
'47': 'Bucuresti - Sector 7 (desfiintat)',
'48': 'Bucuresti - Sector 8 (desfiintat)',
'51': 'Calarasi',
'52': 'Giurgiu',
};

return isValidDateCompactYYYYMMDD(`${century[first]}${dvalue}`);
}
const VALID_COUNTIES = Object.keys(COUNTIES);

const impl: Validator = {
name: 'Romanian Numerical Personal Code',
Expand Down Expand Up @@ -74,7 +124,12 @@ const impl: Validator = {
return { isValid: false, error: new exceptions.InvalidComponent() };
}

if (!checkDate(value)) {
const [first, dvalue, county] = strings.splitAt(value, 1, 7, 9);

if (!isValidDateCompactYYYYMMDD(`${century[first]}${dvalue}`)) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}
if (!VALID_COUNTIES.includes(county)) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}

Expand Down

0 comments on commit 9c1cb9b

Please sign in to comment.