@zmdb/validator 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +35 -0
- package/dist/advanced/index.d.ts +53 -0
- package/dist/advanced/index.d.ts.map +1 -0
- package/dist/advanced/index.js +132 -0
- package/dist/advanced/index.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +102 -0
- package/dist/index.js.map +1 -0
- package/dist/regex-complexity.d.ts +6 -0
- package/dist/regex-complexity.d.ts.map +1 -0
- package/dist/regex-complexity.js +42 -0
- package/dist/regex-complexity.js.map +1 -0
- package/dist/serialization/index.d.ts +20 -0
- package/dist/serialization/index.d.ts.map +1 -0
- package/dist/serialization/index.js +64 -0
- package/dist/serialization/index.js.map +1 -0
- package/dist/utilities/index.d.ts +29 -0
- package/dist/utilities/index.d.ts.map +1 -0
- package/dist/utilities/index.js +694 -0
- package/dist/utilities/index.js.map +1 -0
- package/dist/validation-error.d.ts +31 -0
- package/dist/validation-error.d.ts.map +1 -0
- package/dist/validation-error.js +43 -0
- package/dist/validation-error.js.map +1 -0
- package/package.json +61 -0
- package/src/advanced/index.ts +188 -0
- package/src/errors.ts +27 -0
- package/src/index.ts +115 -0
- package/src/regex-complexity.ts +45 -0
- package/src/serialization/index.ts +70 -0
- package/src/utilities/index.ts +748 -0
- package/src/validation-error.ts +53 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface ValidationIssue {
|
|
2
|
+
readonly path: string;
|
|
3
|
+
readonly message: string;
|
|
4
|
+
readonly expected?: string;
|
|
5
|
+
readonly value?: unknown;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class ValidationError extends Error {
|
|
9
|
+
readonly issues: readonly ValidationIssue[];
|
|
10
|
+
|
|
11
|
+
constructor(message: string, issues: readonly ValidationIssue[] = []) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'ValidationError';
|
|
14
|
+
this.issues = issues;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Whether a thrown value claims to be about validation.
|
|
20
|
+
*
|
|
21
|
+
* Structural rather than `instanceof ValidationError`, because a validator a caller wrote
|
|
22
|
+
* themselves throws its own error type — zod's, io-ts's, or one of their own — and the HTTP
|
|
23
|
+
* adapters that ask this question have no business caring which. Carrying an `issues`
|
|
24
|
+
* property is the claim; {@link validationIssuesOf} decides whether it holds up.
|
|
25
|
+
*/
|
|
26
|
+
export function claimsValidationIssues(error: unknown): boolean {
|
|
27
|
+
return error !== null && typeof error === 'object' && 'issues' in error;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The issues on a thrown error, or `undefined` if it carries none worth reporting.
|
|
32
|
+
*
|
|
33
|
+
* Every entry is checked rather than asserted. These end up in a 400 body that a client
|
|
34
|
+
* reads, and "it has an `issues` property" is no evidence that the property holds issues —
|
|
35
|
+
* an error whose `issues` was a string used to be serialized into the response as though it
|
|
36
|
+
* were the list. An entry missing a `path` or a `message` is dropped rather than passed on
|
|
37
|
+
* half-formed; a `ValidationError` with an empty list still answers with the empty list,
|
|
38
|
+
* which is what tells a caller "validation, and it declined to say more".
|
|
39
|
+
*/
|
|
40
|
+
export function validationIssuesOf(error: unknown): readonly ValidationIssue[] | undefined {
|
|
41
|
+
if (error === null || typeof error !== 'object' || !('issues' in error)) return undefined;
|
|
42
|
+
const issues: unknown = error.issues;
|
|
43
|
+
if (!Array.isArray(issues)) return undefined;
|
|
44
|
+
return issues.filter(
|
|
45
|
+
(issue: unknown): issue is ValidationIssue =>
|
|
46
|
+
issue !== null &&
|
|
47
|
+
typeof issue === 'object' &&
|
|
48
|
+
'path' in issue &&
|
|
49
|
+
typeof issue.path === 'string' &&
|
|
50
|
+
'message' in issue &&
|
|
51
|
+
typeof issue.message === 'string',
|
|
52
|
+
);
|
|
53
|
+
}
|