Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add es/nss #58

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
release:
name: Release
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.test }}


steps:
- name: Checkout
Expand All @@ -42,3 +45,22 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
run: npx semantic-release
- name: Get Version
id: version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
run: echo "version=$(npx semantic-release --version)" >> "$GITHUB_OUTPUT"
- name: GitHub Release update
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
# body_path: ${{ github.workspace }}-CHANGELOG.txt
# note you'll typically need to create a personal access token
# with permissions to create releases in the other repoj
#token: ${{ secrets.CUSTOM_GITHUB_TOKEN }}
files: |
LICENSE.md
README.md
lib
package.json
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ All country validators are in the "namespace" of the ISO country code.
| Spain | ES | DNI | Person | Identity code (Documento Nacional de Identidad) |
| Spain | ES | NIE | Person | Identity code foreigner (Número de Identificación de Extranjero) |
| Spain | ES | NIF | Tax | Tax Identifier (Número de Identificación Fiscal) |
| Spain | ES | NSS | Person | El número de Seguridad Social, Social Security Number |
| Uruguay | UY | RUT | Tax/Vat | Tax Identifier (Registro Único Tributario) |
| Uruguay | UY | CEDULA | Person | Person Identifier (Cédula de Residencia) |
| Uruguay | UY | NIE | Person | ForeignersI identification Number |
Expand Down
12 changes: 8 additions & 4 deletions src/es/dni.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* de Extranjeros, Foreigner's Identity Number) instead.
*
* Sources:
* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/SPAIN-TIN.pdf
*
* PERSON
*/
Expand Down Expand Up @@ -58,11 +59,14 @@ const impl: Validator = {

const [body, check] = strings.splitAt(value, 8);

if (!strings.isdigits(body)) {
if ('KLM'.includes(body[0]) && strings.isdigits(body.substring(1))) {
// Currently no test data for these cases, so
// we're assuming they're good based on format
} else if (!strings.isdigits(body)) {
// Not all digits in the body, it's not valid
return { isValid: false, error: new exceptions.InvalidComponent() };
}

if (calcCheckDigit(body) !== check) {
} else if (calcCheckDigit(body) !== check) {
// Check the checksum on a non-[KLM] person
return { isValid: false, error: new exceptions.InvalidChecksum() };
}

Expand Down
1 change: 1 addition & 0 deletions src/es/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * as cif from './cif';
export * as dni from './dni';
export * as nie from './nie';
export * as nif from './nif';
export * as nss from './nss';
28 changes: 28 additions & 0 deletions src/es/nss.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { validate, format } from './nss';
import { InvalidLength, InvalidComponent } from '../exceptions';

describe('es/nss', () => {
it('format:281234567840', () => {
const result = format('281234567840');

expect('28-1234567840').toEqual(result);
});

it.each(['281234567840'])('validate:%s', (value: string) => {
const result = validate(value);

expect(value).toEqual(result.isValid && result.compact);
});

it('validate:77 12345678 40', () => {
const result = validate('77 12345678 40');

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

it('validate:28 12345678 4', () => {
const result = validate('28 12345678 4');

expect(result.error).toBeInstanceOf(InvalidLength);
});
});
132 changes: 132 additions & 0 deletions src/es/nss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* NSS (El número de Seguridad Social, Social Security Number).
*
* The SSN is a tax identification number for individuals entities. It has 12 digits
* where where the first 2 digits indicate province, followed by 8 digits and
* a 2 digit checksum
*
* Sources:
* https://cis.ier.hit-u.ac.jp/English/society/conference1001/delgado-paper.pdf
* https://www.grupoalquerque.es/ferias/2012/archivos/digitos/codigo_seguridad_social.pdf
*
* TAX/PERSON
*/

import * as exceptions from '../exceptions';
import { strings } from '../util';
import { Validator, ValidateReturn } from '../types';

const PROVINCES = {
ÁLAVA: '01',
ALBACETE: '02',
ALICANTE: '03',
ALMERÍA: '04',
ASTURIAS: '33',
ÁVILA: '05',
BADAJOZ: '06',
BALEARES: '07',
BARCELONA: '08',
BURGOS: '09',
CÁCERES: '10',
CÁDIZ: '11',
CANTABRIA: '39',
CASTELLÓN: '12',
'CIUDAD REAL': '13',
CÓRDOBA: '14',
CORUÑA: '15',
CUENCA: '16',
GERONA: '17',
GRANADA: '18',
GUADALAJARA: '19',
GUIPÚZCOA: '20',
HUELVA: '21',
HUESCA: '22',
JAÉN: '23',
LEÓN: '24',
LÉRIDA: '25',
LUGO: '27',
MADRID: '28',
MÁLAGA: '29',
MURCIA: '30',
NAVARRA: '31',
ORENSE: '32',
PALENCIA: '34',
'PALMAS (LAS)': '35',
PONTEVEDRA: '36',
'RIOJA (LA)': '26',
SALAMANCA: '37',
'SANTA CRUZ DE TENERIFE': '38',
SEGOVIA: '40',
SEVILLA: '41',
SORIA: '42',
TARRAGONA: '43',
TERUEL: '44',
TOLEDO: '45',
VALENCIA: '46',
VALLADOLID: '47',
VIZCAYA: '48',
ZAMORA: '49',
ZARAGOZA: '50',
'OTROS TERRITORIOS': '53',
EXTRANJERO: '66',
};
const VALID_PROVINCES = Object.values(PROVINCES);

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return strings.cleanUnicode(input, ' -/');
}

const impl: Validator = {
name: 'Spanish Social Security Number',
localName: 'Número de Seguridad Social',
abbreviation: 'NSS',
compact(input: string): string {
const [value, err] = clean(input);

if (err) {
throw err;
}

return value;
},

format(input: string): string {
const [value] = clean(input);

return strings.splitAt(value, 2).join('-');
},

validate(input: string): ValidateReturn {
const [value, error] = clean(input);

if (error) {
return { isValid: false, error };
}
if (value.length !== 12) {
return { isValid: false, error: new exceptions.InvalidLength() };
}

if (!strings.isdigits(value)) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}

const [province, body, check] = strings.splitAt(value, 2, 10);
if (!VALID_PROVINCES.includes(province)) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}

if (parseInt(province + body, 10) % 97 !== parseInt(check, 10)) {
return { isValid: false, error: new exceptions.InvalidChecksum() };
}

return {
isValid: true,
compact: value,
isIndividual: true,
isCompany: false,
};
},
};

export const { name, localName, abbreviation, validate, format, compact } =
impl;