From bc95d4a3e6b22ce64a76f9278d640113a08df93b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Jarmu=C5=82a?= Date: Thu, 14 Mar 2024 09:23:29 +0100 Subject: [PATCH 1/2] Add isRoutingCodeValidator & isTaxNumberValidator & update isLanNumberValidator --- DOCS.md | 28 +++++++++++++++++++++ lib/main/general-validators.ts | 43 +++++++++++++++++++++++++++++++++ test/general-validators.spec.ts | 18 ++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/DOCS.md b/DOCS.md index fa46dab..7304d8f 100644 --- a/DOCS.md +++ b/DOCS.md @@ -134,6 +134,8 @@ then it is checked if expiration date is later than release date. Date in format * [.isIbanValidator](#module_General Validators.isIbanValidator) ⇒ boolean * [.isIbanLengthValidator](#module_General Validators.isIbanLengthValidator) ⇒ boolean * [.isLanNumberValidator](#module_General Validators.isLanNumberValidator) ⇒ boolean + * [.isRoutingCodeValidator](#module_General Validators.isRoutingCodeValidator) ⇒ boolean + * [.isTaxNumberValidator](#module_General Validators.isTaxNumberValidator) ⇒ boolean * [.isSwiftValidator](#module_General Validators.isSwiftValidator) ⇒ boolean * [.isSwiftCountryValidator](#module_General Validators.isSwiftCountryValidator) ⇒ boolean * [.isIdNumberValidator](#module_General Validators.isIdNumberValidator) ⇒ boolean @@ -395,6 +397,32 @@ then it is checked if expiration date is later than release date. Date in format | Param | Type | Description | | --- | --- | --- | | value | string |

value to check.

| +| [country] | string |

optional country to check.

| + + + +### General Validators.isRoutingCodeValidator ⇒ boolean +

Checks if value is valid routing code

+ +**Kind**: static property of [General Validators](#module_General Validators) + +| Param | Type | Description | +| --- | --- | --- | +| value | string |

value to check.

| +| [country] | string |

optional country to check.

| + + + +### General Validators.isTaxNumberValidator ⇒ boolean +

Checks if value is valid tax number

+ +**Kind**: static property of [General Validators](#module_General Validators) + +| Param | Type | Description | +| --- | --- | --- | +| value | string |

value to check.

| +| [country] | string |

optional country to check.

| +| [isCompany] | boolean |

optional isCompany to check.

| diff --git a/lib/main/general-validators.ts b/lib/main/general-validators.ts index 9cc05ca..42fa6ea 100644 --- a/lib/main/general-validators.ts +++ b/lib/main/general-validators.ts @@ -205,15 +205,20 @@ export const isIbanLengthValidator = (value: string, countryCode: string): boole export const isLanNumberValidator = (value: string, country?: string): boolean => { const lanCountriesRules = { + AR: /^[0-9]{22}$/, AU: /^[A-Za-z0-9]{6,30}$/, + BR: /^[0-9]{2,15}$/, CA: /^[A-Za-z0-9]{7,30}$/, CN: /^(?:OSA|NRA|FTN)?[A-Z0-9]{1,22}$/i, JP: /^[A-Za-z0-9]{7,14}$/, KR: /^[A-Za-z0-9]{11,16}$/, + MX: /^[0-9]{18}$/, NZ: /^[A-Za-z0-9]{15,16}$/, + PE: /^[0-9]{20}$/, SG: /^[A-Za-z0-9]{1,14}$/, TW: /^[A-Za-z0-9]{1,22}$/, US: /^[A-Za-z0-9]{9,30}$/, + UY: /^[0-9]{7,14}$/, } const defaultRule = /^[A-Za-z0-9]{1,22}$/ return !!value.replace(/ /g, '').match(lanCountriesRules[country] ? lanCountriesRules[country] : defaultRule) @@ -221,6 +226,44 @@ export const isLanNumberValidator = (value: string, country?: string): boolean = /** * Checks if value is valid non-iban account number * @param {string} value - value to check. + * @param {string} [country] - optional country to check. + * @returns {boolean} + */ + +export const isRoutingCodeValidator = (value: string, country?: string): boolean => { + const countries = { + BR: /^[0-9]{3}[0-9]{4,5}$/, + CL: /^\d{3}$/, + CO: /^(\d{2}|\d{5})$/, + PE: /^[0-9]{3}$/, + UY: /^[0-9]{4}$/, + } + return !countries[country] || !!value.replace(/ /g, '').match(countries[country]) +} +/** + * Checks if value is valid routing code + * @param {string} value - value to check. + * @param {string} [country] - optional country to check. + * @returns {boolean} + */ + +export const isTaxNumberValidator = (value: string, country?: string, isCompany?: boolean): boolean => { + const countries = { + AR: /^[0-9]{2}-?[0-9]{8}-?[0-9]{1}$/, + BR: !isCompany ? /^[0-9]{3}[.]?[0-9]{3}[.]?[0-9]{3}[-]?[0-9]{2}$/ : /^[0-9]{2}[.]?[0-9]{3}[.]?[0-9]{3}[/]?[0-9]{4}[-]?[0-9]{2}$/, + CL: /^(\d{1,3}(?:\.\d{1,3}){2}-[\dkK])$/, + CO: /^\d{9}$/i, + MX: /^[A-Za-z]{3,4}[ |\\-]{0,1}[0-9]{6}[ |\\-]{0,1}([0-9A-Za-z]{3})?$/, + PE: /^[0-9]{6,9}$|^[0-9]{8}$|^[0-9]{11}$/, + UY: /(^.{8,12}$)|(^[0-9]{2}[-]?[0-9]{6}[-]?[0-9]{3}[-]?[0-9]{1}$)|(^[0-9]{1}[\\.]?[0-9]{3}[\\.]?[0-9]{3}[-]?[0-9]$)/, + } + return !countries[country] || !!value.replace(/ /g, '').match(countries[country]) +} +/** + * Checks if value is valid tax number + * @param {string} value - value to check. + * @param {string} [country] - optional country to check. + * @param {boolean} [isCompany] - optional isCompany to check. * @returns {boolean} */ diff --git a/test/general-validators.spec.ts b/test/general-validators.spec.ts index 5da1508..75a433e 100644 --- a/test/general-validators.spec.ts +++ b/test/general-validators.spec.ts @@ -26,6 +26,8 @@ import { isSwiftCountryValidator, isSwiftValidator, isValueValidator, + isRoutingCodeValidator, + isTaxNumberValidator, } from '../lib' import { ibanHelper } from '../lib/helpers/iban-helper' @@ -342,6 +344,22 @@ describe('Validators', () => { }) }) + it('should isRoutingCodeValidator() validate value correctly', () => { + expect(isRoutingCodeValidator('123', 'CL')).toBeTruthy() + expect(isRoutingCodeValidator('12', 'CO')).toBeTruthy() + expect(isRoutingCodeValidator('12345', 'CO')).toBeTruthy() + expect(isRoutingCodeValidator('123', 'CO')).toBeFalsy() + expect(isRoutingCodeValidator('123')).toBeTruthy() + }) + + it('should isTaxNumberValidator() validate value correctly', () => { + expect(isTaxNumberValidator('123456789', 'CO')).toBeTruthy() + expect(isTaxNumberValidator('12-12345678-1', 'AR')).toBeTruthy() + expect(isTaxNumberValidator('123.123.123-12', 'BR')).toBeTruthy() + expect(isTaxNumberValidator('12.123.123/1234-12', 'BR', true)).toBeTruthy() + expect(isTaxNumberValidator('123')).toBeTruthy() + }) + it('should isSwiftValidator() validate value correctly', () => { expect(isSwiftValidator('Testzz01')).toBeTruthy() expect(isSwiftValidator('Testzz23XXX')).toBeTruthy() From 35ab1a0904defb4650f23297eb026fa08ec76c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Jarmu=C5=82a?= Date: Thu, 14 Mar 2024 09:25:59 +0100 Subject: [PATCH 2/2] 1.1.24 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index af46d08..15e4302 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@currency-one/validators", - "version": "1.1.23", + "version": "1.1.24", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@currency-one/validators", - "version": "1.1.23", + "version": "1.1.24", "license": "MIT", "devDependencies": { "@babel/cli": "^7.12.13", diff --git a/package.json b/package.json index 2650045..19ab20d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@currency-one/validators", - "version": "1.1.23", + "version": "1.1.24", "description": "Typescript collection of validators and regexp patterns", "main": "./index.js", "types": "./index.d.ts",