From 5202222bc9058a282aeb242d1ae588fcc455d648 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Thu, 16 Feb 2023 17:43:44 +0100 Subject: [PATCH] feat: update to support validator 13.9.0 (#1212) --- declarations/validator.d.ts | 18 +++- package-lock.json | 18 ++-- package.json | 4 +- src/chain/sanitizers-impl.spec.ts | 2 +- src/chain/validators-impl.ts | 21 ++-- src/chain/validators.ts | 11 +- src/options.ts | 164 +++++++++++++++++++++++++++++- 7 files changed, 211 insertions(+), 27 deletions(-) diff --git a/declarations/validator.d.ts b/declarations/validator.d.ts index bce628a0..78d6b240 100644 --- a/declarations/validator.d.ts +++ b/declarations/validator.d.ts @@ -5,7 +5,10 @@ declare module 'validator' { options?: import('../src/options').ContainsOptions, ): boolean; export function equals(str: string, comparison: string): boolean; - export function isAfter(str: string, date?: string): boolean; + export function isAfter( + str: string, + dateOrOptions?: string | import('../src/options').IsAfterOptions, + ): boolean; export function isAlpha( str: string, locale?: import('../src/options').AlphaLocale, @@ -17,7 +20,10 @@ declare module 'validator' { options?: import('../src/options').IsAlphanumericOptions, ): boolean; export function isAscii(str: string): boolean; - export function isBase32(str: string): boolean; + export function isBase32( + str: string, + options?: import('../src/options').IsBase32Options, + ): boolean; export function isBase58(str: string): boolean; export function isBase64( str: string, @@ -66,7 +72,10 @@ declare module 'validator' { export function isIMEI(str: string, options?: import('../src/options').IsIMEIOptions): boolean; export function isIP(str: string, version?: import('../src/options').IPVersion): boolean; export function isIPRange(str: string, version?: import('../src/options').IPVersion): boolean; - export function isISBN(str: string, version?: number): boolean; + export function isISBN( + str: string, + versionOrOptions?: number | import('../src/options').IsISBNOptions, + ): boolean; export function isISSN(str: string, options?: import('../src/options').IsISSNOptions): boolean; export function isISIN(str: string): boolean; export function isISO8601( @@ -78,6 +87,7 @@ declare module 'validator' { export function isISO4217(str: string): boolean; export function isISRC(str: string): boolean; export function isIn(str: string, values: readonly any[]): boolean; + export function isISO6391(str: string): boolean; export function isInt(str: string, options?: import('../src/options').IsIntOptions): boolean; export function isJSON(str: string, options?: import('../src/options').IsJSONOptions): boolean; export function isJWT(str: string): boolean; @@ -92,6 +102,7 @@ declare module 'validator' { ): boolean; export function isLocale(str: string): boolean; export function isLowercase(str: string): boolean; + export function isLuhnNumber(str: string): boolean; export function isMagnetURI(str: string): boolean; export function isMACAddress( str: string, @@ -132,6 +143,7 @@ declare module 'validator' { ): boolean; export function isSurrogatePair(str: string): boolean; export function isTaxID(str: string, locale: import('../src/options').TaxIDLocale): boolean; + export function isTime(str: string, options?: import('../src/options').IsTimeOptions): boolean; export function isURL(str: string, options?: import('../src/options').IsURLOptions): boolean; export function isUUID(str: string, version?: import('../src/options').UUIDVersion): boolean; export function isUppercase(str: string): boolean; diff --git a/package-lock.json b/package-lock.json index 4bd36922..1237259d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { "name": "express-validator", - "version": "6.14.3", + "version": "6.15.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "express-validator", - "version": "6.14.3", + "version": "6.15.0", "license": "MIT", "dependencies": { "lodash": "^4.17.21", - "validator": "^13.7.0" + "validator": "^13.9.0" }, "devDependencies": { "@docusaurus/core": "^2.2.0", @@ -21035,9 +21035,9 @@ } }, "node_modules/validator": { - "version": "13.7.0", - "resolved": "https://registry.npmjs.org/validator/-/validator-13.7.0.tgz", - "integrity": "sha512-nYXQLCBkpJ8X6ltALua9dRrZDHVYxjJ1wgskNt1lH9fzGjs3tgojGSCBjmEPwkWS1y29+DrizMTW19Pr9uB2nw==", + "version": "13.9.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.9.0.tgz", + "integrity": "sha512-B+dGG8U3fdtM0/aNK4/X8CXq/EcxU2WPrPEkJGslb47qyHsxmbggTWK0yEA4qnYVNF+nxNlN88o14hIcPmSIEA==", "engines": { "node": ">= 0.10" } @@ -37916,9 +37916,9 @@ } }, "validator": { - "version": "13.7.0", - "resolved": "https://registry.npmjs.org/validator/-/validator-13.7.0.tgz", - "integrity": "sha512-nYXQLCBkpJ8X6ltALua9dRrZDHVYxjJ1wgskNt1lH9fzGjs3tgojGSCBjmEPwkWS1y29+DrizMTW19Pr9uB2nw==" + "version": "13.9.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.9.0.tgz", + "integrity": "sha512-B+dGG8U3fdtM0/aNK4/X8CXq/EcxU2WPrPEkJGslb47qyHsxmbggTWK0yEA4qnYVNF+nxNlN88o14hIcPmSIEA==" }, "value-equal": { "version": "1.0.1", diff --git a/package.json b/package.json index a754cbed..1428822e 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "Gustavo Henke ", "Federico Ciardi " ], - "version": "6.14.3", + "version": "6.15.0", "homepage": "https://express-validator.github.io", "license": "MIT", "repository": { @@ -44,7 +44,7 @@ }, "dependencies": { "lodash": "^4.17.21", - "validator": "^13.7.0" + "validator": "^13.9.0" }, "devDependencies": { "@docusaurus/core": "^2.2.0", diff --git a/src/chain/sanitizers-impl.spec.ts b/src/chain/sanitizers-impl.spec.ts index 2ab8670b..d061748e 100644 --- a/src/chain/sanitizers-impl.spec.ts +++ b/src/chain/sanitizers-impl.spec.ts @@ -17,7 +17,7 @@ beforeEach(() => { sanitizers = new SanitizersImpl(builder, chain); }); -it('has methods for all standard validators', () => { +it('has methods for all standard sanitizers', () => { // Cast is here to workaround the lack of index signature const validatorModule = validator as any; diff --git a/src/chain/validators-impl.ts b/src/chain/validators-impl.ts index 929f7abe..c4e54e92 100644 --- a/src/chain/validators-impl.ts +++ b/src/chain/validators-impl.ts @@ -86,8 +86,8 @@ export class ValidatorsImpl implements Validators { equals(comparison: string) { return this.addStandardValidation(validator.equals, comparison); } - isAfter(date?: string) { - return this.addStandardValidation(validator.isAfter, date); + isAfter(dateOrOptions?: string | Options.IsAfterOptions) { + return this.addStandardValidation(validator.isAfter, dateOrOptions); } isAlpha(locale?: Options.AlphaLocale, options?: Options.IsAlphaOptions) { // TODO(v7): remove string[] support @@ -100,8 +100,8 @@ export class ValidatorsImpl implements Validators { isAscii() { return this.addStandardValidation(validator.isAscii); } - isBase32() { - return this.addStandardValidation(validator.isBase32); + isBase32(options?: Options.IsBase32Options) { + return this.addStandardValidation(validator.isBase32, options); } isBase58() { return this.addStandardValidation(validator.isBase58); @@ -203,8 +203,8 @@ export class ValidatorsImpl implements Validators { isIPRange(version?: Options.IPVersion) { return this.addStandardValidation(validator.isIPRange, version); } - isISBN(version?: number) { - return this.addStandardValidation(validator.isISBN, version); + isISBN(versionOrOptions?: number | Options.IsISBNOptions) { + return this.addStandardValidation(validator.isISBN, versionOrOptions); } isISSN(options?: Options.IsISSNOptions) { return this.addStandardValidation(validator.isISSN, options); @@ -212,6 +212,9 @@ export class ValidatorsImpl implements Validators { isISIN() { return this.addStandardValidation(validator.isISIN); } + isISO6391() { + return this.addStandardValidation(validator.isISO6391); + } isISO8601(options?: Options.IsISO8601Options) { return this.addStandardValidation(validator.isISO8601, options); } @@ -254,6 +257,9 @@ export class ValidatorsImpl implements Validators { isLowercase() { return this.addStandardValidation(validator.isLowercase); } + isLuhnNumber() { + return this.addStandardValidation(validator.isLuhnNumber); + } isMagnetURI() { return this.addStandardValidation(validator.isMagnetURI); } @@ -314,6 +320,9 @@ export class ValidatorsImpl implements Validators { isTaxID(locale: Options.TaxIDLocale) { return this.addStandardValidation(validator.isTaxID, locale); } + isTime(options?: Options.IsTimeOptions) { + return this.addStandardValidation(validator.isTime, options); + } isURL(options?: Options.IsURLOptions) { return this.addStandardValidation(validator.isURL, options); } diff --git a/src/chain/validators.ts b/src/chain/validators.ts index e99ee044..3e38f2d3 100644 --- a/src/chain/validators.ts +++ b/src/chain/validators.ts @@ -80,14 +80,14 @@ export interface Validators { // validator's validators contains(elem: any, options?: Options.ContainsOptions): Return; equals(comparison: string): Return; - isAfter(date?: string): Return; + isAfter(dateOrOptions?: string | Options.IsAfterOptions): Return; isAlpha(locale?: Options.AlphaLocale, options?: Options.IsAlphaOptions): Return; isAlphanumeric( locale?: Options.AlphanumericLocale, options?: Options.IsAlphanumericOptions, ): Return; isAscii(): Return; - isBase32(): Return; + isBase32(options?: Options.IsBase32Options): Return; isBase58(): Return; isBase64(options?: Options.IsBase64Options): Return; isBefore(date?: string): Return; @@ -95,7 +95,7 @@ export interface Validators { isBoolean(options?: Options.IsBooleanOptions): Return; isBtcAddress(): Return; isByteLength(options: Options.MinMaxExtendedOptions): Return; - isCreditCard(): Return; + isCreditCard(options?: Options.IsCreditCard): Return; isCurrency(options?: Options.IsCurrencyOptions): Return; isDataURI(): Return; isDate(options?: Options.IsDateOptions): Return; @@ -118,9 +118,10 @@ export interface Validators { isIMEI(options?: Options.IsIMEIOptions): Return; isIP(version?: Options.IPVersion): Return; isIPRange(version?: Options.IPVersion): Return; - isISBN(version?: number): Return; + isISBN(versionOrOptions?: number | Options.IsISBNOptions): Return; isISSN(options?: Options.IsISSNOptions): Return; isISIN(): Return; + isISO6391(): Return; isISO8601(options?: Options.IsISO8601Options): Return; isISO31661Alpha2(): Return; isISO31661Alpha3(): Return; @@ -135,6 +136,7 @@ export interface Validators { isLicensePlate(locale: Options.IsLicensePlateLocale): Return; isLocale(): Return; isLowercase(): Return; + isLuhnNumber(): Return; isMagnetURI(): Return; isMACAddress(options?: Options.IsMACAddressOptions): Return; isMD5(): Return; @@ -157,6 +159,7 @@ export interface Validators { isStrongPassword(options?: Options.IsStrongPasswordOptions): Return; isSurrogatePair(): Return; isTaxID(locale: Options.TaxIDLocale): Return; + isTime(options: Options.IsTimeOptions): Return; isURL(options?: Options.IsURLOptions): Return; isUUID(version?: Options.UUIDVersion): Return; isUppercase(): Return; diff --git a/src/options.ts b/src/options.ts index f723f804..8d9eccd8 100644 --- a/src/options.ts +++ b/src/options.ts @@ -23,6 +23,7 @@ export type AlphaLocale = | 'ar-YE' | 'az-AZ' | 'bg-BG' + | 'bn-BD' | 'cs-CZ' | 'da-DK' | 'de-DE' @@ -45,6 +46,8 @@ export type AlphaLocale = | 'hu-HU' | 'id-ID' | 'it-IT' + | 'ja-JP' + | 'ko-KR' | 'ku-IQ' | 'nb-NO' | 'nl-NL' @@ -53,6 +56,7 @@ export type AlphaLocale = | 'pt-BR' | 'pt-PT' | 'ru-RU' + | 'si-LK' | 'sk-SK' | 'sl-SI' | 'sr-RS' @@ -84,6 +88,7 @@ export type AlphanumericLocale = | 'ar-YE' | 'az-AZ' | 'bg-BG' + | 'bn-BD' | 'cs-CZ' | 'da-DK' | 'de-DE' @@ -107,6 +112,8 @@ export type AlphanumericLocale = | 'hu-HU' | 'it-IT' | 'id-ID' + | 'ja-JP' + | 'ko-KR' | 'ku-IQ' | 'nb-NO' | 'nl-BE' @@ -116,6 +123,7 @@ export type AlphanumericLocale = | 'pt-BR' | 'pt-PT' | 'ru-RU' + | 'si-LK' | 'sk-SK' | 'sl-SI' | 'sr-RS' @@ -133,6 +141,7 @@ export type MobilePhoneLocale = | 'ar-BH' | 'ar-DZ' | 'ar-EG' + | 'ar-EH' | 'ar-IQ' | 'ar-JO' | 'ar-KW' @@ -144,6 +153,7 @@ export type MobilePhoneLocale = | 'ar-SA' | 'ar-SY' | 'ar-TN' + | 'ar-YE' | 'az-AZ' | 'be-BY' | 'bg-BG' @@ -155,10 +165,15 @@ export type MobilePhoneLocale = | 'de-DE' | 'de-LU' | 'da-DK' + | 'dv-MV' | 'dz-BT' + | 'el-CY' | 'el-GR' + | 'en-AG' + | 'en-AI' | 'en-AU' | 'en-BM' + | 'en-BS' | 'en-BW' | 'en-CA' | 'en-GB' @@ -169,18 +184,23 @@ export type MobilePhoneLocale = | 'en-HN' | 'en-IE' | 'en-IN' + | 'en-JM' | 'en-KE' | 'en-KI' + | 'en-KN' + | 'en-LS' | 'en-MT' | 'en-MU' | 'en-NA' | 'en-NG' | 'en-NZ' + | 'en-PG' | 'en-PH' | 'en-PK' | 'en-RW' | 'en-SG' | 'en-SL' + | 'en-SS' | 'en-TZ' | 'en-UG' | 'en-US' @@ -196,7 +216,9 @@ export type MobilePhoneLocale = | 'es-DO' | 'es-EC' | 'es-ES' + | 'es-HN' | 'es-MX' + | 'es-NI' | 'es-PA' | 'es-PE' | 'es-PY' @@ -204,12 +226,15 @@ export type MobilePhoneLocale = | 'es-UY' | 'es-VE' | 'et-EE' + | 'fa-AF' | 'fa-IR' | 'fi-FI' | 'fj-FJ' | 'fo-FO' | 'fr-BE' | 'fr-BF' + | 'fr-BJ' + | 'fr-CD' | 'fr-CH' | 'fr-CM' | 'fr-FR' @@ -222,6 +247,7 @@ export type MobilePhoneLocale = | 'he-IL' | 'hu-HU' | 'id-ID' + | 'ir-IR' | 'it-CH' | 'it-IT' | 'it-SM' @@ -229,11 +255,16 @@ export type MobilePhoneLocale = | 'ka-GE' | 'kk-KZ' | 'kl-GL' + | 'ky-KG' | 'lt-LT' | 'lv-LV' + | 'mg-MG' + | 'mn-MN' | 'ms-MY' + | 'my-MM' | 'mz-MZ' | 'nb-NO' + | 'nl-AW' | 'nl-BE' | 'nl-NL' | 'ne-NP' @@ -242,6 +273,7 @@ export type MobilePhoneLocale = | 'pt-AO' | 'pt-BR' | 'pt-PT' + | 'ro-MD' | 'ro-RO' | 'ru-RU' | 'si-LK' @@ -267,6 +299,7 @@ export type PostalCodeLocale = | 'AT' | 'AU' | 'AZ' + | 'BA' | 'BE' | 'BG' | 'BR' @@ -349,6 +382,7 @@ export type IdentityCardLocale = | 'ES' | 'FI' | 'he-IL' + | 'hk-HK' | 'IN' | 'IT' | 'IR' @@ -364,6 +398,7 @@ export type PassportCountryCode = | 'AR' | 'AT' | 'AU' + | 'AZ' | 'BE' | 'BG' | 'BY' @@ -390,8 +425,11 @@ export type PassportCountryCode = | 'IR' | 'IS' | 'IT' + | 'JM' | 'JP' | 'KR' + | 'KZ' + | 'LI' | 'LT' | 'LU' | 'LV' @@ -400,6 +438,9 @@ export type PassportCountryCode = | 'MY' | 'MZ' | 'NL' + | 'NZ' + | 'PH' + | 'PK' | 'PL' | 'PO' | 'PT' @@ -408,6 +449,7 @@ export type PassportCountryCode = | 'SE' | 'SL' | 'SK' + | 'TH' | 'TR' | 'UA' | 'US'; @@ -416,10 +458,14 @@ export type IsLicensePlateLocale = | 'cs-CZ' | 'de-DE' | 'de-LI' + | 'en-NI' + | 'es-AR' | 'fi-FI' + | 'hu-HU' | 'pt-BR' | 'pt-PT' | 'sq-AL' + | 'sv-SE' | 'any'; export type TaxIDLocale = @@ -430,6 +476,7 @@ export type TaxIDLocale = | 'dk-DK' | 'el-CY' | 'el-GR' + | 'en-CA' | 'en-GB' | 'en-IE' | 'en-US' @@ -456,7 +503,75 @@ export type TaxIDLocale = | 'sl-SI' | 'sv-SE'; -export type VATCountryCode = 'GB' | 'IT' | 'NL'; +export type VATCountryCode = + | 'GB' + | 'IT' + | 'NL' + | 'AT' + | 'BE' + | 'BG' + | 'HR' + | 'CY' + | 'CZ' + | 'DK' + | 'EE' + | 'FI' + | 'FR' + | 'DE' + | 'EL' + | 'HU' + | 'IE' + | 'LV' + | 'LT' + | 'LU' + | 'MT' + | 'PL' + | 'PT' + | 'RO' + | 'SK' + | 'SI' + | 'ES' + | 'SE' + | 'AL' + | 'MK' + | 'AU' + | 'BY' + | 'CA' + | 'IS' + | 'IN' + | 'ID' + | 'IL' + | 'KZ' + | 'NZ' + | 'NG' + | 'NO' + | 'PH' + | 'RU' + | 'SM' + | 'SA' + | 'RS' + | 'CH' + | 'TR' + | 'UA' + | 'UZ' + | 'AR' + | 'BO' + | 'BR' + | 'CL' + | 'CO' + | 'CR' + | 'EC' + | 'SV' + | 'GT' + | 'HN' + | 'MX' + | 'NI' + | 'PA' + | 'PY' + | 'PE' + | 'DO' + | 'UY' + | 'VE'; export interface MinMaxOptions { min?: number; @@ -480,6 +595,16 @@ export interface ContainsOptions { minOccurrences?: number; } +/** + * defaults to + * { + * comparisonDate: Date().toString() + * } + */ +export interface IsAfterOptions { + comparisonDate?: string; +} + export interface IsAlphaOptions { // TODO(v7): remove string[] support ignore?: string | string[] | RegExp; @@ -489,6 +614,16 @@ export interface IsAlphanumericOptions { ignore?: string | RegExp; } +/** + * defaults to + * { + * crockford: false + * } + */ +export interface IsBase32Options { + crockford?: boolean; +} + /** * defaults to * { @@ -511,6 +646,10 @@ export interface IsBooleanOptions { loose?: boolean; } +export interface IsCreditCard { + provider?: 'amex' | 'dinersclub' | 'discover' | 'jcb' | 'mastercard' | 'unionpay' | 'visa'; +} + /** * defaults to * { @@ -576,6 +715,7 @@ export interface IsEmailOptions { domain_specific_validation?: boolean; blacklisted_chars?: string; host_blacklist?: string[]; + host_whitelist?: string[]; } /** @@ -599,7 +739,8 @@ export interface IsFloatOptions extends MinMaxExtendedOptions { * allow_underscores: false, * allow_trailing_dot: false, * allow_numeric_tld: false, - * allow_wildcard?: false + * allow_wildcard: false, + * ignore_max_length: false * } */ export interface IsFQDNOptions { @@ -608,6 +749,7 @@ export interface IsFQDNOptions { allow_trailing_dot?: boolean; allow_numeric_tld?: boolean; allow_wildcard?: boolean; + ignore_max_length?: boolean; } export interface IsIntOptions extends MinMaxExtendedOptions { @@ -656,6 +798,10 @@ export interface IsISO8601Options { strictSeparator?: boolean; } +export interface IsISBNOptions { + version?: '10' | '13'; +} + /** * defaults to * { @@ -686,6 +832,8 @@ export interface IsMACAddressOptions { */ // TODO(v7): remove no_colons no_colons?: boolean; + // Specify if it should validate against EUI-48 or EUI-64. If omitted it will try both. + eui?: '48' | '64'; } export interface IsMobilePhoneOptions { @@ -772,6 +920,18 @@ export interface IsURLOptions { allow_query_components?: boolean; } +/** + * defaults to + * { + * hourFormat: 'hour24', + * mode: 'default', + * }; + */ +export interface IsTimeOptions { + hourFormat?: 'hour24' | 'hour12'; + mode?: 'default' | 'withSeconds'; +} + export interface NormalizeEmailOptions { all_lowercase?: boolean; gmail_lowercase?: boolean;