1.0.2 • Published 8 months ago

spanish-ssn v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

spanish-ssn

Spanish Social Security Number validation library

Instalation

> npm install spanish-ssn
or
> yarn add spanish-ssn

Definitions

isValidSsn: (str: string) => boolean;

validateSsn: (str: string) => SsnValidationResult;

type SsnValidationResult = {
  isValid: boolean;

  //if isValid === false
  errorCode: SsnValidationErrorCode;

  //if isValid === true
  provinceCode: string;
  province: string;
  number: string;
  controlDigits: string;
};

enum SsnValidationErrorCode {
  INVALID_LENGTH,
  INVALID_CHAR,
  INVALID_PROVINCE_CODE,
  INVALID_CONTROL_DIGITS
}

Usage

import { isValidSsn, validateSsn } from 'spanish-ssn';

const ok1 = isValidSsn('770999999969'); // returns false
const ok2 = isValidSsn('280999999969'); // returns true

const result1 = validateSsn('770999999969');
/* returns
{
  isValid: false,
  errorCode: 'INVALID_PROVINCE_CODE'
}
*/

const result2 = validateSsn('280999999969');
/* returns
{
  isValid: true,
  provinceCode: '28'
  province: 'Madrid'
  number: '2809999999'
  controlDigits: '69'
}
*/

Disclaimer

I have not found any official place where it is well explained how social security numbers are formed, this lib is the product of research on various online sites (ie: www.migoia.com). I am not responsible for any malfunction. PRs are welcome.

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago