Skip to content

Commit c446a94

Browse files
Merge pull request #2 from Santhosh-Kumar-99/FEATURE/taxID
Feature/tax
2 parents e7569d5 + 6c6b864 commit c446a94

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Validator | Description
164164
**isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`]
165165
**isStrongPassword(str [, options])** | Check if a password is strong or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.<br/>Default options: <br/>`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }`
166166
**isTime(str [, options])** | Check if the input is a valid time. e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFomat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format.
167-
**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-IN-GSTIN', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]`
167+
**isTaxID(str, locale, localeOption)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`<br/><br/>Specify localeOption if country has multiple Tax Identification Number.<br/><br/>Supported localeOption: `['GSTIN','PAN']` for `en-IN` locale.<br></br>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-IN-GSTIN', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]`
168168
**isURL(str [, options])** | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>require_protocol - if set as true isURL will return false if protocol is not present in the URL.<br/>require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.<br/>protocols - valid protocols can be modified with this option.<br/>require_host - if set as false isURL will not check if host is present in the URL.<br/>require_port - if set as true isURL will check if port is present in the URL.<br/>allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.<br/>allow_fragments - if set as false isURL will return false if fragments are present.<br/>allow_query_components - if set as false isURL will return false if query components are present.<br/>validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length).
169169
**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5).
170170
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.

src/lib/isTaxID.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,10 @@ const taxIdFormat = {
11361136
'en-CA': /^\d{9}$/,
11371137
'en-GB': /^\d{10}$|^(?!GB|NK|TN|ZZ)(?![DFIQUV])[A-Z](?![DFIQUVO])[A-Z]\d{6}[ABCD ]$/i,
11381138
'en-IE': /^\d{7}[A-W][A-IW]{0,1}$/i,
1139-
'en-IN-GSTIN': /^((?!39)(?!00)[0-3][0-9]|97)([A-Z]{3}[ABCFGHLJPT][A-Z](?!0000)[0-9]{4}[A-Z])[1-9A-Z]Z[0-9 A-Z]$/,
1139+
'en-IN': {
1140+
GSTIN: /^((?!39)(?!00)[0-3][0-9]|97)([A-Z]{3}[ABCFGHLJPT][A-Z](?!0000)[0-9]{4}[A-Z])[1-9A-Z]Z[0-9 A-Z]$/,
1141+
PAN: /^[A-Z]{3}[ABCFGHLJPT][A-Z](?!0000)[0-9]{4}[A-Z]$/,
1142+
},
11401143
'en-US': /^\d{2}[- ]{0,1}\d{7}$/,
11411144
'es-ES': /^(\d{0,8}|[XYZKLM]\d{7})[A-HJ-NP-TV-Z]$/i,
11421145
'et-EE': /^[1-6]\d{6}(00[1-9]|0[1-9][0-9]|[1-6][0-9]{2}|70[0-9]|710)\d$/,
@@ -1212,13 +1215,21 @@ const sanitizeRegexes = {
12121215
// sanitizeRegexes locale aliases
12131216
sanitizeRegexes['nl-BE'] = sanitizeRegexes['fr-BE'];
12141217

1218+
// If locale has multiple TIN
1219+
const multipleTinLocale = {
1220+
'en-IN': {
1221+
GSTIN: true,
1222+
PAN: true,
1223+
},
1224+
};
1225+
12151226
/*
12161227
* Validator function
12171228
* Return true if the passed string is a valid tax identification number
12181229
* for the specified locale.
12191230
* Throw an error exception if the locale is not supported.
12201231
*/
1221-
export default function isTaxID(str, locale = 'en-US') {
1232+
export default function isTaxID(str, locale = 'en-US', localeOption) {
12221233
assertString(str);
12231234
// Copy TIN to avoid replacement if sanitized
12241235
let strcopy = str.slice(0);
@@ -1227,15 +1238,30 @@ export default function isTaxID(str, locale = 'en-US') {
12271238
if (locale in sanitizeRegexes) {
12281239
strcopy = strcopy.replace(sanitizeRegexes[locale], '');
12291240
}
1230-
if (!taxIdFormat[locale].test(strcopy)) {
1231-
return false;
1232-
}
12331241

1234-
if (locale in taxIdCheck) {
1235-
return taxIdCheck[locale](strcopy);
1242+
// Check if locale has multiple TIN
1243+
if (locale in multipleTinLocale) {
1244+
if (!localeOption) throw new Error(`Locale requires localeOption ${localeOption}`);
1245+
1246+
if (!(multipleTinLocale[locale][localeOption])) throw new Error(`Locale Option ${localeOption} not available for specified locale ${locale}`);
1247+
1248+
if (!(taxIdFormat[locale][localeOption].test(strcopy))) return false;
1249+
1250+
if (locale in taxIdCheck) {
1251+
return taxIdCheck[locale][localeOption](strcopy);
1252+
}
1253+
} else {
1254+
if (localeOption || localeOption === '') throw new Error(`Locale ${locale} do not have localeOption '${localeOption}'`);
1255+
1256+
if (!taxIdFormat[locale].test(strcopy)) {
1257+
return false;
1258+
}
1259+
if (locale in taxIdCheck) {
1260+
return taxIdCheck[locale](strcopy);
1261+
}
12361262
}
1237-
// Fallthrough; not all locales have algorithmic checks
12381263
return true;
12391264
}
1265+
12401266
throw new Error(`Invalid locale '${locale}'`);
12411267
}

test/validators.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(
99
function test(options) {
1010
let args = options.args || [];
1111
args.unshift(null);
12+
console.log(args);
1213
if (options.error) {
1314
options.error.forEach((error) => {
1415
args[0] = error;
@@ -12090,15 +12091,38 @@ describe('Validators', () => {
1209012091
});
1209112092
test({
1209212093
validator: 'isTaxID',
12093-
args: ['en-IN-GSTIN'],
12094+
args: ['en-IN', 'GSTIN'],
1209412095
valid: [
1209512096
'27AAPFU0939F1ZV',
1209612097
'27AASCS2460H1Z0',
1209712098
'29AAGCB7383J1Z4'],
1209812099
invalid: [
12100+
'',
1209912101
'00AAGCB0000J1Z4',
1210012102
'29AAGCB7383J114',
12101-
'88AAPFU093921ZV'],
12103+
'88AAPFU093921ZV',
12104+
'**AAGCB7689J1Z4',
12105+
'S AAGCB7893J1Z4'],
12106+
});
12107+
test({
12108+
validator: 'isTaxID',
12109+
args: ['en-IN', 'PAN'],
12110+
valid: [
12111+
'MFWAA0001A',
12112+
'ABPPA2020K',
12113+
'KLSPK0909Z',
12114+
'ACSLY4499S',
12115+
'ABPPA2020K'],
12116+
invalid: [
12117+
'',
12118+
'123PA0001A',
12119+
'ACSZA2020K',
12120+
'ACSP19090K',
12121+
'ACSPA0000K',
12122+
'ACSYA44999',
12123+
'ACS A4499A',
12124+
'*#$PA4499J',
12125+
'ACSLY4499SK'],
1210212126
});
1210312127
test({
1210412128
validator: 'isTaxID',

0 commit comments

Comments
 (0)