Skip to content

Commit 54aff80

Browse files
committed
Update validator.js-asserts to include its types
1 parent ebf9529 commit 54aff80

3 files changed

Lines changed: 74 additions & 38 deletions

File tree

index.d.ts

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,130 @@
11
import 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
*/
644
type 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
*/
1653
interface 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

103140
type ExtraAssertType = {
@@ -114,8 +151,8 @@ type ExtraAsserts<EA extends ExtraAssertType> = {
114151

115152
/** Callable/newable `Assert` constructor. */
116153
interface 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. */
131168
type ConstraintMapping<T> =

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"json-mask": "^2.0.0",
2424
"lodash": "^4.17.21",
2525
"validator.js": "^2.0.3",
26-
"validator.js-asserts": "^7.3.1"
26+
"validator.js-asserts": "https://github.com/uphold/validator.js-asserts#support/type-declaration"
2727
},
2828
"devDependencies": {
2929
"@fastify/pre-commit": "^2.2.0",

yarn.lock

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,10 +2574,9 @@ url-join@5.0.0:
25742574
resolved "https://registry.yarnpkg.com/url-join/-/url-join-5.0.0.tgz#c2f1e5cbd95fa91082a93b58a1f42fecb4bdbcf1"
25752575
integrity sha512-n2huDr9h9yzd6exQVnH/jU5mr+Pfx08LRXXZhkLLetAMESRj+anQsTAh940iMrIetKAmry9coFuZQ2jY8/p3WA==
25762576

2577-
validator.js-asserts@^7.3.1:
2578-
version "7.3.1"
2579-
resolved "https://registry.yarnpkg.com/validator.js-asserts/-/validator.js-asserts-7.3.1.tgz#ac6de9c82861ea5571b5433558b00a9708254880"
2580-
integrity sha512-NCKicmp9OBPVw+xgBftzlJNBlqzySCYirp3lPcXAGDuciBxf3dgK+bcjZCf90aq3+/20HbDUopxWW0h6xcrnhA==
2577+
"validator.js-asserts@https://github.com/uphold/validator.js-asserts#support/type-declaration":
2578+
version "8.4.0"
2579+
resolved "https://github.com/uphold/validator.js-asserts#d84cd3d9b8e7f1a0f91bc3674d75289010328c8f"
25812580
dependencies:
25822581
lodash "^4.17.21"
25832582
validator.js "^2.0.0"

0 commit comments

Comments
 (0)