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

Add new validators #55

Merged
merged 2 commits into from
Mar 14, 2024
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
28 changes: 28 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ then it is checked if expiration date is later than release date. Date in format
* [.isIbanValidator](#module_General Validators.isIbanValidator) ⇒ <code>boolean</code>
* [.isIbanLengthValidator](#module_General Validators.isIbanLengthValidator) ⇒ <code>boolean</code>
* [.isLanNumberValidator](#module_General Validators.isLanNumberValidator) ⇒ <code>boolean</code>
* [.isRoutingCodeValidator](#module_General Validators.isRoutingCodeValidator) ⇒ <code>boolean</code>
* [.isTaxNumberValidator](#module_General Validators.isTaxNumberValidator) ⇒ <code>boolean</code>
* [.isSwiftValidator](#module_General Validators.isSwiftValidator) ⇒ <code>boolean</code>
* [.isSwiftCountryValidator](#module_General Validators.isSwiftCountryValidator) ⇒ <code>boolean</code>
* [.isIdNumberValidator](#module_General Validators.isIdNumberValidator) ⇒ <code>boolean</code>
Expand Down Expand Up @@ -395,6 +397,32 @@ then it is checked if expiration date is later than release date. Date in format
| Param | Type | Description |
| --- | --- | --- |
| value | <code>string</code> | <p>value to check.</p> |
| [country] | <code>string</code> | <p>optional country to check.</p> |

<a name="module_General Validators.isRoutingCodeValidator"></a>

### General Validators.isRoutingCodeValidator ⇒ <code>boolean</code>
<p>Checks if value is valid routing code</p>

**Kind**: static property of [<code>General Validators</code>](#module_General Validators)

| Param | Type | Description |
| --- | --- | --- |
| value | <code>string</code> | <p>value to check.</p> |
| [country] | <code>string</code> | <p>optional country to check.</p> |

<a name="module_General Validators.isTaxNumberValidator"></a>

### General Validators.isTaxNumberValidator ⇒ <code>boolean</code>
<p>Checks if value is valid tax number</p>

**Kind**: static property of [<code>General Validators</code>](#module_General Validators)

| Param | Type | Description |
| --- | --- | --- |
| value | <code>string</code> | <p>value to check.</p> |
| [country] | <code>string</code> | <p>optional country to check.</p> |
| [isCompany] | <code>boolean</code> | <p>optional isCompany to check.</p> |

<a name="module_General Validators.isSwiftValidator"></a>

Expand Down
43 changes: 43 additions & 0 deletions lib/main/general-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,65 @@ 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)
}
/**
* 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}
*/

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 18 additions & 0 deletions test/general-validators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
isSwiftCountryValidator,
isSwiftValidator,
isValueValidator,
isRoutingCodeValidator,
isTaxNumberValidator,
} from '../lib'
import { ibanHelper } from '../lib/helpers/iban-helper'

Expand Down Expand Up @@ -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()
Expand Down
Loading