Skip to content

Commit e099d0c

Browse files
risantosfranciscocardoso
authored andcommitted
Add type declarations
1 parent 5513eae commit e099d0c

2 files changed

Lines changed: 211 additions & 0 deletions

File tree

index.d.ts

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import type { Assert as BaseAssert, Constraint as BaseConstraint } from 'validator.js';
2+
3+
/**
4+
* Valid values for constraint mapping
5+
*/
6+
type ConstraintValue =
7+
| BaseAssert
8+
| BaseConstraint
9+
| Record<string, unknown>
10+
| Array<BaseAssert | BaseConstraint | Record<string, unknown>>
11+
| unknown;
12+
13+
/**
14+
* All core `validator.js` assert factories.
15+
*/
16+
interface CoreAssertsMap {
17+
/** An object must have the given property. */
18+
haveProperty(node: string): ConstraintValue;
19+
20+
/** Alias for `haveProperty`. */
21+
propertyDefined(node: string): ConstraintValue;
22+
23+
/** A string must be empty or only whitespace. */
24+
blank(): ConstraintValue;
25+
26+
/** Run a custom callback that returns true on success. */
27+
callback(fn: (value: unknown, ...args: unknown[]) => boolean, ...args: unknown[]): ConstraintValue;
28+
29+
/** Value must be one of the provided list. */
30+
choice(list: unknown[] | (() => unknown[])): ConstraintValue;
31+
32+
/** Validate each element of an array. */
33+
collection(assertOrConstraint: ConstraintValue | BaseConstraint | object): ConstraintValue;
34+
35+
/** Array must have exactly N items. */
36+
count(count: number | ((arr: unknown[]) => number)): ConstraintValue;
37+
38+
/** Valid email address. */
39+
email(): ConstraintValue;
40+
41+
/** Value must equal reference or match function. */
42+
equalTo(reference: unknown | ((value: unknown) => unknown)): ConstraintValue;
43+
44+
/** Numeric value must be > threshold. */
45+
greaterThan(threshold: number): ConstraintValue;
46+
47+
/** Numeric value must be ≥ threshold. */
48+
greaterThanOrEqual(threshold: number): ConstraintValue;
49+
50+
/** Value must be `instanceof` the given class. */
51+
instanceOf(classRef: new (...args: unknown[]) => unknown): ConstraintValue;
52+
53+
/** Value must be a string. */
54+
string(): ConstraintValue;
55+
56+
/** String/array length must be within `[min, max]`. */
57+
length(boundaries: { min?: number; max?: number }): ConstraintValue;
58+
59+
/** Alias for `length()`. */
60+
ofLength(boundaries: { min?: number; max?: number }): ConstraintValue;
61+
62+
/** Numeric value must be < threshold. */
63+
lessThan(threshold: number): ConstraintValue;
64+
65+
/** Numeric value must be ≤ threshold. */
66+
lessThanOrEqual(threshold: number): ConstraintValue;
67+
68+
/** Value must not be null or undefined. */
69+
notNull(): ConstraintValue;
70+
71+
/** String must contain ≥1 non-whitespace character. */
72+
notBlank(): ConstraintValue;
73+
74+
/** Value must not equal reference or match fn. */
75+
notEqualTo(reference: unknown | ((value: unknown) => unknown)): ConstraintValue;
76+
77+
/** Value must be exactly null. */
78+
null(): ConstraintValue;
79+
80+
/** `Number`/`String`/`Array` must fall within `[min, max]`. */
81+
range(min: number, max: number): ConstraintValue;
82+
83+
/** `String` must match the given `RegExp`. */
84+
regexp(regexp: string | RegExp, flag?: string): ConstraintValue;
85+
86+
/** Value must be defined (not `undefined`). */
87+
required(): ConstraintValue;
88+
89+
/** Array items must be unique (by `key` if given). */
90+
unique(opts?: { key: string }): ConstraintValue;
91+
92+
/** If `context[ref]` passes `is`, run `then` else `otherwise`. */
93+
when(
94+
ref: string,
95+
options: {
96+
is: ConstraintValue | object;
97+
then?: ConstraintValue | object;
98+
otherwise?: ConstraintValue | object;
99+
}
100+
): ConstraintValue;
101+
}
102+
103+
type ExtraAssertType = {
104+
[key: string]: ConstraintValue;
105+
};
106+
107+
/**
108+
* Map runtime‐passed `extraAsserts` into uncapitalized methods.
109+
* This can include both built‐ins *and* entirely custom keys.
110+
*/
111+
type ExtraAsserts<EA extends ExtraAssertType> = {
112+
[K in keyof EA as Uncapitalize<string & K>]: EA[K];
113+
};
114+
115+
/** Callable/newable `Assert` constructor. */
116+
interface BaseAssertStatic {
117+
new (group?: string | string[]): BaseAssert;
118+
(group?: string | string[]): BaseAssert;
119+
}
120+
121+
/**
122+
* The full `is` API:
123+
* - BaseAssert constructor.
124+
* - core `validator.js` asserts.
125+
* - all `validator.js-asserts` (camelCased).
126+
* - any `extraAsserts` passed at runtime.
127+
*/
128+
type AssertStatic<EA extends ExtraAssertType> = BaseAssertStatic & CoreAssertsMap & ExtraAsserts<EA> & ExtraAssertType;
129+
130+
/** Map a data type to `validator.js` constraints. */
131+
type ConstraintMapping<T> =
132+
| { [P in keyof T]?: ConstraintValue | ConstraintValue[] }
133+
| Record<string, ConstraintValue | ConstraintValue[]>;
134+
135+
/** `validate`/`assert` function signature. */
136+
type ValidateFunction = <T>(data: T, constraints: ConstraintMapping<T> | Record<string, ConstraintValue>) => T;
137+
138+
/** Type of `Error` to pass to `AssertionError`/`ValidationError`. */
139+
type ValidatorErrorType = new (...args: any[]) => Error;
140+
141+
/** Options for creating a validator. */
142+
interface ValidatorOptions<EA extends ExtraAssertType = ExtraAssertType> {
143+
AssertionError?: ValidatorErrorType;
144+
ValidationError?: ValidatorErrorType;
145+
extraAsserts?: EA;
146+
logger?: (errors: any) => void;
147+
obfuscator?: (input: { errors: any }) => { errors: any };
148+
mask?: boolean;
149+
}
150+
151+
/**
152+
* Base exports with just `is`.
153+
*/
154+
interface BaseValidatorExports<EA extends ExtraAssertType = ExtraAssertType> {
155+
is: AssertStatic<EA>;
156+
}
157+
158+
/**
159+
* Exports with `assert` added when `AssertionError` is provided.
160+
*/
161+
interface ValidatorExportsWithAssert<EA extends ExtraAssertType = ExtraAssertType> extends BaseValidatorExports<EA> {
162+
assert: ValidateFunction;
163+
}
164+
165+
/**
166+
* Exports with `validate` added when `ValidationError` is provided.
167+
*/
168+
interface ValidatorExportsWithValidate<EA extends ExtraAssertType = ExtraAssertType> extends BaseValidatorExports<EA> {
169+
validate: ValidateFunction;
170+
}
171+
172+
/**
173+
* Exports with both `assert` and `validate` when both errors are provided.
174+
*/
175+
interface ValidatorExportsWithBoth<EA extends ExtraAssertType = ExtraAssertType> extends BaseValidatorExports<EA> {
176+
assert: ValidateFunction;
177+
validate: ValidateFunction;
178+
}
179+
180+
/**
181+
* Factory function to create a `validator` instance.
182+
*/
183+
declare function validator<EA extends ExtraAssertType = ExtraAssertType>(
184+
options: ValidatorOptions<EA> & { AssertionError: ValidatorErrorType; ValidationError: ValidatorErrorType }
185+
): ValidatorExportsWithBoth<EA>;
186+
187+
declare function validator<EA extends ExtraAssertType = ExtraAssertType>(
188+
options: ValidatorOptions<EA> & { AssertionError: ValidatorErrorType }
189+
): ValidatorExportsWithAssert<EA>;
190+
191+
declare function validator<EA extends ExtraAssertType = ExtraAssertType>(
192+
options: ValidatorOptions<EA> & { ValidationError: ValidatorErrorType }
193+
): ValidatorExportsWithValidate<EA>;
194+
195+
declare function validator<EA extends ExtraAssertType = ExtraAssertType>(
196+
options?: ValidatorOptions<EA>
197+
): BaseValidatorExports<EA>;
198+
199+
/**
200+
* Export `validator`.
201+
*/
202+
203+
export = validator;

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
"version": "2.1.0",
44
"description": "Extensive validations built on top of validator.js",
55
"main": "index.js",
6+
"types": "./index.d.ts",
7+
"exports": {
8+
".": {
9+
"types": "./index.d.ts",
10+
"import": "./index.js",
11+
"require": "./index.js"
12+
}
13+
},
614
"author": "Uphold",
715
"license": "MIT",
816
"scripts": {

0 commit comments

Comments
 (0)