@nxgt/graphql-validation 0.3.0 → 0.4.0
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/README.md +55 -3
- package/dist/builder/leaf.d.ts.map +1 -1
- package/dist/chunks/{index-1mzzydy3.js → index-twevz7yc.js} +107 -42
- package/dist/chunks/index-twevz7yc.js.map +26 -0
- package/dist/codegen.js +1 -1
- package/dist/field-issue-of.d.ts +28 -0
- package/dist/field-issue-of.d.ts.map +1 -0
- package/dist/formats/format.d.ts +8 -0
- package/dist/formats/format.d.ts.map +1 -1
- package/dist/formats/marked.d.ts +42 -0
- package/dist/formats/marked.d.ts.map +1 -0
- package/dist/formats/registry.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +101 -2
- package/dist/index.js.map +5 -3
- package/dist/invalid-input.d.ts +61 -0
- package/dist/invalid-input.d.ts.map +1 -0
- package/docs/README.md +1 -0
- package/docs/guide/constraints.md +26 -10
- package/docs/guide/errors.md +3 -1
- package/docs/guide/invalid-input.md +240 -0
- package/docs/roadmap.md +21 -5
- package/docs/troubleshooting.md +14 -8
- package/package.json +1 -1
- package/dist/chunks/index-1mzzydy3.js.map +0 -25
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
} from "./chunks/index-xcaf9kes.js";
|
|
5
5
|
import {
|
|
6
6
|
checkConstraints2
|
|
7
|
-
} from "./chunks/index-
|
|
7
|
+
} from "./chunks/index-twevz7yc.js";
|
|
8
8
|
|
|
9
9
|
// src/bad-user-input.ts
|
|
10
10
|
import { GraphQLError } from "graphql";
|
|
@@ -32,6 +32,99 @@ async function parseArgs(schema, args, where, constraintOf) {
|
|
|
32
32
|
throw errorOf(where, issuesOf(result.error, constraintOf));
|
|
33
33
|
return result.data;
|
|
34
34
|
}
|
|
35
|
+
// src/invalid-input.ts
|
|
36
|
+
import { GraphQLError as GraphQLError2 } from "graphql";
|
|
37
|
+
var INVALID_INPUT = "validation.errors.invalid-input";
|
|
38
|
+
function paramsOf(params) {
|
|
39
|
+
return params && Object.keys(params).length > 0 ? params : undefined;
|
|
40
|
+
}
|
|
41
|
+
function messageOf(code, params, translate) {
|
|
42
|
+
return translate?.(code, params) ?? code;
|
|
43
|
+
}
|
|
44
|
+
function fieldsOf(issues, translate) {
|
|
45
|
+
return issues.map(({ path, code, params: given }) => {
|
|
46
|
+
const params = paramsOf(given);
|
|
47
|
+
return {
|
|
48
|
+
path,
|
|
49
|
+
code,
|
|
50
|
+
message: messageOf(code, params, translate),
|
|
51
|
+
...params && { params }
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function invalidInputError(issues, options = {}) {
|
|
56
|
+
const { translate, ...graphql } = options;
|
|
57
|
+
return new GraphQLError2(messageOf(INVALID_INPUT, undefined, translate), {
|
|
58
|
+
...graphql,
|
|
59
|
+
extensions: {
|
|
60
|
+
code: INVALID_INPUT,
|
|
61
|
+
http: { status: 400 },
|
|
62
|
+
fields: fieldsOf(issues, translate)
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function fieldsError(code, issues, options = {}) {
|
|
67
|
+
const { translate, status, params: given, ...graphql } = options;
|
|
68
|
+
const params = paramsOf(given);
|
|
69
|
+
return new GraphQLError2(messageOf(code, params, translate), {
|
|
70
|
+
...graphql,
|
|
71
|
+
extensions: {
|
|
72
|
+
code,
|
|
73
|
+
...params && { params },
|
|
74
|
+
...status && { http: { status } },
|
|
75
|
+
fields: fieldsOf(issues, translate)
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function inputPath(path) {
|
|
80
|
+
const [first, ...rest] = path;
|
|
81
|
+
return (first === "input" && rest.length > 0 ? rest : path).map((key) => String(key)).join(".");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/field-issue-of.ts
|
|
85
|
+
var FIELD_CODES = {
|
|
86
|
+
tooShort: "validation.errors.too-short",
|
|
87
|
+
tooLong: "validation.errors.too-long",
|
|
88
|
+
outOfRange: "validation.errors.out-of-range",
|
|
89
|
+
tooFew: "validation.errors.too-few",
|
|
90
|
+
tooMany: "validation.errors.too-many",
|
|
91
|
+
invalidFormat: "validation.errors.invalid-format",
|
|
92
|
+
invalid: "validation.errors.invalid"
|
|
93
|
+
};
|
|
94
|
+
var TEXT_CHECKS = new Set([
|
|
95
|
+
"starts_with",
|
|
96
|
+
"ends_with",
|
|
97
|
+
"includes"
|
|
98
|
+
]);
|
|
99
|
+
function fieldIssueOf(issue) {
|
|
100
|
+
return { path: inputPath(issue.path), code: codeOf(issue) };
|
|
101
|
+
}
|
|
102
|
+
function codeOf(issue) {
|
|
103
|
+
switch (issue.code) {
|
|
104
|
+
case "too_small":
|
|
105
|
+
case "too_big": {
|
|
106
|
+
const small = issue.code === "too_small";
|
|
107
|
+
switch (issue.origin) {
|
|
108
|
+
case "string":
|
|
109
|
+
return small ? FIELD_CODES.tooShort : FIELD_CODES.tooLong;
|
|
110
|
+
case "number":
|
|
111
|
+
case "bigint":
|
|
112
|
+
return FIELD_CODES.outOfRange;
|
|
113
|
+
case "array":
|
|
114
|
+
case "set":
|
|
115
|
+
return small ? FIELD_CODES.tooFew : FIELD_CODES.tooMany;
|
|
116
|
+
default:
|
|
117
|
+
return FIELD_CODES.invalid;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
case "not_multiple_of":
|
|
121
|
+
return FIELD_CODES.outOfRange;
|
|
122
|
+
case "invalid_format":
|
|
123
|
+
return TEXT_CHECKS.has(issue.format) ? FIELD_CODES.invalid : FIELD_CODES.invalidFormat;
|
|
124
|
+
default:
|
|
125
|
+
return FIELD_CODES.invalid;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
35
128
|
// src/validated.ts
|
|
36
129
|
import { z } from "zod";
|
|
37
130
|
function validated(schema, resolve) {
|
|
@@ -87,11 +180,17 @@ function sameFormats(a, b) {
|
|
|
87
180
|
return names.length === Object.keys(b).length && names.every((name) => Object.hasOwn(b, name) && a[name] === b[name]);
|
|
88
181
|
}
|
|
89
182
|
export {
|
|
183
|
+
FIELD_CODES,
|
|
184
|
+
INVALID_INPUT,
|
|
90
185
|
badUserInput,
|
|
91
186
|
constraintTypeDefs2 as constraintTypeDefs,
|
|
187
|
+
fieldIssueOf,
|
|
188
|
+
fieldsError,
|
|
189
|
+
inputPath,
|
|
190
|
+
invalidInputError,
|
|
92
191
|
validated,
|
|
93
192
|
withValidation
|
|
94
193
|
};
|
|
95
194
|
|
|
96
|
-
//# debugId=
|
|
195
|
+
//# debugId=BC885DEBB67481A464756E2164756E21
|
|
97
196
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/bad-user-input.ts", "../src/validated.ts", "../src/with-validation.ts"],
|
|
3
|
+
"sources": ["../src/bad-user-input.ts", "../src/invalid-input.ts", "../src/field-issue-of.ts", "../src/validated.ts", "../src/with-validation.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import { GraphQLError } from 'graphql';\nimport type { z } from 'zod';\nimport type { ConstraintArgument } from './rules';\n\n/** One reason an input was refused, at its path inside the arguments. */\nexport interface ValidationIssue {\n\t/** `['input', 'address', 'zip']`: the argument, then the input fields. */\n\treadonly path: readonly (string | number)[];\n\treadonly message: string;\n\t/** Zod's issue code: `too_small`, `invalid_format`, `custom`, ... */\n\treadonly code: string;\n\t/**\n\t * The `@constraint` argument that refused (`minLength`, `format`), when a\n\t * directive did: stable across Zod versions, unlike `code`. Absent for a\n\t * refusal of `validated()`'s own schema.\n\t */\n\treadonly constraint?: ConstraintArgument;\n}\n\n/** The `extensions` of the error an invalid input becomes. */\nexport type BadUserInputExtensions = {\n\treadonly code: 'BAD_USER_INPUT';\n\treadonly issues: readonly ValidationIssue[];\n};\n\n/** Names the `@constraint` argument behind an issue, when there is one. */\nexport type ConstraintOf = (\n\tissue: z.core.$ZodIssue,\n) => ConstraintArgument | undefined;\n\nfunction issuesOf(\n\terror: z.ZodError,\n\tconstraintOf?: ConstraintOf,\n): ValidationIssue[] {\n\treturn error.issues.map((issue) => {\n\t\tconst path = issue.path.map((key) =>\n\t\t\ttypeof key === 'number' ? key : String(key),\n\t\t);\n\t\tconst constraint = constraintOf?.(issue);\n\t\treturn constraint\n\t\t\t? { path, message: issue.message, code: issue.code, constraint }\n\t\t\t: { path, message: issue.message, code: issue.code };\n\t});\n}\n\nfunction errorOf(where: string, issues: ValidationIssue[]): GraphQLError {\n\tconst [first] = issues;\n\tconst reason = first\n\t\t? `${first.path.join('.') || 'arguments'}: ${first.message}`\n\t\t: 'refused';\n\tconst extensions: BadUserInputExtensions = { code: 'BAD_USER_INPUT', issues };\n\treturn new GraphQLError(`Invalid arguments for ${where}. ${reason}`, {\n\t\textensions,\n\t});\n}\n\n/**\n * The one error an invalid input becomes: `extensions.code` is\n * `BAD_USER_INPUT`, the code Apollo and most clients already read, and\n * `extensions.issues` lists every reason with its path, so a form can show\n * each one beside its field. The message names the field and the first\n * reason, for a log line.\n */\nexport function badUserInput(where: string, error: z.ZodError): GraphQLError {\n\treturn errorOf(where, issuesOf(error));\n}\n\n/** The parsed arguments, or the error above, its issues named by `constraintOf`. */\nexport async function parseArgs(\n\tschema: z.ZodType,\n\targs: unknown,\n\twhere: string,\n\tconstraintOf?: ConstraintOf,\n): Promise<unknown> {\n\tconst result = await schema.safeParseAsync(args);\n\tif (!result.success)\n\t\tthrow errorOf(where, issuesOf(result.error, constraintOf));\n\treturn result.data;\n}\n",
|
|
6
|
+
"import { GraphQLError, type GraphQLErrorOptions } from 'graphql';\n\n/** The code of the error `invalidInputError` builds, and its default message. */\nexport const INVALID_INPUT = 'validation.errors.invalid-input';\n\n/** Values a message is built from, such as `{ min: 2 }`. */\nexport type IssueParams = Record<string, string | number>;\n\n/** One field at fault: where it is, why, and what the reason's message needs. */\nexport interface FieldIssue {\n\t/** The field inside the input, dotted: `person.firstName`, `tags.1`. */\n\treadonly path: string;\n\t/** A code your client and your translations know: `validation.errors.too-short`. */\n\treadonly code: string;\n\t/** Values the message needs; an object with no key counts as none. */\n\treadonly params?: IssueParams;\n}\n\n/** One entry of `extensions.fields`: a `FieldIssue` with its message. */\nexport interface FieldError {\n\treadonly path: string;\n\treadonly code: string;\n\t/** `translate(code, params)`, or the code itself without a translator. */\n\treadonly message: string;\n\t/** Present only when the issue had params with at least one key. */\n\treadonly params?: IssueParams;\n}\n\n/** Turns a code (and its params) into the text a person reads. */\nexport type Translate = (code: string, params?: IssueParams) => string;\n\n/** The options of `invalidInputError`: GraphQL's own, and a translator. */\nexport type InvalidInputOptions = GraphQLErrorOptions & {\n\treadonly translate?: Translate;\n};\n\n/** The options of `fieldsError`: also an HTTP status and the error's own params. */\nexport type FieldsErrorOptions = InvalidInputOptions & {\n\t/** `extensions.http.status`, for a server that reads it (409, ...). */\n\treadonly status?: number;\n\treadonly params?: IssueParams;\n};\n\n/** `params` itself, or `undefined` when it holds no key: never `{}`. */\nfunction paramsOf(params: IssueParams | undefined): IssueParams | undefined {\n\treturn params && Object.keys(params).length > 0 ? params : undefined;\n}\n\nfunction messageOf(\n\tcode: string,\n\tparams: IssueParams | undefined,\n\ttranslate: Translate | undefined,\n): string {\n\treturn translate?.(code, params) ?? code;\n}\n\nfunction fieldsOf(\n\tissues: readonly FieldIssue[],\n\ttranslate: Translate | undefined,\n): FieldError[] {\n\treturn issues.map(({ path, code, params: given }) => {\n\t\tconst params = paramsOf(given);\n\t\treturn {\n\t\t\tpath,\n\t\t\tcode,\n\t\t\tmessage: messageOf(code, params, translate),\n\t\t\t...(params && { params }),\n\t\t};\n\t});\n}\n\n/**\n * One 400 error listing every field at fault, for an API that answers invalid\n * input this way: `extensions` is `{ code: 'validation.errors.invalid-input',\n * http: { status: 400 }, fields }`, each field with its translated message\n * (the code itself without `translate`), in the order given. GraphQL's\n * options (`originalError`, `path`, `nodes`, ...) are kept; an `extensions`\n * among them is replaced by this one.\n */\nexport function invalidInputError(\n\tissues: readonly FieldIssue[],\n\toptions: InvalidInputOptions = {},\n): GraphQLError {\n\tconst { translate, ...graphql } = options;\n\treturn new GraphQLError(messageOf(INVALID_INPUT, undefined, translate), {\n\t\t...graphql,\n\t\textensions: {\n\t\t\tcode: INVALID_INPUT,\n\t\t\thttp: { status: 400 },\n\t\t\tfields: fieldsOf(issues, translate),\n\t\t},\n\t});\n}\n\n/**\n * A refusal of your own (a 409 conflict, say) that names the fields at\n * fault: `extensions` is `{ code, params?, http?: { status }, fields }`, the\n * fields worded as `invalidInputError` words them, the message\n * `translate(code, params)` or the code. An `extensions` among the options\n * is replaced by this one.\n */\nexport function fieldsError(\n\tcode: string,\n\tissues: readonly FieldIssue[],\n\toptions: FieldsErrorOptions = {},\n): GraphQLError {\n\tconst { translate, status, params: given, ...graphql } = options;\n\tconst params = paramsOf(given);\n\treturn new GraphQLError(messageOf(code, params, translate), {\n\t\t...graphql,\n\t\textensions: {\n\t\t\tcode,\n\t\t\t...(params && { params }),\n\t\t\t...(status && { http: { status } }),\n\t\t\tfields: fieldsOf(issues, translate),\n\t\t},\n\t});\n}\n\n/**\n * A GraphQL or Zod path as a `FieldIssue` path: dotted, without the leading\n * `input` argument when a field follows it, a symbol written as\n * `String(key)`. `['input', 'person', 'firstName']` is `person.firstName`,\n * `['input', 'tags', 1]` is `tags.1`, `['first']` and `['input']` stay as\n * they are.\n */\nexport function inputPath(path: readonly PropertyKey[]): string {\n\tconst [first, ...rest] = path;\n\treturn (first === 'input' && rest.length > 0 ? rest : path)\n\t\t.map((key) => String(key))\n\t\t.join('.');\n}\n",
|
|
7
|
+
"import type { z } from 'zod';\nimport { type FieldIssue, inputPath } from './invalid-input';\n\n/**\n * The codes `fieldIssueOf` gives, each under `validation.errors.`: a string's\n * length (`too-short`, `too-long`), a number's or bigint's bound or multiple\n * (`out-of-range`), a list's or set's size (`too-few`, `too-many`), a named\n * format or a pattern (`invalid-format`), anything else (`invalid`).\n */\nexport const FIELD_CODES = {\n\ttooShort: 'validation.errors.too-short',\n\ttooLong: 'validation.errors.too-long',\n\toutOfRange: 'validation.errors.out-of-range',\n\ttooFew: 'validation.errors.too-few',\n\ttooMany: 'validation.errors.too-many',\n\tinvalidFormat: 'validation.errors.invalid-format',\n\tinvalid: 'validation.errors.invalid',\n} as const;\n\n/** The string checks Zod reports as `invalid_format` that are not a format. */\nconst TEXT_CHECKS: ReadonlySet<string> = new Set([\n\t'starts_with',\n\t'ends_with',\n\t'includes',\n]);\n\n/**\n * A Zod issue as a `FieldIssue`, for `invalidInputError`: its path through\n * `inputPath` (dotted, a leading `input` dropped) and one of `FIELD_CODES`,\n * without params. `too-short` / `too-long` for a string's length,\n * `out-of-range` for a number's or bigint's bound or a multiple, `too-few` /\n * `too-many` for a list's or set's size, `invalid-format` for a named format\n * or a pattern, `invalid` for anything else (a prefix, a suffix or a\n * substring included).\n */\nexport function fieldIssueOf(issue: z.core.$ZodIssue): FieldIssue {\n\treturn { path: inputPath(issue.path), code: codeOf(issue) };\n}\n\nfunction codeOf(issue: z.core.$ZodIssue): string {\n\tswitch (issue.code) {\n\t\tcase 'too_small':\n\t\tcase 'too_big': {\n\t\t\tconst small = issue.code === 'too_small';\n\t\t\tswitch (issue.origin) {\n\t\t\t\tcase 'string':\n\t\t\t\t\treturn small ? FIELD_CODES.tooShort : FIELD_CODES.tooLong;\n\t\t\t\tcase 'number':\n\t\t\t\tcase 'bigint':\n\t\t\t\t\treturn FIELD_CODES.outOfRange;\n\t\t\t\tcase 'array':\n\t\t\t\tcase 'set':\n\t\t\t\t\treturn small ? FIELD_CODES.tooFew : FIELD_CODES.tooMany;\n\t\t\t\tdefault:\n\t\t\t\t\treturn FIELD_CODES.invalid;\n\t\t\t}\n\t\t}\n\t\tcase 'not_multiple_of':\n\t\t\treturn FIELD_CODES.outOfRange;\n\t\tcase 'invalid_format':\n\t\t\t// A prefix, a suffix or a substring is not a format: only a named\n\t\t\t// format (`email`, `uuid`, …) or a pattern is.\n\t\t\treturn TEXT_CHECKS.has(issue.format)\n\t\t\t\t? FIELD_CODES.invalid\n\t\t\t\t: FIELD_CODES.invalidFormat;\n\t\tdefault:\n\t\t\treturn FIELD_CODES.invalid;\n\t}\n}\n",
|
|
6
8
|
"import type { GraphQLFieldResolver, GraphQLResolveInfo } from 'graphql';\nimport { z } from 'zod';\nimport { parseArgs } from './bad-user-input';\n\n/** An object schema of the arguments, or its shape. */\nexport type ArgsSchema = z.ZodType<Record<string, unknown>> | z.ZodRawShape;\n\n/** The schema `ArgsSchema` stands for. */\nexport type SchemaOf<S extends ArgsSchema> = S extends z.ZodType\n\t? S\n\t: z.ZodObject<S & z.ZodRawShape>;\n\n/**\n * A resolver whose arguments are checked by `schema` before it runs, for what\n * a `@constraint` cannot say: a rule across arguments, a business refinement,\n * an async check. It receives the parsed arguments (`z.output`), defaults and\n * transforms applied; an invalid input is the same `BAD_USER_INPUT` error as\n * the directives'.\n *\n * ```ts\n * signUp: validated(\n * z.object({ input: signUpSchema }).refine(({ input }) => input.password !== input.email),\n * (_, { input }) => users.create(input),\n * )\n * ```\n */\nexport function validated<\n\tS extends ArgsSchema,\n\tTSource = unknown,\n\tTContext = unknown,\n\tTResult = unknown,\n>(\n\tschema: S,\n\tresolve: (\n\t\tsource: TSource,\n\t\targs: z.output<SchemaOf<S>>,\n\t\tcontext: TContext,\n\t\tinfo: GraphQLResolveInfo,\n\t) => TResult,\n): GraphQLFieldResolver<\n\tTSource,\n\tTContext,\n\tz.input<SchemaOf<S>>,\n\tPromise<Awaited<TResult>>\n> {\n\t// `_zod`, not `instanceof`: a schema from another copy of zod is still one.\n\tconst checked = (\n\t\t'_zod' in schema ? schema : z.object(schema as z.ZodRawShape)\n\t) as z.ZodType;\n\treturn async (source, args, context, info): Promise<Awaited<TResult>> => {\n\t\tconst where = `${info.parentType.name}.${info.fieldName}`;\n\t\tconst parsed = (await parseArgs(checked, args, where)) as z.output<\n\t\t\tSchemaOf<S>\n\t\t>;\n\t\treturn await resolve(source, parsed, context, info);\n\t};\n}\n",
|
|
7
9
|
"import {\n\tdefaultFieldResolver,\n\ttype GraphQLField,\n\ttype GraphQLFieldResolver,\n\ttype GraphQLSchema,\n} from 'graphql';\nimport type { z } from 'zod';\nimport { parseArgs } from './bad-user-input';\nimport { checkConstraints } from './builder/check-constraints';\nimport type { FormatSchemas, OwnFormats } from './formats/registry';\nimport { constraintOf } from './rules';\n\n// Symbol.for: two copies of the package in one tree still wrap a field once.\nconst wrapped = Symbol.for('@nxgt/graphql-validation/wrapped');\n// The formats a schema was wrapped with, kept on the schema itself.\nconst wrappedWith = Symbol.for('@nxgt/graphql-validation/formats');\n\n/** What `withValidation` takes besides the schema. */\nexport interface WithValidationOptions<\n\tF extends FormatSchemas = FormatSchemas,\n> {\n\t/**\n\t * The application's own formats, which `@constraint(format: \"...\")` may\n\t * name beside the built-in ones: one Zod string schema each, keyed by its\n\t * name (lowercase letters, digits and hyphens). A built-in name is a type\n\t * error, and refused at startup.\n\t */\n\treadonly formats?: OwnFormats<F>;\n}\n\ntype Resolver = GraphQLFieldResolver<unknown, unknown> & { [wrapped]?: true };\n\nfunction checking(\n\tschema: z.ZodType,\n\twhere: string,\n\tresolve: Resolver,\n): Resolver {\n\tconst check: Resolver = async (source, args, context, info) =>\n\t\tawait resolve(\n\t\t\tsource,\n\t\t\tawait parseArgs(schema, args, where, constraintOf),\n\t\t\tcontext,\n\t\t\tinfo,\n\t\t);\n\tcheck[wrapped] = true;\n\treturn check;\n}\n\n/**\n * Wraps a field once. A subscription field is checked in `subscribe`, which\n * runs once per subscription — graphql's default one when the field has none\n * — and its `resolve` runs once per event, with the same arguments, already\n * checked.\n */\nfunction wrap(\n\tfield: GraphQLField<unknown, unknown>,\n\targs: z.ZodType,\n\twhere: string,\n\tsubscription: boolean,\n): void {\n\tconst subscribe = (field.subscribe ??\n\t\t(subscription ? defaultFieldResolver : undefined)) as Resolver | undefined;\n\tif (subscribe) {\n\t\tif (!subscribe[wrapped]) field.subscribe = checking(args, where, subscribe);\n\t\treturn;\n\t}\n\tconst resolve = (field.resolve ?? defaultFieldResolver) as Resolver;\n\tif (!resolve[wrapped]) field.resolve = checking(args, where, resolve);\n}\n\n/**\n * Checks every `@constraint` of `schema` before the resolvers run. Each\n * field with a constrained argument — directly, or anywhere inside an input\n * type it takes — gets its resolver (and its `subscribe`) wrapped: the\n * arguments are parsed by the Zod schema the directives describe, the\n * resolver receives the parsed value, and an invalid input becomes one\n * `BAD_USER_INPUT` error. Fields with nothing to check are left untouched.\n *\n * The schema is built here, once: a constraint that cannot apply (a\n * `minLength` on an `Int`, an unknown format, a bad pattern) throws now, not\n * on the first request. The resolvers are wrapped in place and the same\n * schema is returned, so it goes wherever it went before; calling it twice\n * wraps nothing twice. Call it last, once every resolver is attached: a\n * resolver set on a field afterwards replaces the check.\n *\n * A `@constraint` on an interface's argument must be repeated on each\n * object's field, which is the one that resolves; a field that drops it\n * fails here.\n *\n * The directives are read from the SDL (`astNode`), so a schema built from\n * type definitions — `buildSchema`, `makeExecutableSchema`, Yoga, Apollo —\n * is checked; a code-first schema with no SDL has no `@constraint` to read.\n *\n * `options.formats` adds the application's own formats; wrapping a schema\n * again throws unless the record has the same names, each the very same\n * schema (no formats after some, or some after none, throws too), since its\n * fields are already checked against the first ones.\n */\nexport function withValidation<\n\tS extends GraphQLSchema,\n\t// biome-ignore lint/complexity/noBannedTypes: no formats of its own by default\n\tconst F extends FormatSchemas = {},\n>(schema: S, options: WithValidationOptions<F> = {}): S {\n\tconst formats: FormatSchemas = options.formats ?? {};\n\tconst holder = schema as unknown as { [wrappedWith]?: FormatSchemas };\n\tconst before = holder[wrappedWith];\n\tif (before && !sameFormats(before, formats)) {\n\t\tthrow new Error(\n\t\t\t'withValidation: this schema is already wrapped with other formats. Call withValidation once, with every format.',\n\t\t);\n\t}\n\tconst directive = checkConstraints(\n\t\tschema,\n\t\t(type, field, args) => {\n\t\t\tconst subscription = type === schema.getSubscriptionType();\n\t\t\twrap(field, args, `${type.name}.${field.name}`, subscription);\n\t\t},\n\t\tformats,\n\t);\n\tif (!directive) {\n\t\tthrow new Error(\n\t\t\t'withValidation: this schema declares no @constraint directive. Add constraintTypeDefs to its type definitions.',\n\t\t);\n\t}\n\tif (!before) Object.defineProperty(schema, wrappedWith, { value: formats });\n\treturn schema;\n}\n\n/** The same names, each the very same schema. */\nfunction sameFormats(a: FormatSchemas, b: FormatSchemas): boolean {\n\tconst names = Object.keys(a);\n\treturn (\n\t\tnames.length === Object.keys(b).length &&\n\t\tnames.every((name) => Object.hasOwn(b, name) && a[name] === b[name])\n\t);\n}\n"
|
|
8
10
|
],
|
|
9
|
-
"mappings": ";;;;;;;;;AAAA;AA8BA,SAAS,QAAQ,CAChB,OACA,cACoB;AAAA,EACpB,OAAO,MAAM,OAAO,IAAI,CAAC,UAAU;AAAA,IAClC,MAAM,OAAO,MAAM,KAAK,IAAI,CAAC,QAC5B,OAAO,QAAQ,WAAW,MAAM,OAAO,GAAG,CAC3C;AAAA,IACA,MAAM,aAAa,eAAe,KAAK;AAAA,IACvC,OAAO,aACJ,EAAE,MAAM,SAAS,MAAM,SAAS,MAAM,MAAM,MAAM,WAAW,IAC7D,EAAE,MAAM,SAAS,MAAM,SAAS,MAAM,MAAM,KAAK;AAAA,GACpD;AAAA;AAGF,SAAS,OAAO,CAAC,OAAe,QAAyC;AAAA,EACxE,OAAO,SAAS;AAAA,EAChB,MAAM,SAAS,QACZ,GAAG,MAAM,KAAK,KAAK,GAAG,KAAK,gBAAgB,MAAM,YACjD;AAAA,EACH,MAAM,aAAqC,EAAE,MAAM,kBAAkB,OAAO;AAAA,EAC5E,OAAO,IAAI,aAAa,yBAAyB,UAAU,UAAU;AAAA,IACpE;AAAA,EACD,CAAC;AAAA;AAUK,SAAS,YAAY,CAAC,OAAe,OAAiC;AAAA,EAC5E,OAAO,QAAQ,OAAO,SAAS,KAAK,CAAC;AAAA;AAItC,e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,CAAC,WAAW;AAAA,IACf,MAAM,IAAI,MACT,gHACD;AAAA,EACD;AAAA,EACA,IAAI,CAAC;AAAA,IAAQ,OAAO,eAAe,QAAQ,aAAa,EAAE,OAAO,QAAQ,CAAC;AAAA,EAC1E,OAAO;AAAA;AAIR,SAAS,WAAW,CAAC,GAAkB,GAA2B;AAAA,EACjE,MAAM,QAAQ,OAAO,KAAK,CAAC;AAAA,EAC3B,OACC,MAAM,WAAW,OAAO,KAAK,CAAC,EAAE,UAChC,MAAM,MAAM,CAAC,SAAS,OAAO,OAAO,GAAG,IAAI,KAAK,EAAE,UAAU,EAAE,KAAK;AAAA;",
|
|
12
|
+
"debugId": "BC885DEBB67481A464756E2164756E21",
|
|
11
13
|
"names": []
|
|
12
14
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { GraphQLError, type GraphQLErrorOptions } from 'graphql';
|
|
2
|
+
/** The code of the error `invalidInputError` builds, and its default message. */
|
|
3
|
+
export declare const INVALID_INPUT = "validation.errors.invalid-input";
|
|
4
|
+
/** Values a message is built from, such as `{ min: 2 }`. */
|
|
5
|
+
export type IssueParams = Record<string, string | number>;
|
|
6
|
+
/** One field at fault: where it is, why, and what the reason's message needs. */
|
|
7
|
+
export interface FieldIssue {
|
|
8
|
+
/** The field inside the input, dotted: `person.firstName`, `tags.1`. */
|
|
9
|
+
readonly path: string;
|
|
10
|
+
/** A code your client and your translations know: `validation.errors.too-short`. */
|
|
11
|
+
readonly code: string;
|
|
12
|
+
/** Values the message needs; an object with no key counts as none. */
|
|
13
|
+
readonly params?: IssueParams;
|
|
14
|
+
}
|
|
15
|
+
/** One entry of `extensions.fields`: a `FieldIssue` with its message. */
|
|
16
|
+
export interface FieldError {
|
|
17
|
+
readonly path: string;
|
|
18
|
+
readonly code: string;
|
|
19
|
+
/** `translate(code, params)`, or the code itself without a translator. */
|
|
20
|
+
readonly message: string;
|
|
21
|
+
/** Present only when the issue had params with at least one key. */
|
|
22
|
+
readonly params?: IssueParams;
|
|
23
|
+
}
|
|
24
|
+
/** Turns a code (and its params) into the text a person reads. */
|
|
25
|
+
export type Translate = (code: string, params?: IssueParams) => string;
|
|
26
|
+
/** The options of `invalidInputError`: GraphQL's own, and a translator. */
|
|
27
|
+
export type InvalidInputOptions = GraphQLErrorOptions & {
|
|
28
|
+
readonly translate?: Translate;
|
|
29
|
+
};
|
|
30
|
+
/** The options of `fieldsError`: also an HTTP status and the error's own params. */
|
|
31
|
+
export type FieldsErrorOptions = InvalidInputOptions & {
|
|
32
|
+
/** `extensions.http.status`, for a server that reads it (409, ...). */
|
|
33
|
+
readonly status?: number;
|
|
34
|
+
readonly params?: IssueParams;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* One 400 error listing every field at fault, for an API that answers invalid
|
|
38
|
+
* input this way: `extensions` is `{ code: 'validation.errors.invalid-input',
|
|
39
|
+
* http: { status: 400 }, fields }`, each field with its translated message
|
|
40
|
+
* (the code itself without `translate`), in the order given. GraphQL's
|
|
41
|
+
* options (`originalError`, `path`, `nodes`, ...) are kept; an `extensions`
|
|
42
|
+
* among them is replaced by this one.
|
|
43
|
+
*/
|
|
44
|
+
export declare function invalidInputError(issues: readonly FieldIssue[], options?: InvalidInputOptions): GraphQLError;
|
|
45
|
+
/**
|
|
46
|
+
* A refusal of your own (a 409 conflict, say) that names the fields at
|
|
47
|
+
* fault: `extensions` is `{ code, params?, http?: { status }, fields }`, the
|
|
48
|
+
* fields worded as `invalidInputError` words them, the message
|
|
49
|
+
* `translate(code, params)` or the code. An `extensions` among the options
|
|
50
|
+
* is replaced by this one.
|
|
51
|
+
*/
|
|
52
|
+
export declare function fieldsError(code: string, issues: readonly FieldIssue[], options?: FieldsErrorOptions): GraphQLError;
|
|
53
|
+
/**
|
|
54
|
+
* A GraphQL or Zod path as a `FieldIssue` path: dotted, without the leading
|
|
55
|
+
* `input` argument when a field follows it, a symbol written as
|
|
56
|
+
* `String(key)`. `['input', 'person', 'firstName']` is `person.firstName`,
|
|
57
|
+
* `['input', 'tags', 1]` is `tags.1`, `['first']` and `['input']` stay as
|
|
58
|
+
* they are.
|
|
59
|
+
*/
|
|
60
|
+
export declare function inputPath(path: readonly PropertyKey[]): string;
|
|
61
|
+
//# sourceMappingURL=invalid-input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invalid-input.d.ts","sourceRoot":"","sources":["../src/invalid-input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEjE,iFAAiF;AACjF,eAAO,MAAM,aAAa,oCAAoC,CAAC;AAE/D,4DAA4D;AAC5D,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AAE1D,iFAAiF;AACjF,MAAM,WAAW,UAAU;IAC1B,wEAAwE;IACxE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,oFAAoF;IACpF,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B;AAED,yEAAyE;AACzE,MAAM,WAAW,UAAU;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,oEAAoE;IACpE,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B;AAED,kEAAkE;AAClE,MAAM,MAAM,SAAS,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,MAAM,CAAC;AAEvE,2EAA2E;AAC3E,MAAM,MAAM,mBAAmB,GAAG,mBAAmB,GAAG;IACvD,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;CAC/B,CAAC;AAEF,oFAAoF;AACpF,MAAM,MAAM,kBAAkB,GAAG,mBAAmB,GAAG;IACtD,uEAAuE;IACvE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AA8BF;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,SAAS,UAAU,EAAE,EAC7B,OAAO,GAAE,mBAAwB,GAC/B,YAAY,CAUd;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAC1B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,SAAS,UAAU,EAAE,EAC7B,OAAO,GAAE,kBAAuB,GAC9B,YAAY,CAYd;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,CAK9D"}
|
package/docs/README.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
| Page | Read it when |
|
|
6
6
|
| --- | --- |
|
|
7
7
|
| [Constraints](guide/constraints.md) | you write `@constraint` in your schema: setup with `withValidation`, every argument and its Zod equivalent, the formats and your own, lists and nested inputs |
|
|
8
|
+
| [Answering invalid input as one error](guide/invalid-input.md) | your API answers invalid input as one 400 error with a list of fields (`invalidInputError`, `fieldIssueOf` for a Zod parse), or a refusal such as a 409 names the fields at fault (`fieldsError`) |
|
|
8
9
|
| [Errors](guide/errors.md) | you handle the `BAD_USER_INPUT` error in a client, build it yourself with `badUserInput`, or add rules a directive cannot say with `validated` |
|
|
9
10
|
|
|
10
11
|
## Reference
|
|
@@ -302,10 +302,9 @@ What a format of your own is:
|
|
|
302
302
|
then at most 64 characters. A check of yours with `abort: true`
|
|
303
303
|
(`.refine(…, { message, abort: true })`) stops the rules after it, on the
|
|
304
304
|
server as in a generated client: only its issue is returned. Once an async
|
|
305
|
-
check of your format is pending,
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
async one.
|
|
305
|
+
check of your format is pending, a client has already run the rules, so
|
|
306
|
+
the abort stops nothing on either side; the server lists your format's
|
|
307
|
+
issues first. Put an aborting check before any async one.
|
|
309
308
|
- **Its messages are yours.** A refusal reads as your schema writes it (the
|
|
310
309
|
package's own messages never name the value; yours may, so keep the input
|
|
311
310
|
out of them if your logs or clients should not see it). Every issue your
|
|
@@ -319,13 +318,30 @@ What a format of your own is:
|
|
|
319
318
|
- **Named in lowercase letters, digits and hyphens**, starting with a letter
|
|
320
319
|
(`siret`, `work-email`, `iso-6346`). A built-in name (`email`, `uuid`, …)
|
|
321
320
|
cannot be replaced: it is a type error, and refused at startup.
|
|
322
|
-
- **An async check stays async.** `withValidation` parses
|
|
323
|
-
`.refine(async …)`
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
321
|
+
- **An async check stays async, and runs once.** `withValidation` parses
|
|
322
|
+
asynchronously, so a `.refine(async …)` (or an async `.superRefine()`, or
|
|
323
|
+
any check returning a promise) works in a request, from this package's zod
|
|
324
|
+
or another copy. It runs once per value, from the first, and when it
|
|
325
|
+
rejects (`db down`) the operation fails with that error, as a resolver's
|
|
326
|
+
would: no rejection is left unhandled. A default value is checked at
|
|
327
|
+
startup synchronously, so a field whose format is async cannot have a
|
|
327
328
|
default: startup throws `The default value of Query.a(s:) cannot be checked
|
|
328
|
-
at startup: the format "free" checks asynchronously, …`.
|
|
329
|
+
at startup: the format "free" checks asynchronously, …`. A check that
|
|
330
|
+
throws on the default value reads the same, since its error ends a run
|
|
331
|
+
startup cannot wait for.
|
|
332
|
+
|
|
333
|
+
- **Only Zod's public API runs it.** Your format is parsed with
|
|
334
|
+
`safeParseAsync` only, never a synchronous `safeParse` that would start an
|
|
335
|
+
async check and drop it; a `.catch()` and `.refine()`s chained after your
|
|
336
|
+
checks tell whether the run finished at once and which abort stopped it,
|
|
337
|
+
and the rules after it run inside it, replayed on its issues as Zod runs
|
|
338
|
+
them when chained on your schema. No Zod internal is called, so a Zod 4
|
|
339
|
+
release that changes its internals changes nothing here, and a format
|
|
340
|
+
from another copy of Zod (another version included) gets its messages
|
|
341
|
+
from that copy's configuration (`z.config()`). Startup still reads the
|
|
342
|
+
schema's definition (`_zod.def`, Zod's introspection) to refuse what
|
|
343
|
+
rewrites the value. The cost, measured on Bun 1.4.2 against 0.3.0: about
|
|
344
|
+
0.1 µs more per accepted value and 1.5 µs more per refused one.
|
|
329
345
|
|
|
330
346
|
Call `withValidation` once, with every format. A second call on the same
|
|
331
347
|
schema wraps nothing when its record has the same names, each the very same
|
package/docs/guide/errors.md
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
The error an invalid input becomes, how a client reads it, and `validated` for
|
|
4
4
|
rules a `@constraint` cannot say. For declaring constraints, see
|
|
5
|
-
[Constraints](constraints.md).
|
|
5
|
+
[Constraints](constraints.md). For an API that answers invalid input as one
|
|
6
|
+
400 error with a list of fields, see
|
|
7
|
+
[Answering invalid input as one error](invalid-input.md).
|
|
6
8
|
|
|
7
9
|
## The error
|
|
8
10
|
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# Answering invalid input as one error
|
|
2
|
+
|
|
3
|
+
Some APIs answer invalid input as **one 400 error with a list of fields**,
|
|
4
|
+
each with a code a client translates or a message already translated, rather
|
|
5
|
+
than as [`BAD_USER_INPUT`](errors.md) with Zod's issues. `invalidInputError`
|
|
6
|
+
and `fieldsError` build that error, so every resolver and every module words
|
|
7
|
+
it the same way. They do not change what `withValidation` and `validated`
|
|
8
|
+
raise: those still throw `BAD_USER_INPUT`.
|
|
9
|
+
|
|
10
|
+
## The error
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { invalidInputError } from '@nxgt/graphql-validation';
|
|
14
|
+
|
|
15
|
+
throw invalidInputError(
|
|
16
|
+
[
|
|
17
|
+
{ path: 'person.lastName', code: 'validation.errors.required' },
|
|
18
|
+
{ path: 'person.firstName', code: 'validation.errors.too-short', params: { min: 2 } },
|
|
19
|
+
],
|
|
20
|
+
{ translate: (code, params) => t(code, params) }, // your i18n function
|
|
21
|
+
);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"errors": [
|
|
27
|
+
{
|
|
28
|
+
"message": "Invalid input",
|
|
29
|
+
"path": ["createPerson"],
|
|
30
|
+
"extensions": {
|
|
31
|
+
"code": "validation.errors.invalid-input",
|
|
32
|
+
"http": { "status": 400 },
|
|
33
|
+
"fields": [
|
|
34
|
+
{ "path": "person.lastName", "code": "validation.errors.required", "message": "Required" },
|
|
35
|
+
{ "path": "person.firstName", "code": "validation.errors.too-short", "message": "At least 2 characters", "params": { "min": 2 } }
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
"data": { "createPerson": null }
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
- `message` is `translate('validation.errors.invalid-input')`, or that code
|
|
45
|
+
itself without `translate` (also exported as `INVALID_INPUT`).
|
|
46
|
+
- `extensions.code` is `validation.errors.invalid-input`, and
|
|
47
|
+
`extensions.http.status` is `400`, for a server that sets the response's
|
|
48
|
+
HTTP status from it (GraphQL Yoga and Apollo Server read this key).
|
|
49
|
+
- `extensions.fields` holds one entry per issue, **in the order given**
|
|
50
|
+
(duplicates kept): `path` and `code` as you wrote them, `message` as
|
|
51
|
+
`translate(code, params)` or the code itself, and `params` only when the
|
|
52
|
+
issue has some.
|
|
53
|
+
- **`params` is never `{}`**: an issue whose `params` has no key is written
|
|
54
|
+
without it, and `translate` receives `undefined` for it.
|
|
55
|
+
- A `translate` that returns `undefined` at run time (a missing key in some
|
|
56
|
+
i18n libraries) falls back to the code, as if there were none.
|
|
57
|
+
|
|
58
|
+
## GraphQL's options
|
|
59
|
+
|
|
60
|
+
The second argument also takes `GraphQLError`'s own options, so the error
|
|
61
|
+
keeps its cause and its place in the response:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
throw invalidInputError(issues, {
|
|
65
|
+
translate,
|
|
66
|
+
originalError: zodError, // kept: error.originalError
|
|
67
|
+
path: info.path ? [info.path.key] : undefined,
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**`extensions` among them is replaced**, not merged: the error's
|
|
72
|
+
`extensions` is always exactly the shape above. Put anything else your
|
|
73
|
+
server needs in a `formatError` hook.
|
|
74
|
+
|
|
75
|
+
## From a resolver
|
|
76
|
+
|
|
77
|
+
A check that needs the database, or a rule your schema cannot say, ends with
|
|
78
|
+
the same error:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { type FieldIssue, invalidInputError } from '@nxgt/graphql-validation';
|
|
82
|
+
|
|
83
|
+
const resolvers = {
|
|
84
|
+
Mutation: {
|
|
85
|
+
async createPerson(_: unknown, { input }: { input: PersonInput }, { t }: Context) {
|
|
86
|
+
const issues: FieldIssue[] = [];
|
|
87
|
+
if (!input.person.lastName.trim())
|
|
88
|
+
issues.push({ path: 'person.lastName', code: 'validation.errors.required' });
|
|
89
|
+
if (input.person.birthDate > new Date())
|
|
90
|
+
issues.push({ path: 'person.birthDate', code: 'validation.errors.in-the-future' });
|
|
91
|
+
if (issues.length) throw invalidInputError(issues, { translate: t });
|
|
92
|
+
return people.create(input.person);
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## From a zod parse
|
|
99
|
+
|
|
100
|
+
`fieldIssueOf(issue)` maps one Zod issue to a `FieldIssue`: its path through
|
|
101
|
+
`inputPath`, and a code under `validation.errors.`, without params:
|
|
102
|
+
|
|
103
|
+
| Zod issue | `code` |
|
|
104
|
+
| --- | --- |
|
|
105
|
+
| a string too short or too long (`min`, `max`, `length`) | `validation.errors.too-short` / `validation.errors.too-long` |
|
|
106
|
+
| a number or bigint out of its bounds, or not a multiple | `validation.errors.out-of-range` |
|
|
107
|
+
| a list or set too small or too big | `validation.errors.too-few` / `validation.errors.too-many` |
|
|
108
|
+
| a named format (`email`, `uuid`, …) or a pattern (`regex`) | `validation.errors.invalid-format` |
|
|
109
|
+
| anything else: `startsWith`, `endsWith`, `includes`, a refinement, a wrong type, a date's bound | `validation.errors.invalid` |
|
|
110
|
+
|
|
111
|
+
The codes are also exported as `FIELD_CODES` (`FIELD_CODES.tooShort`, …),
|
|
112
|
+
for your translations.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { fieldIssueOf, invalidInputError } from '@nxgt/graphql-validation';
|
|
116
|
+
import { z } from 'zod';
|
|
117
|
+
|
|
118
|
+
const PersonInput = z.object({
|
|
119
|
+
person: z.object({
|
|
120
|
+
firstName: z.string().min(2),
|
|
121
|
+
email: z.email(),
|
|
122
|
+
}),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const result = PersonInput.safeParse(input);
|
|
126
|
+
if (!result.success)
|
|
127
|
+
throw invalidInputError(result.error.issues.map(fieldIssueOf), {
|
|
128
|
+
translate: t,
|
|
129
|
+
originalError: result.error,
|
|
130
|
+
});
|
|
131
|
+
// fields: person.firstName (validation.errors.too-short),
|
|
132
|
+
// person.email (validation.errors.invalid-format)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
[`@nxgt/graphql-codegen-msw`](https://github.com/softistx/nxgt-graphql/blob/develop/packages/graphql-codegen-msw/docs/guide/handlers.md)'s
|
|
136
|
+
mocks apply the same mapping by default, so a mocked refusal reads as the
|
|
137
|
+
server's. For a code of your own, or params (`{ min: 2 }`), map the issue
|
|
138
|
+
yourself and keep `fieldIssueOf` for the rest:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { type FieldIssue, fieldIssueOf } from '@nxgt/graphql-validation';
|
|
142
|
+
|
|
143
|
+
const issueOf = (issue: z.core.$ZodIssue): FieldIssue =>
|
|
144
|
+
issue.code === 'too_small' && issue.origin === 'string'
|
|
145
|
+
? { ...fieldIssueOf(issue), params: { min: Number(issue.minimum) } }
|
|
146
|
+
: fieldIssueOf(issue);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`inputPath` turns a GraphQL or Zod path into a dotted one, dropping the
|
|
150
|
+
leading `input` argument: `inputPath(['input', 'person', 'firstName'])` is
|
|
151
|
+
`person.firstName`, `['input', 'tags', 1]` is `tags.1`; a path that does not
|
|
152
|
+
start with `input` (`['first']`), or is `['input']` alone, is only joined
|
|
153
|
+
with dots. It takes a Zod issue's path as it is, a symbol key written as
|
|
154
|
+
`String(key)`.
|
|
155
|
+
|
|
156
|
+
The same mapping reads [`BAD_USER_INPUT`](errors.md)'s `issues[].path`, which
|
|
157
|
+
starts with the argument name.
|
|
158
|
+
|
|
159
|
+
## A refusal that names fields: `fieldsError`
|
|
160
|
+
|
|
161
|
+
For a refusal of your own that is not a 400, a 409 conflict for instance,
|
|
162
|
+
that still points at the fields at fault:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import { fieldsError } from '@nxgt/graphql-validation';
|
|
166
|
+
|
|
167
|
+
throw fieldsError(
|
|
168
|
+
'person.errors.duplicate',
|
|
169
|
+
[{ path: 'person.email', code: 'person.errors.email-taken' }],
|
|
170
|
+
{ translate: t, status: 409, params: { name: existing.name } },
|
|
171
|
+
);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
```json
|
|
175
|
+
{
|
|
176
|
+
"message": "Already registered as Ada",
|
|
177
|
+
"extensions": {
|
|
178
|
+
"code": "person.errors.duplicate",
|
|
179
|
+
"params": { "name": "Ada" },
|
|
180
|
+
"http": { "status": 409 },
|
|
181
|
+
"fields": [
|
|
182
|
+
{ "path": "person.email", "code": "person.errors.email-taken", "message": "This email is taken" }
|
|
183
|
+
]
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
- `message` is `translate(code, params)`, or `code`.
|
|
189
|
+
- `extensions` is `{ code, params?, http?, fields }`, in that order:
|
|
190
|
+
`params` only when it has a key, `http: { status }` only when `status` is
|
|
191
|
+
given, `fields` worded as `invalidInputError` words them.
|
|
192
|
+
- GraphQL's options are kept and an `extensions` among them is replaced, as
|
|
193
|
+
above.
|
|
194
|
+
|
|
195
|
+
## Types
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import type { GraphQLError, GraphQLErrorOptions } from 'graphql';
|
|
199
|
+
import type { z } from 'zod';
|
|
200
|
+
|
|
201
|
+
type IssueParams = Record<string, string | number>;
|
|
202
|
+
|
|
203
|
+
interface FieldIssue {
|
|
204
|
+
readonly path: string;
|
|
205
|
+
readonly code: string;
|
|
206
|
+
readonly params?: IssueParams; // {} counts as none
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** One entry of `extensions.fields`. */
|
|
210
|
+
interface FieldError {
|
|
211
|
+
readonly path: string;
|
|
212
|
+
readonly code: string;
|
|
213
|
+
readonly message: string;
|
|
214
|
+
readonly params?: IssueParams;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
type Translate = (code: string, params?: IssueParams) => string;
|
|
218
|
+
|
|
219
|
+
type InvalidInputOptions = GraphQLErrorOptions & { readonly translate?: Translate };
|
|
220
|
+
type FieldsErrorOptions = InvalidInputOptions & {
|
|
221
|
+
readonly status?: number;
|
|
222
|
+
readonly params?: IssueParams;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
declare const INVALID_INPUT: 'validation.errors.invalid-input';
|
|
226
|
+
declare function invalidInputError(issues: readonly FieldIssue[], options?: InvalidInputOptions): GraphQLError;
|
|
227
|
+
declare function fieldsError(code: string, issues: readonly FieldIssue[], options?: FieldsErrorOptions): GraphQLError;
|
|
228
|
+
declare function inputPath(path: readonly PropertyKey[]): string;
|
|
229
|
+
|
|
230
|
+
declare const FIELD_CODES: {
|
|
231
|
+
readonly tooShort: 'validation.errors.too-short';
|
|
232
|
+
readonly tooLong: 'validation.errors.too-long';
|
|
233
|
+
readonly outOfRange: 'validation.errors.out-of-range';
|
|
234
|
+
readonly tooFew: 'validation.errors.too-few';
|
|
235
|
+
readonly tooMany: 'validation.errors.too-many';
|
|
236
|
+
readonly invalidFormat: 'validation.errors.invalid-format';
|
|
237
|
+
readonly invalid: 'validation.errors.invalid';
|
|
238
|
+
};
|
|
239
|
+
declare function fieldIssueOf(issue: z.core.$ZodIssue): FieldIssue;
|
|
240
|
+
```
|
package/docs/roadmap.md
CHANGED
|
@@ -4,19 +4,35 @@ What `@nxgt/graphql-validation` is heading for, phrased as what you get.
|
|
|
4
4
|
|
|
5
5
|
## Now
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
`@constraint(format: "...")`: `withValidation(schema, { formats })`, one
|
|
9
|
-
record your module exports, checked at startup, its refusals named
|
|
10
|
-
`format`. `@nxgt/graphql-codegen-zod` 0.3 reads the same record, so the
|
|
11
|
-
client refuses what the server refuses, with the same messages.
|
|
7
|
+
Nothing in progress.
|
|
12
8
|
|
|
13
9
|
## Next
|
|
14
10
|
|
|
15
11
|
- **Schemas that read the context**, `(args, context) => schema`, for checks
|
|
16
12
|
that need the request (uniqueness in a database, permissions).
|
|
17
13
|
|
|
14
|
+
## Later
|
|
15
|
+
|
|
16
|
+
- **Emit the invalid-input shape directly.** An option for `withValidation`
|
|
17
|
+
and `validated` to raise the `invalidInputError` shape in place of
|
|
18
|
+
`BAD_USER_INPUT`, so an API using it needs no mapping of its own.
|
|
19
|
+
|
|
18
20
|
## Shipped
|
|
19
21
|
|
|
22
|
+
- **Answering invalid input as one error** — 0.4.0.
|
|
23
|
+
`invalidInputError(issues, { translate })` builds one 400 error whose
|
|
24
|
+
`extensions.fields` lists every field at fault with your own codes,
|
|
25
|
+
translated; `fieldsError` does the same for a refusal such as a 409
|
|
26
|
+
conflict; `fieldIssueOf` maps a Zod issue to a field and a code
|
|
27
|
+
(`too-short`, `invalid-format`, …), the mapping
|
|
28
|
+
[`@nxgt/graphql-codegen-msw`](https://www.npmjs.com/package/@nxgt/graphql-codegen-msw)'s
|
|
29
|
+
mocks apply; `inputPath` turns a GraphQL or Zod path into `person.firstName`.
|
|
30
|
+
- **Your own formats** — 0.3.0. Name a Zod string schema in
|
|
31
|
+
`@constraint(format: "...")`: `withValidation(schema, { formats })`, one
|
|
32
|
+
record your module exports, checked at startup, its refusals named
|
|
33
|
+
`format`. `@nxgt/graphql-codegen-zod` 0.3 reads the same record, so the
|
|
34
|
+
client refuses what the server refuses, with the same messages. From
|
|
35
|
+
0.3.1 they run through Zod's public API only.
|
|
20
36
|
- **Declarations resolve under `nodenext`** — 0.2.1. `moduleResolution`
|
|
21
37
|
`nodenext` and `node16` find every export, as `bundler` does.
|
|
22
38
|
- **A Zod codegen plugin** — 0.2.0. [`@nxgt/graphql-codegen-zod`](https://www.npmjs.com/package/@nxgt/graphql-codegen-zod)
|