11import type { Assert as BaseAssert , Constraint as BaseConstraint } from 'validator.js' ;
2+ import type { ValidatorJSAsserts } from 'validator.js-asserts' ;
3+
4+ /**
5+ * Extended Assert class that includes the methods on `Assert.prototype`.
6+ */
7+ interface ExtendedAssert extends BaseAssert {
8+ /** Check if value matches the assert */
9+ check ( value : unknown , group ?: string | string [ ] , context ?: unknown ) : true | any ;
10+ /** Validate the value against the assert */
11+ validate ( value : unknown , group ?: string | string [ ] , context ?: unknown ) : true ;
12+ /** Check if the assert requires validation */
13+ requiresValidation ( group ?: string | string [ ] ) : boolean ;
14+ /** Check if the assert belongs to a specific group */
15+ hasGroup ( group : string | string [ ] ) : boolean ;
16+ /** Check `hasGroup` over the specified groups */
17+ hasOneOf ( groups : string [ ] ) : boolean ;
18+ /** Check if the assert has any groups */
19+ hasGroups ( ) : boolean ;
20+ }
21+
22+ /**
23+ * Extended Constraint class that includes the methods on `Constraint.prototype`.
24+ */
25+ interface ExtendedConstraint extends BaseConstraint {
26+ nodes : Record < string , ExtendedAssert | ExtendedAssert [ ] | ExtendedConstraint > ;
27+ options : {
28+ strict ?: boolean ;
29+ deepRequired ?: boolean ;
30+ } ;
31+
32+ // Core prototype methods from validator.js
33+ isRequired ( property : string , group ?: string | string [ ] , deepRequired ?: boolean ) : boolean ;
34+ check ( object : any , group ?: string | string [ ] ) : true | Record < string , any > ;
35+ add ( node : string , object : ExtendedAssert | ExtendedAssert [ ] | ExtendedConstraint | Record < string , any > ) : this;
36+ has ( node : string , nodes ?: Record < string , any > ) : boolean ;
37+ get ( node : string , placeholder ?: any ) : ExtendedAssert | ExtendedAssert [ ] | ExtendedConstraint | null ;
38+ remove ( node : string ) : this;
39+ }
240
341/**
442 * Valid values for constraint mapping
543 */
644type ConstraintValue =
7- | BaseAssert
8- | BaseConstraint
45+ | ExtendedAssert
46+ | ExtendedConstraint
947 | Record < string , unknown >
10- | Array < BaseAssert | BaseConstraint | Record < string , unknown > >
11- | unknown ;
48+ | Array < ExtendedAssert | ExtendedConstraint | Record < string , unknown > > ;
1249
1350/**
1451 * All core `validator.js` assert factories.
1552 */
1653interface CoreAssertsMap {
1754 /** An object must have the given property. */
18- haveProperty ( node : string ) : ConstraintValue ;
55+ haveProperty ( node : string ) : ExtendedAssert ;
1956
2057 /** Alias for `haveProperty`. */
21- propertyDefined ( node : string ) : ConstraintValue ;
58+ propertyDefined ( node : string ) : ExtendedAssert ;
2259
2360 /** A string must be empty or only whitespace. */
24- blank ( ) : ConstraintValue ;
61+ blank ( ) : ExtendedAssert ;
2562
2663 /** Run a custom callback that returns true on success. */
27- callback ( fn : ( value : unknown , ...args : unknown [ ] ) => boolean , ...args : unknown [ ] ) : ConstraintValue ;
64+ callback ( fn : ( value : unknown , ...args : unknown [ ] ) => boolean , ...args : unknown [ ] ) : ExtendedAssert ;
2865
2966 /** Value must be one of the provided list. */
30- choice ( list : unknown [ ] | ( ( ) => unknown [ ] ) ) : ConstraintValue ;
67+ choice ( list : unknown [ ] | ( ( ) => unknown [ ] ) ) : ExtendedAssert ;
3168
3269 /** Validate each element of an array. */
33- collection ( assertOrConstraint : ConstraintValue | BaseConstraint | object ) : ConstraintValue ;
70+ collection ( assertOrConstraint : ConstraintValue | ExtendedConstraint | object ) : ExtendedAssert ;
3471
3572 /** Array must have exactly N items. */
36- count ( count : number | ( ( arr : unknown [ ] ) => number ) ) : ConstraintValue ;
73+ count ( count : number | ( ( arr : unknown [ ] ) => number ) ) : ExtendedAssert ;
3774
3875 /** Valid email address. */
39- email ( ) : ConstraintValue ;
76+ email ( ) : ExtendedAssert ;
4077
4178 /** Value must equal reference or match function. */
42- equalTo ( reference : unknown | ( ( value : unknown ) => unknown ) ) : ConstraintValue ;
79+ equalTo ( reference : unknown | ( ( value : unknown ) => unknown ) ) : ExtendedAssert ;
4380
4481 /** Numeric value must be > threshold. */
45- greaterThan ( threshold : number ) : ConstraintValue ;
82+ greaterThan ( threshold : number ) : ExtendedAssert ;
4683
4784 /** Numeric value must be ≥ threshold. */
48- greaterThanOrEqual ( threshold : number ) : ConstraintValue ;
85+ greaterThanOrEqual ( threshold : number ) : ExtendedAssert ;
4986
5087 /** Value must be `instanceof` the given class. */
51- instanceOf ( classRef : new ( ...args : unknown [ ] ) => unknown ) : ConstraintValue ;
88+ instanceOf ( classRef : new ( ...args : unknown [ ] ) => unknown ) : ExtendedAssert ;
5289
5390 /** Value must be a string. */
54- string ( ) : ConstraintValue ;
91+ string ( ) : ExtendedAssert ;
5592
5693 /** String/array length must be within `[min, max]`. */
57- length ( boundaries : { min ?: number ; max ?: number } ) : ConstraintValue ;
94+ length ( boundaries : { min ?: number ; max ?: number } ) : ExtendedAssert ;
5895
5996 /** Alias for `length()`. */
60- ofLength ( boundaries : { min ?: number ; max ?: number } ) : ConstraintValue ;
97+ ofLength ( boundaries : { min ?: number ; max ?: number } ) : ExtendedAssert ;
6198
6299 /** Numeric value must be < threshold. */
63- lessThan ( threshold : number ) : ConstraintValue ;
100+ lessThan ( threshold : number ) : ExtendedAssert ;
64101
65102 /** Numeric value must be ≤ threshold. */
66- lessThanOrEqual ( threshold : number ) : ConstraintValue ;
103+ lessThanOrEqual ( threshold : number ) : ExtendedAssert ;
67104
68105 /** Value must not be null or undefined. */
69- notNull ( ) : ConstraintValue ;
106+ notNull ( ) : ExtendedAssert ;
70107
71108 /** String must contain ≥1 non-whitespace character. */
72- notBlank ( ) : ConstraintValue ;
109+ notBlank ( ) : ExtendedAssert ;
73110
74111 /** Value must not equal reference or match fn. */
75- notEqualTo ( reference : unknown | ( ( value : unknown ) => unknown ) ) : ConstraintValue ;
112+ notEqualTo ( reference : unknown | ( ( value : unknown ) => unknown ) ) : ExtendedAssert ;
76113
77114 /** Value must be exactly null. */
78- null ( ) : ConstraintValue ;
115+ null ( ) : ExtendedAssert ;
79116
80117 /** `Number`/`String`/`Array` must fall within `[min, max]`. */
81- range ( min : number , max : number ) : ConstraintValue ;
118+ range ( min : number , max : number ) : ExtendedAssert ;
82119
83120 /** `String` must match the given `RegExp`. */
84- regexp ( regexp : string | RegExp , flag ?: string ) : ConstraintValue ;
121+ regexp ( regexp : string | RegExp , flag ?: string ) : ExtendedAssert ;
85122
86123 /** Value must be defined (not `undefined`). */
87- required ( ) : ConstraintValue ;
124+ required ( ) : ExtendedAssert ;
88125
89126 /** Array items must be unique (by `key` if given). */
90- unique ( opts ?: { key : string } ) : ConstraintValue ;
127+ unique ( opts ?: { key : string } ) : ExtendedAssert ;
91128
92129 /** If `context[ref]` passes `is`, run `then` else `otherwise`. */
93130 when (
@@ -97,7 +134,7 @@ interface CoreAssertsMap {
97134 then ?: ConstraintValue | object ;
98135 otherwise ?: ConstraintValue | object ;
99136 }
100- ) : ConstraintValue ;
137+ ) : ExtendedAssert ;
101138}
102139
103140type ExtraAssertType = {
@@ -114,8 +151,8 @@ type ExtraAsserts<EA extends ExtraAssertType> = {
114151
115152/** Callable/newable `Assert` constructor. */
116153interface BaseAssertStatic {
117- new ( group ?: string | string [ ] ) : BaseAssert ;
118- ( group ?: string | string [ ] ) : BaseAssert ;
154+ new ( group ?: string | string [ ] ) : ExtendedAssert ;
155+ ( group ?: string | string [ ] ) : ExtendedAssert ;
119156}
120157
121158/**
@@ -125,7 +162,7 @@ interface BaseAssertStatic {
125162 * - all `validator.js-asserts` (camelCased).
126163 * - any `extraAsserts` passed at runtime.
127164 */
128- type AssertStatic < EA extends ExtraAssertType > = BaseAssertStatic & CoreAssertsMap & ExtraAsserts < EA > & ExtraAssertType ;
165+ type AssertStatic < EA extends ExtraAssertType > = BaseAssertStatic & CoreAssertsMap & ValidatorJSAsserts & ExtraAsserts < EA > & ExtraAssertType ;
129166
130167/** Map a data type to `validator.js` constraints. */
131168type ConstraintMapping < T > =
0 commit comments