Skip to content

Commit d84cd3d

Browse files
committed
Add type declarations
1 parent 9075c8b commit d84cd3d

2 files changed

Lines changed: 222 additions & 0 deletions

File tree

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
}
3232
],
3333
"main": "./src/index.js",
34+
"types": "./src/types/index.d.ts",
35+
"exports": {
36+
".": {
37+
"types": "./src/types/index.d.ts",
38+
"import": "./src/index.js",
39+
"require": "./src/index.js"
40+
}
41+
},
3442
"scripts": {
3543
"lint": "eslint",
3644
"release": "release-it",

src/types/index.d.ts

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
import type { Assert as BaseAssert } from 'validator.js';
2+
3+
/**
4+
* Extended Assert class that includes the methods on `Assert.prototype`.
5+
*/
6+
interface ExtendedAssert extends BaseAssert {
7+
/** Check if value matches the assert */
8+
check(value: unknown, group?: string | string[], context?: unknown): true | any;
9+
/** Validate the value against the assert */
10+
validate(value: unknown, group?: string | string[], context?: unknown): true;
11+
/** Check if the assert requires validation */
12+
requiresValidation(group?: string | string[]): boolean;
13+
/** Check if the assert belongs to a specific group */
14+
hasGroup(group: string | string[]): boolean;
15+
/** Check `hasGroup` over the specified groups */
16+
hasOneOf(groups: string[]): boolean;
17+
/** Check if the assert has any groups */
18+
hasGroups(): boolean;
19+
}
20+
21+
export interface ValidatorJSAsserts {
22+
/**
23+
* Value is a valid American Bankers Association Routing Number used in ACH payments.
24+
* @requires abavalidator
25+
*/
26+
abaRoutingNumber(): ExtendedAssert;
27+
/** Value is a valid Bank Identifier Code (BIC) used for international wire transfers. */
28+
bankIdentifierCode(): ExtendedAssert;
29+
/**
30+
* Value is a BigNumber.
31+
* @requires bignumber.js
32+
*/
33+
bigNumber(options?: { validateSignificantDigits?: boolean }): ExtendedAssert;
34+
/**
35+
* Value is a BigNumber equal to a specific value.
36+
* @requires bignumber.js
37+
*/
38+
bigNumberEqualTo(value: string | number, options?: { validateSignificantDigits?: boolean }): ExtendedAssert;
39+
/**
40+
* Value is a BigNumber greater than a specific value.
41+
* @requires bignumber.js
42+
*/
43+
bigNumberGreaterThan(threshold: string | number, options?: { validateSignificantDigits?: boolean }): ExtendedAssert;
44+
/**
45+
* Value is a BigNumber greater than or equal to a specific value.
46+
* @requires bignumber.js
47+
*/
48+
bigNumberGreaterThanOrEqualTo(
49+
threshold: string | number,
50+
options?: { validateSignificantDigits?: boolean }
51+
): ExtendedAssert;
52+
/**
53+
* Value is a BigNumber less than a specific value.
54+
* @requires bignumber.js
55+
*/
56+
bigNumberLessThan(threshold: string | number, options?: { validateSignificantDigits?: boolean }): ExtendedAssert;
57+
/**
58+
* Value is a BigNumber less than or equal to a specific value.
59+
* @requires bignumber.js
60+
*/
61+
bigNumberLessThanOrEqualTo(
62+
threshold: string | number,
63+
options?: { validateSignificantDigits?: boolean }
64+
): ExtendedAssert;
65+
/** Value is a boolean. */
66+
boolean(): ExtendedAssert;
67+
/** Valid Canadian zip code. */
68+
caZipCode(): ExtendedAssert;
69+
/** Run a custom callback function, passing a custom class name. */
70+
callback(fn: (value: unknown) => boolean, customClass: string): ExtendedAssert;
71+
/**
72+
* Valid Brazilian CPF number.
73+
* @requires cpf
74+
*/
75+
cpfNumber(): ExtendedAssert;
76+
/**
77+
* Valid credit card number.
78+
* @requires creditcard
79+
*/
80+
creditCard(): ExtendedAssert;
81+
/**
82+
* Valid Mexican CURP number.
83+
* @requires curp
84+
*/
85+
curpNumber(): ExtendedAssert;
86+
/**
87+
* Valid `moment` date, optionally with a specific format.
88+
* - If `moment` is not available, returns `true` by default.
89+
* @requires moment
90+
*/
91+
date(options?: { format?: string }): ExtendedAssert;
92+
/**
93+
* Valid `moment` date difference greater than a threshold.
94+
* @requires moment
95+
*/
96+
dateDiffGreaterThan(
97+
threshold: number,
98+
options?: {
99+
absolute?: boolean;
100+
asFloat?: boolean;
101+
fromDate?: Date | string | null;
102+
unit?: string;
103+
}
104+
): ExtendedAssert;
105+
/**
106+
* Valid `moment` date difference greater than or equal to a threshold.
107+
* @requires moment
108+
*/
109+
dateDiffGreaterThanOrEqualTo(
110+
threshold: number,
111+
options?: {
112+
absolute?: boolean;
113+
asFloat?: boolean;
114+
fromDate?: Date | string | null;
115+
unit?: string;
116+
}
117+
): ExtendedAssert;
118+
/**
119+
* Valid `moment` date difference less than a threshold.
120+
* @requires moment
121+
*/
122+
dateDiffLessThan(
123+
threshold: number,
124+
options?: {
125+
absolute?: boolean;
126+
asFloat?: boolean;
127+
fromDate?: Date | string | null;
128+
unit?: string;
129+
}
130+
): ExtendedAssert;
131+
/**
132+
* Valid `moment` date difference less than or equal to a threshold.
133+
* @requires moment
134+
*/
135+
dateDiffLessThanOrEqualTo(
136+
threshold: number,
137+
options?: {
138+
absolute?: boolean;
139+
asFloat?: boolean;
140+
fromDate?: Date | string | null;
141+
unit?: string;
142+
}
143+
): ExtendedAssert;
144+
/**
145+
* Extends base email address assert.
146+
* - Max length is 254 characters.
147+
* - Returns `true` if email ends in `.deleted`.
148+
* @requires validator
149+
*/
150+
email(): ExtendedAssert;
151+
/** Value is an object with exactly the specified keys. */
152+
equalKeys(...keys: string[] | [string[]]): ExtendedAssert;
153+
/** Validate a hash string using a specific algorithm. */
154+
hash(algorithm: 'sha1' | 'sha256' | 'sha512'): ExtendedAssert;
155+
/** Valid integer number. */
156+
integer(): ExtendedAssert;
157+
/**
158+
* Valid International Bank Account Number (IBAN).
159+
* @requires iban
160+
*/
161+
internationalBankAccountNumber(): ExtendedAssert;
162+
/** Value is an IP address. */
163+
ip(): ExtendedAssert;
164+
/**
165+
* Value is an ISO 3166 country code.
166+
* @requires isoc
167+
*/
168+
iso3166Country(): ExtendedAssert;
169+
/** Value is a JSON string. */
170+
json(): ExtendedAssert;
171+
/** Value is not empty. */
172+
notEmpty(): ExtendedAssert;
173+
/** Value is null or passes the provided assert. */
174+
nullOr(assert: ExtendedAssert): ExtendedAssert;
175+
/** Value is null or a boolean. */
176+
nullOrBoolean(): ExtendedAssert;
177+
/** Value is null or a date. */
178+
nullOrDate(): ExtendedAssert;
179+
/** Value is null or a string. */
180+
nullOrString(boundaries?: { min?: number; max?: number }): ExtendedAssert;
181+
/**
182+
* Valid phone number, optionally validating against a specific country code.
183+
* @requires google-libphonenumber
184+
*/
185+
phone(options?: { countryCode?: string }): ExtendedAssert;
186+
/** Value is a plain object. */
187+
plainObject(): ExtendedAssert;
188+
/**
189+
* Valid Mexican RFC number.
190+
* @requires validate-rfc
191+
*/
192+
rfcNumber(): ExtendedAssert;
193+
/**
194+
* Valid taxpayer identification number (TIN).
195+
* @requires tin-validator
196+
*/
197+
taxpayerIdentificationNumber(): ExtendedAssert;
198+
/**
199+
* Value is UK bank account details using modulus checking.
200+
* @requires uk-modulus-checking
201+
*/
202+
ukModulusChecking(): ExtendedAssert;
203+
/**
204+
* Valid URI.
205+
* @requires uri-js
206+
*/
207+
uri(constraints?: Record<string, any>): ExtendedAssert;
208+
/** Value is a US subdivision code. */
209+
usSubdivision(options?: { categories?: string[]; alpha2Only?: boolean }): ExtendedAssert;
210+
/** Value is a US zip code. */
211+
usZipCode(): ExtendedAssert;
212+
/** Valid UUID. Accepts version 3, 4, or 5. */
213+
uuid(version?: 3 | 4 | 5): ExtendedAssert;
214+
}

0 commit comments

Comments
 (0)