@nxgt/graphql-validation 0.1.0 → 0.2.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.
Files changed (57) hide show
  1. package/README.md +27 -1
  2. package/dist/bad-user-input.d.ts +1 -1
  3. package/dist/builder/args-schema.d.ts +1 -1
  4. package/dist/builder/check-constraints.d.ts +15 -0
  5. package/dist/builder/check-constraints.d.ts.map +1 -0
  6. package/dist/builder/constraints.d.ts +1 -1
  7. package/dist/builder/input-code.d.ts +29 -0
  8. package/dist/builder/input-code.d.ts.map +1 -0
  9. package/dist/builder/input-schema.d.ts.map +1 -1
  10. package/dist/builder/leaf.d.ts +14 -4
  11. package/dist/builder/leaf.d.ts.map +1 -1
  12. package/dist/chunks/{index-n3hypgpf.js → index-rbxe5m3y.js} +7 -3
  13. package/dist/chunks/{index-n3hypgpf.js.map → index-rbxe5m3y.js.map} +3 -3
  14. package/dist/chunks/index-s1kkmg35.js +266 -0
  15. package/dist/chunks/index-s1kkmg35.js.map +14 -0
  16. package/dist/cli.js +1 -1
  17. package/dist/codegen.d.ts +4 -0
  18. package/dist/codegen.d.ts.map +1 -0
  19. package/dist/codegen.js +56 -0
  20. package/dist/codegen.js.map +10 -0
  21. package/dist/formats/all.d.ts +8 -8
  22. package/dist/formats/byte.d.ts +1 -1
  23. package/dist/formats/date-time.d.ts +1 -1
  24. package/dist/formats/date.d.ts +1 -1
  25. package/dist/formats/email.d.ts +1 -1
  26. package/dist/formats/index.d.ts +2 -2
  27. package/dist/formats/ipv4.d.ts +1 -1
  28. package/dist/formats/ipv6.d.ts +1 -1
  29. package/dist/formats/uri.d.ts +1 -1
  30. package/dist/formats/uuid.d.ts +1 -1
  31. package/dist/index.d.ts +5 -5
  32. package/dist/index.js +12 -243
  33. package/dist/index.js.map +4 -8
  34. package/dist/rules/all.d.ts +15 -15
  35. package/dist/rules/contains.d.ts +1 -1
  36. package/dist/rules/ends-with.d.ts +1 -1
  37. package/dist/rules/exclusive-max.d.ts +1 -1
  38. package/dist/rules/exclusive-min.d.ts +1 -1
  39. package/dist/rules/format.d.ts +1 -1
  40. package/dist/rules/index.d.ts +3 -1
  41. package/dist/rules/index.d.ts.map +1 -1
  42. package/dist/rules/max-items.d.ts +1 -1
  43. package/dist/rules/max-length.d.ts +1 -1
  44. package/dist/rules/max.d.ts +1 -1
  45. package/dist/rules/min-items.d.ts +1 -1
  46. package/dist/rules/min-length.d.ts +1 -1
  47. package/dist/rules/min.d.ts +1 -1
  48. package/dist/rules/multiple-of.d.ts +1 -1
  49. package/dist/rules/not-contains.d.ts +1 -1
  50. package/dist/rules/pattern.d.ts +1 -1
  51. package/dist/rules/rule.d.ts +1 -1
  52. package/dist/rules/starts-with.d.ts +1 -1
  53. package/dist/with-validation.d.ts.map +1 -1
  54. package/docs/guide/constraints.md +1 -1
  55. package/docs/roadmap.md +6 -3
  56. package/docs/troubleshooting.md +14 -2
  57. package/package.json +7 -1
@@ -0,0 +1,56 @@
1
+ import {
2
+ applyRuleCode
3
+ } from "./chunks/index-rbxe5m3y.js";
4
+ import {
5
+ constraintsOn2,
6
+ assertLeafTargets,
7
+ assertObjectTargets,
8
+ baseCode,
9
+ checkConstraints2
10
+ } from "./chunks/index-s1kkmg35.js";
11
+ // src/builder/input-code.ts
12
+ import {
13
+ getNamedType,
14
+ isInputObjectType,
15
+ isListType,
16
+ isNonNullType
17
+ } from "graphql";
18
+ function inputCode(type, constraints, where, named, options = {}) {
19
+ const write = { where, named, options };
20
+ return typedCode(write, type, constraints);
21
+ }
22
+ function typedCode(write, type, constraints) {
23
+ if (isNonNullType(type))
24
+ return requiredCode(write, type.ofType, constraints);
25
+ return `${requiredCode(write, type, constraints)}.nullish()`;
26
+ }
27
+ function requiredCode(write, type, constraints) {
28
+ if (isNonNullType(type))
29
+ return requiredCode(write, type.ofType, constraints);
30
+ if (isListType(type)) {
31
+ const own = constraints.filter(({ rule }) => rule.target === "list");
32
+ const items = constraints.filter(({ rule }) => rule.target !== "list");
33
+ const code = applyCode(`z.array(${typedCode(write, type.ofType, items)})`, own);
34
+ if (!write.options.list)
35
+ return code;
36
+ const single = requiredCode(write, getNamedType(type), []);
37
+ return write.options.list({ type, code, single });
38
+ }
39
+ if (isInputObjectType(type)) {
40
+ assertObjectTargets(type.name, constraints, write.where);
41
+ return write.named(type);
42
+ }
43
+ assertLeafTargets(type, constraints, write.where);
44
+ return applyCode(baseCode(type) ?? write.named(type), constraints);
45
+ }
46
+ function applyCode(source, constraints) {
47
+ return constraints.reduce((code, { rule, value }) => applyRuleCode(rule, code, value), source);
48
+ }
49
+ export {
50
+ checkConstraints2 as checkConstraints,
51
+ constraintsOn2 as constraintsOn,
52
+ inputCode
53
+ };
54
+
55
+ //# debugId=CF7DECB6FA09613D64756E2164756E21
56
+ //# sourceMappingURL=codegen.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/builder/input-code.ts"],
4
+ "sourcesContent": [
5
+ "import {\n\ttype GraphQLInputType,\n\ttype GraphQLList,\n\ttype GraphQLNamedInputType,\n\tgetNamedType,\n\tisInputObjectType,\n\tisListType,\n\tisNonNullType,\n} from 'graphql';\nimport { applyRuleCode } from '../rules';\nimport type { Constraint } from './constraints';\nimport { assertLeafTargets, assertObjectTargets, baseCode } from './leaf';\n\n/**\n * The source of the schema of one argument or input field: what\n * `InputSchemas` builds at startup, written for a code generator. The\n * source names zod as a free `z`.\n *\n * The runtime leaves what graphql already checked to graphql; generated\n * code checks every value, so each named type comes from `named`: an input\n * object's or an enum's schema by name, a custom scalar's from a mapping.\n * The built-in scalars are written here, with their constraints. A\n * constraint that cannot apply fails as it does at startup.\n */\nexport function inputCode(\n\ttype: GraphQLInputType,\n\tconstraints: readonly Constraint[],\n\twhere: string,\n\tnamed: (type: GraphQLNamedInputType) => string,\n\toptions: InputCodeOptions = {},\n): string {\n\tconst write: Write = { where, named, options };\n\treturn typedCode(write, type, constraints);\n}\n\n/** How `inputCode` writes what a generator may want written otherwise. */\nexport interface InputCodeOptions {\n\t/**\n\t * A list's schema, given its `type`, `code` (the array with every rule)\n\t * and `single` (the list's named type, non-null, without rules): to\n\t * take a single value for a list, as graphql does, `single` wrapped then\n\t * piped into `code`. Default: `code`.\n\t */\n\treadonly list?: (list: {\n\t\treadonly type: GraphQLList<GraphQLInputType>;\n\t\treadonly code: string;\n\t\treadonly single: string;\n\t}) => string;\n}\n\ninterface Write {\n\treadonly where: string;\n\treadonly named: (type: GraphQLNamedInputType) => string;\n\treadonly options: InputCodeOptions;\n}\n\nfunction typedCode(\n\twrite: Write,\n\ttype: GraphQLInputType,\n\tconstraints: readonly Constraint[],\n): string {\n\tif (isNonNullType(type)) return requiredCode(write, type.ofType, constraints);\n\treturn `${requiredCode(write, type, constraints)}.nullish()`;\n}\n\nfunction requiredCode(\n\twrite: Write,\n\ttype: GraphQLInputType,\n\tconstraints: readonly Constraint[],\n): string {\n\tif (isNonNullType(type)) return requiredCode(write, type.ofType, constraints);\n\tif (isListType(type)) {\n\t\t// minItems and maxItems are the list's; every other rule is its items'.\n\t\tconst own = constraints.filter(({ rule }) => rule.target === 'list');\n\t\tconst items = constraints.filter(({ rule }) => rule.target !== 'list');\n\t\tconst code = applyCode(\n\t\t\t`z.array(${typedCode(write, type.ofType, items)})`,\n\t\t\town,\n\t\t);\n\t\tif (!write.options.list) return code;\n\t\tconst single = requiredCode(write, getNamedType(type), []);\n\t\treturn write.options.list({ type, code, single });\n\t}\n\tif (isInputObjectType(type)) {\n\t\tassertObjectTargets(type.name, constraints, write.where);\n\t\treturn write.named(type);\n\t}\n\tassertLeafTargets(type, constraints, write.where);\n\treturn applyCode(baseCode(type) ?? write.named(type), constraints);\n}\n\nfunction applyCode(source: string, constraints: readonly Constraint[]): string {\n\treturn constraints.reduce(\n\t\t(code, { rule, value }) => applyRuleCode(rule, code, value),\n\t\tsource,\n\t);\n}\n"
6
+ ],
7
+ "mappings": ";;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;A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OAClB,CAAC,QAAQ,MAAM,YAAY,cAAc,MAAM,MAAM,KAAK,GAC1D,MACD;AAAA;",
8
+ "debugId": "CF7DECB6FA09613D64756E2164756E21",
9
+ "names": []
10
+ }
@@ -1,9 +1,9 @@
1
- export * from './byte';
2
- export * from './date';
3
- export * from './date-time';
4
- export * from './email';
5
- export * from './ipv4';
6
- export * from './ipv6';
7
- export * from './uri';
8
- export * from './uuid';
1
+ export * from './byte.js';
2
+ export * from './date.js';
3
+ export * from './date-time.js';
4
+ export * from './email.js';
5
+ export * from './ipv4.js';
6
+ export * from './ipv6.js';
7
+ export * from './uri.js';
8
+ export * from './uuid.js';
9
9
  //# sourceMappingURL=all.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const byteFormat: import("./format").Format<"byte">;
1
+ export declare const byteFormat: import("./format.js").Format<"byte">;
2
2
  //# sourceMappingURL=byte.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const dateTimeFormat: import("./format").Format<"date-time">;
1
+ export declare const dateTimeFormat: import("./format.js").Format<"date-time">;
2
2
  //# sourceMappingURL=date-time.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const dateFormat: import("./format").Format<"date">;
1
+ export declare const dateFormat: import("./format.js").Format<"date">;
2
2
  //# sourceMappingURL=date.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const emailFormat: import("./format").Format<"email">;
1
+ export declare const emailFormat: import("./format.js").Format<"email">;
2
2
  //# sourceMappingURL=email.d.ts.map
@@ -1,5 +1,5 @@
1
- import * as all from './all';
2
- import type { Format } from './format';
1
+ import * as all from './all.js';
2
+ import type { Format } from './format.js';
3
3
  /**
4
4
  * Every format, keyed by the value `@constraint(format: "...")` takes.
5
5
  * Derived from what `./all` exports, so a new format is one file and one
@@ -1,2 +1,2 @@
1
- export declare const ipv4Format: import("./format").Format<"ipv4">;
1
+ export declare const ipv4Format: import("./format.js").Format<"ipv4">;
2
2
  //# sourceMappingURL=ipv4.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const ipv6Format: import("./format").Format<"ipv6">;
1
+ export declare const ipv6Format: import("./format.js").Format<"ipv6">;
2
2
  //# sourceMappingURL=ipv6.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const uriFormat: import("./format").Format<"uri">;
1
+ export declare const uriFormat: import("./format.js").Format<"uri">;
2
2
  //# sourceMappingURL=uri.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const uuidFormat: import("./format").Format<"uuid">;
1
+ export declare const uuidFormat: import("./format.js").Format<"uuid">;
2
2
  //# sourceMappingURL=uuid.d.ts.map
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export { type BadUserInputExtensions, badUserInput, type ValidationIssue, } from './bad-user-input';
2
- export { constraintTypeDefs } from './constraint-directive';
3
- export type { ConstraintArgument } from './rules';
4
- export { type ArgsSchema, type SchemaOf, validated } from './validated';
5
- export { withValidation } from './with-validation';
1
+ export { type BadUserInputExtensions, badUserInput, type ValidationIssue, } from './bad-user-input.js';
2
+ export { constraintTypeDefs } from './constraint-directive.js';
3
+ export type { ConstraintArgument } from './rules/index.js';
4
+ export { type ArgsSchema, type SchemaOf, validated } from './validated.js';
5
+ export { withValidation } from './with-validation.js';
6
6
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import {
2
- rules,
3
- applyRule,
4
2
  constraintOf,
5
- constraintTypeDefs2,
6
- assertOwnConstraint
7
- } from "./chunks/index-n3hypgpf.js";
3
+ constraintTypeDefs2
4
+ } from "./chunks/index-rbxe5m3y.js";
5
+ import {
6
+ checkConstraints2
7
+ } from "./chunks/index-s1kkmg35.js";
8
8
 
9
9
  // src/bad-user-input.ts
10
10
  import { GraphQLError } from "graphql";
@@ -44,211 +44,8 @@ function validated(schema, resolve) {
44
44
  }
45
45
  // src/with-validation.ts
46
46
  import {
47
- defaultFieldResolver,
48
- isInterfaceType,
49
- isObjectType
50
- } from "graphql";
51
-
52
- // src/builder/args-schema.ts
53
- import { z as z2 } from "zod";
54
-
55
- // src/builder/constraints.ts
56
- import {
57
- getDirectiveValues
47
+ defaultFieldResolver
58
48
  } from "graphql";
59
- function constraintsOn(directive, node) {
60
- if (!directive || !node)
61
- return [];
62
- const values = getDirectiveValues(directive, { directives: node.directives ?? [] }) ?? {};
63
- return Object.entries(values).filter(([, value]) => value !== undefined && value !== null).map(([argument, value]) => ({
64
- rule: rules[argument],
65
- value
66
- })).sort((a, b) => Number(Boolean(b.rule.base)) - Number(Boolean(a.rule.base)));
67
- }
68
-
69
- // src/builder/args-schema.ts
70
- function argsSchemaOf(inputs, parent, field) {
71
- if (!field.args.some((arg) => inputs.needsCheck(arg)))
72
- return;
73
- return z2.object(Object.fromEntries(field.args.map((arg) => [
74
- arg.name,
75
- inputs.of(arg, `${parent}.${field.name}(${arg.name}:)`)
76
- ])));
77
- }
78
- function signature(inputs, arg) {
79
- return constraintsOn(inputs.directive, arg.astNode).map(({ rule, value }) => `${rule.argument}: ${JSON.stringify(value)}`).sort().join(", ");
80
- }
81
- var written = (signature) => signature ? `@constraint(${signature})` : "no @constraint";
82
- function assertInterfaceConstraintsKept(inputs, type) {
83
- for (const contract of type.getInterfaces()) {
84
- for (const [name, declared] of Object.entries(contract.getFields())) {
85
- const field = type.getFields()[name];
86
- for (const arg of declared.args) {
87
- const wanted = signature(inputs, arg);
88
- if (!wanted)
89
- continue;
90
- const own = field?.args.find(({ name: argName }) => argName === arg.name);
91
- const found = own ? signature(inputs, own) : wanted;
92
- if (found !== wanted) {
93
- const verb = found ? "differs on" : "is not repeated on";
94
- throw new Error(`@constraint on ${contract.name}.${name}(${arg.name}:) ${verb} ${type.name}.${name}(${arg.name}:), which resolves it (${written(wanted)} there, ${written(found)} here). Write the same @constraint on both.`);
95
- }
96
- }
97
- }
98
- }
99
- }
100
-
101
- // src/builder/input-schema.ts
102
- import {
103
- getNamedType,
104
- isInputObjectType,
105
- isListType,
106
- isNonNullType,
107
- valueFromAST
108
- } from "graphql";
109
- import { z as z4 } from "zod";
110
-
111
- // src/builder/leaf.ts
112
- import {
113
- GraphQLFloat,
114
- GraphQLID,
115
- GraphQLInt,
116
- GraphQLString
117
- } from "graphql";
118
- import { z as z3 } from "zod";
119
- var kinds = {
120
- [GraphQLString.name]: "string",
121
- [GraphQLID.name]: "string",
122
- [GraphQLInt.name]: "number",
123
- [GraphQLFloat.name]: "number"
124
- };
125
- function baseOf(type) {
126
- if (type.name === GraphQLInt.name)
127
- return z3.number().int();
128
- return kinds[type.name] === "number" ? z3.number() : z3.string();
129
- }
130
- function leafSchema(type, constraints, where) {
131
- if (constraints.length === 0)
132
- return z3.unknown();
133
- const kind = kinds[type.name];
134
- let schema = baseOf(type);
135
- for (const { rule, value } of constraints) {
136
- if (rule.target !== kind) {
137
- throw new Error(`@constraint(${rule.argument}) on ${where} needs ${describeTarget(rule.target)}, not ${type.name}.`);
138
- }
139
- schema = applyRule(rule, schema, value);
140
- }
141
- return schema;
142
- }
143
- function describeTarget(target) {
144
- if (target === "string")
145
- return "a String or an ID";
146
- if (target === "number")
147
- return "an Int or a Float";
148
- return "a list";
149
- }
150
-
151
- // src/builder/input-schema.ts
152
- class InputSchemas {
153
- directive;
154
- #objects = new Map;
155
- #defaults = [];
156
- #constrained = new Set;
157
- constructor(schema) {
158
- this.directive = schema.getDirective("constraint") ?? undefined;
159
- const objects = Object.values(schema.getTypeMap()).filter(isInputObjectType);
160
- for (let changed = true;changed; ) {
161
- changed = false;
162
- for (const type of objects) {
163
- if (this.#constrained.has(type.name))
164
- continue;
165
- const constrained = Object.values(type.getFields()).some((field) => constraintsOn(this.directive, field.astNode).length > 0 || this.isConstrained(field.type));
166
- if (constrained) {
167
- this.#constrained.add(type.name);
168
- changed = true;
169
- }
170
- }
171
- }
172
- }
173
- isConstrained(type) {
174
- const named = getNamedType(type);
175
- return isInputObjectType(named) && this.#constrained.has(named.name);
176
- }
177
- buildAll(schema) {
178
- for (const type of Object.values(schema.getTypeMap())) {
179
- if (isInputObjectType(type))
180
- this.#object(type);
181
- }
182
- }
183
- needsCheck(input) {
184
- return constraintsOn(this.directive, input.astNode).length > 0 || this.isConstrained(input.type);
185
- }
186
- of(input, where) {
187
- const schema = this.#typed(input.type, constraintsOn(this.directive, input.astNode), where);
188
- const literal = input.astNode?.defaultValue;
189
- if (literal)
190
- this.#defaults.push({ schema, literal, type: input.type, where });
191
- return schema;
192
- }
193
- checkDefaults() {
194
- for (const { schema, literal, type, where } of this.#defaults.splice(0)) {
195
- const value = valueFromAST(literal, type);
196
- if (value === undefined)
197
- continue;
198
- const result = schema.safeParse(value);
199
- if (!result.success) {
200
- throw new Error(`The default value of ${where} breaks its @constraint: ${result.error.issues[0]?.message}`);
201
- }
202
- }
203
- }
204
- #typed(type, constraints, where) {
205
- if (isNonNullType(type))
206
- return this.#required(type.ofType, constraints, where);
207
- return this.#required(type, constraints, where).nullish();
208
- }
209
- #required(type, constraints, where) {
210
- if (isNonNullType(type))
211
- return this.#required(type.ofType, constraints, where);
212
- if (isListType(type)) {
213
- const own = constraints.filter(({ rule }) => rule.target === "list");
214
- const items = constraints.filter(({ rule }) => rule.target !== "list");
215
- let list = z4.array(this.#typed(type.ofType, items, where));
216
- for (const { rule, value } of own)
217
- list = applyRule(rule, list, value);
218
- return list;
219
- }
220
- if (isInputObjectType(type)) {
221
- const list = constraints.find(({ rule }) => rule.target === "list");
222
- const first = list ?? constraints[0];
223
- if (first) {
224
- throw new Error(`@constraint(${first.rule.argument}) on ${where} needs ${describeTarget(first.rule.target)}, not ${type.name}.`);
225
- }
226
- return this.#object(type);
227
- }
228
- const misplaced = constraints.find(({ rule }) => rule.target === "list");
229
- if (misplaced) {
230
- throw new Error(`@constraint(${misplaced.rule.argument}) on ${where} needs a list, not ${type.name}.`);
231
- }
232
- return leafSchema(type, constraints, where);
233
- }
234
- #object(type) {
235
- if (!this.isConstrained(type))
236
- return z4.unknown();
237
- const built = this.#objects.get(type.name);
238
- if (built)
239
- return built;
240
- let schema;
241
- this.#objects.set(type.name, z4.lazy(() => schema));
242
- schema = z4.object(Object.fromEntries(Object.values(type.getFields()).map((field) => [
243
- field.name,
244
- this.of(field, `${type.name}.${field.name}`)
245
- ])));
246
- this.#objects.set(type.name, schema);
247
- return schema;
248
- }
249
- }
250
-
251
- // src/with-validation.ts
252
49
  var wrapped = Symbol.for("@nxgt/graphql-validation/wrapped");
253
50
  function checking(schema, where, resolve) {
254
51
  const check = async (source, args, context, info) => await resolve(source, await parseArgs(schema, args, where, constraintOf), context, info);
@@ -266,42 +63,14 @@ function wrap(field, args, where, subscription) {
266
63
  if (!resolve[wrapped])
267
64
  field.resolve = checking(args, where, resolve);
268
65
  }
269
- function assertNoDirectiveArgumentConstrained(inputs, schema) {
270
- for (const directive of schema.getDirectives()) {
271
- for (const arg of directive.args) {
272
- if (constraintsOn(inputs.directive, arg.astNode).length > 0) {
273
- throw new Error(`@constraint on @${directive.name}(${arg.name}:) checks nothing: a directive's argument reaches no resolver. Remove it.`);
274
- }
275
- }
276
- }
277
- }
278
66
  function withValidation(schema) {
279
- const inputs = new InputSchemas(schema);
280
- if (!inputs.directive) {
67
+ const directive = checkConstraints2(schema, (type, field, args) => {
68
+ const subscription = type === schema.getSubscriptionType();
69
+ wrap(field, args, `${type.name}.${field.name}`, subscription);
70
+ });
71
+ if (!directive) {
281
72
  throw new Error("withValidation: this schema declares no @constraint directive. Add constraintTypeDefs to its type definitions.");
282
73
  }
283
- assertOwnConstraint(inputs.directive);
284
- inputs.buildAll(schema);
285
- assertNoDirectiveArgumentConstrained(inputs, schema);
286
- for (const type of Object.values(schema.getTypeMap())) {
287
- if (type.name.startsWith("__"))
288
- continue;
289
- if (isInterfaceType(type)) {
290
- for (const field of Object.values(type.getFields()))
291
- argsSchemaOf(inputs, type.name, field);
292
- }
293
- if (!isObjectType(type))
294
- continue;
295
- assertInterfaceConstraintsKept(inputs, type);
296
- for (const field of Object.values(type.getFields())) {
297
- const args = argsSchemaOf(inputs, type.name, field);
298
- if (args) {
299
- const subscription = type === schema.getSubscriptionType();
300
- wrap(field, args, `${type.name}.${field.name}`, subscription);
301
- }
302
- }
303
- }
304
- inputs.checkDefaults();
305
74
  return schema;
306
75
  }
307
76
  export {
@@ -311,5 +80,5 @@ export {
311
80
  withValidation
312
81
  };
313
82
 
314
- //# debugId=E5B8091B29212FAF64756E2164756E21
83
+ //# debugId=EAADEEA4CBC8F7A764756E2164756E21
315
84
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1,16 +1,12 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/bad-user-input.ts", "../src/validated.ts", "../src/with-validation.ts", "../src/builder/args-schema.ts", "../src/builder/constraints.ts", "../src/builder/input-schema.ts", "../src/builder/leaf.ts"],
3
+ "sources": ["../src/bad-user-input.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
6
  "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
- "import {\n\tdefaultFieldResolver,\n\ttype GraphQLField,\n\ttype GraphQLFieldResolver,\n\ttype GraphQLSchema,\n\tisInterfaceType,\n\tisObjectType,\n} from 'graphql';\nimport type { z } from 'zod';\nimport { parseArgs } from './bad-user-input';\nimport {\n\targsSchemaOf,\n\tassertInterfaceConstraintsKept,\n} from './builder/args-schema';\nimport { constraintsOn } from './builder/constraints';\nimport { InputSchemas } from './builder/input-schema';\nimport { assertOwnConstraint } from './constraint-directive';\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\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 * Fails on a `@constraint` written on a directive's argument: graphql allows\n * it there (ARGUMENT_DEFINITION), but no resolver receives that argument, so\n * it would check nothing.\n */\nfunction assertNoDirectiveArgumentConstrained(\n\tinputs: InputSchemas,\n\tschema: GraphQLSchema,\n): void {\n\tfor (const directive of schema.getDirectives()) {\n\t\tfor (const arg of directive.args) {\n\t\t\tif (constraintsOn(inputs.directive, arg.astNode).length > 0) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`@constraint on @${directive.name}(${arg.name}:) checks nothing: a directive's argument reaches no resolver. Remove it.`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\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 */\nexport function withValidation<S extends GraphQLSchema>(schema: S): S {\n\tconst inputs = new InputSchemas(schema);\n\tif (!inputs.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\tassertOwnConstraint(inputs.directive);\n\tinputs.buildAll(schema);\n\tassertNoDirectiveArgumentConstrained(inputs, schema);\n\tfor (const type of Object.values(schema.getTypeMap())) {\n\t\tif (type.name.startsWith('__')) continue;\n\t\t// An interface's fields resolve on its objects: built for their errors only.\n\t\tif (isInterfaceType(type)) {\n\t\t\tfor (const field of Object.values(type.getFields()))\n\t\t\t\targsSchemaOf(inputs, type.name, field);\n\t\t}\n\t\tif (!isObjectType(type)) continue;\n\t\tassertInterfaceConstraintsKept(inputs, type);\n\t\tfor (const field of Object.values(type.getFields()) as GraphQLField<\n\t\t\tunknown,\n\t\t\tunknown\n\t\t>[]) {\n\t\t\tconst args = argsSchemaOf(inputs, type.name, field);\n\t\t\tif (args) {\n\t\t\t\tconst subscription = type === schema.getSubscriptionType();\n\t\t\t\twrap(field, args, `${type.name}.${field.name}`, subscription);\n\t\t\t}\n\t\t}\n\t}\n\tinputs.checkDefaults();\n\treturn schema;\n}\n",
8
- "import type { GraphQLArgument, GraphQLField, GraphQLObjectType } from 'graphql';\nimport { z } from 'zod';\nimport { constraintsOn } from './constraints';\nimport type { InputSchemas } from './input-schema';\n\n/**\n * The schema of a field's arguments, or `undefined` when none of them can\n * break a constraint, so a field with nothing to check is left untouched.\n * `parent` is the type's name, for errors: `Mutation.signUp(input:)`.\n */\nexport function argsSchemaOf(\n\tinputs: InputSchemas,\n\tparent: string,\n\tfield: GraphQLField<unknown, unknown>,\n): z.ZodType | undefined {\n\tif (!field.args.some((arg) => inputs.needsCheck(arg))) return undefined;\n\treturn z.object(\n\t\tObject.fromEntries(\n\t\t\tfield.args.map((arg) => [\n\t\t\t\targ.name,\n\t\t\t\tinputs.of(arg, `${parent}.${field.name}(${arg.name}:)`),\n\t\t\t]),\n\t\t),\n\t);\n}\n\n/** The constraints of an argument as written, sorted: `maxLength: 5, min: 1`. */\nfunction signature(inputs: InputSchemas, arg: GraphQLArgument): string {\n\treturn constraintsOn(inputs.directive, arg.astNode)\n\t\t.map(({ rule, value }) => `${rule.argument}: ${JSON.stringify(value)}`)\n\t\t.sort()\n\t\t.join(', ');\n}\n\n/** A signature as it reads in an error. */\nconst written = (signature: string) =>\n\tsignature ? `@constraint(${signature})` : 'no @constraint';\n\n/**\n * Fails when an object type's field does not repeat the `@constraint` its\n * interface writes on an argument. The object's field is the one that\n * resolves, and graphql lets it redeclare the argument bare: the interface's\n * constraint would then check nothing, in silence.\n */\nexport function assertInterfaceConstraintsKept(\n\tinputs: InputSchemas,\n\ttype: GraphQLObjectType,\n): void {\n\tfor (const contract of type.getInterfaces()) {\n\t\tfor (const [name, declared] of Object.entries(contract.getFields())) {\n\t\t\tconst field = type.getFields()[name];\n\t\t\tfor (const arg of declared.args) {\n\t\t\t\tconst wanted = signature(inputs, arg);\n\t\t\t\tif (!wanted) continue;\n\t\t\t\tconst own = field?.args.find(\n\t\t\t\t\t({ name: argName }) => argName === arg.name,\n\t\t\t\t);\n\t\t\t\tconst found = own ? signature(inputs, own) : wanted;\n\t\t\t\tif (found !== wanted) {\n\t\t\t\t\tconst verb = found ? 'differs on' : 'is not repeated on';\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`@constraint on ${contract.name}.${name}(${arg.name}:) ${verb} ${type.name}.${name}(${arg.name}:), which resolves it (${written(wanted)} there, ${written(found)} here). Write the same @constraint on both.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n",
9
- "import {\n\ttype DirectiveNode,\n\ttype GraphQLDirective,\n\tgetDirectiveValues,\n} from 'graphql';\nimport { type RegisteredRule, rules } from '../rules';\n\n/** One `@constraint` argument written on an argument or an input field. */\nexport interface Constraint {\n\treadonly rule: RegisteredRule;\n\treadonly value: string | number;\n}\n\n/**\n * The constraints written on a node, base rules (`format`) first so the\n * others narrow the schema they produce. Empty when the schema declares no\n * `@constraint` or the node carries none.\n */\nexport function constraintsOn(\n\tdirective: GraphQLDirective | undefined,\n\tnode:\n\t\t| { readonly directives?: ReadonlyArray<DirectiveNode> | undefined }\n\t\t| null\n\t\t| undefined,\n): Constraint[] {\n\tif (!directive || !node) return [];\n\tconst values =\n\t\tgetDirectiveValues(directive, { directives: node.directives ?? [] }) ?? {};\n\treturn Object.entries(values)\n\t\t.filter(([, value]) => value !== undefined && value !== null)\n\t\t.map(([argument, value]) => ({\n\t\t\trule: rules[argument as keyof typeof rules],\n\t\t\tvalue: value as string | number,\n\t\t}))\n\t\t.sort(\n\t\t\t(a, b) => Number(Boolean(b.rule.base)) - Number(Boolean(a.rule.base)),\n\t\t);\n}\n",
10
- "import {\n\ttype ConstValueNode,\n\ttype GraphQLArgument,\n\ttype GraphQLDirective,\n\ttype GraphQLInputField,\n\ttype GraphQLInputObjectType,\n\ttype GraphQLInputType,\n\ttype GraphQLSchema,\n\tgetNamedType,\n\tisInputObjectType,\n\tisListType,\n\tisNonNullType,\n\tvalueFromAST,\n} from 'graphql';\nimport { z } from 'zod';\nimport { applyRule } from '../rules';\nimport { type Constraint, constraintsOn } from './constraints';\nimport { describeTarget, leafSchema } from './leaf';\n\n/**\n * Builds the schemas of the arguments and input fields of one GraphQL\n * schema, once each: an input type is walked a single time however many\n * fields use it, and a recursive one (`input Filter { and: [Filter!] }`) is\n * reached through `z.lazy`.\n */\nexport class InputSchemas {\n\treadonly directive: GraphQLDirective | undefined;\n\treadonly #objects = new Map<string, z.ZodType>();\n\treadonly #defaults: {\n\t\tschema: z.ZodType;\n\t\tliteral: ConstValueNode;\n\t\ttype: GraphQLInputType;\n\t\twhere: string;\n\t}[] = [];\n\t/** The input types a value can break a constraint inside. */\n\treadonly #constrained = new Set<string>();\n\n\tconstructor(schema: GraphQLSchema) {\n\t\tthis.directive = schema.getDirective('constraint') ?? undefined;\n\t\tconst objects = Object.values(schema.getTypeMap()).filter(\n\t\t\tisInputObjectType,\n\t\t);\n\t\t// A fixpoint, not a memoised walk: in a cycle, a type visited while it\n\t\t// is still being decided would be cached as unconstrained for good.\n\t\tfor (let changed = true; changed; ) {\n\t\t\tchanged = false;\n\t\t\tfor (const type of objects) {\n\t\t\t\tif (this.#constrained.has(type.name)) continue;\n\t\t\t\tconst constrained = Object.values(type.getFields()).some(\n\t\t\t\t\t(field) =>\n\t\t\t\t\t\tconstraintsOn(this.directive, field.astNode).length > 0 ||\n\t\t\t\t\t\tthis.isConstrained(field.type),\n\t\t\t\t);\n\t\t\t\tif (constrained) {\n\t\t\t\t\tthis.#constrained.add(type.name);\n\t\t\t\t\tchanged = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/** Whether a value of this type can break a constraint anywhere inside. */\n\tisConstrained(type: GraphQLInputType): boolean {\n\t\tconst named = getNamedType(type);\n\t\treturn isInputObjectType(named) && this.#constrained.has(named.name);\n\t}\n\n\t/**\n\t * Builds every constrained input type, reached by an argument or not, so\n\t * a constraint that cannot apply fails at startup wherever it is written.\n\t */\n\tbuildAll(schema: GraphQLSchema): void {\n\t\tfor (const type of Object.values(schema.getTypeMap())) {\n\t\t\tif (isInputObjectType(type)) this.#object(type);\n\t\t}\n\t}\n\n\t/** Whether an argument or input field needs checking at all. */\n\tneedsCheck(input: GraphQLArgument | GraphQLInputField): boolean {\n\t\treturn (\n\t\t\tconstraintsOn(this.directive, input.astNode).length > 0 ||\n\t\t\tthis.isConstrained(input.type)\n\t\t);\n\t}\n\n\t/** The schema of one argument or input field, `where` naming it in errors. */\n\tof(input: GraphQLArgument | GraphQLInputField, where: string): z.ZodType {\n\t\tconst schema = this.#typed(\n\t\t\tinput.type,\n\t\t\tconstraintsOn(this.directive, input.astNode),\n\t\t\twhere,\n\t\t);\n\t\tconst literal = input.astNode?.defaultValue;\n\t\tif (literal)\n\t\t\tthis.#defaults.push({ schema, literal, type: input.type, where });\n\t\treturn schema;\n\t}\n\n\t/**\n\t * Fails when a default value breaks its own constraint: every request that\n\t * leaves the value out would be refused for something the client never\n\t * sent. Run once every schema is built, so a recursive type is complete.\n\t */\n\tcheckDefaults(): void {\n\t\tfor (const { schema, literal, type, where } of this.#defaults.splice(0)) {\n\t\t\t// Coerced as graphql coerces it: a single value for a list, an input\n\t\t\t// object's own field defaults. One graphql refuses is its to report.\n\t\t\tconst value = valueFromAST(literal, type);\n\t\t\tif (value === undefined) continue;\n\t\t\tconst result = schema.safeParse(value);\n\t\t\tif (!result.success) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`The default value of ${where} breaks its @constraint: ${result.error.issues[0]?.message}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t#typed(\n\t\ttype: GraphQLInputType,\n\t\tconstraints: readonly Constraint[],\n\t\twhere: string,\n\t): z.ZodType {\n\t\tif (isNonNullType(type))\n\t\t\treturn this.#required(type.ofType, constraints, where);\n\t\treturn this.#required(type, constraints, where).nullish();\n\t}\n\n\t#required(\n\t\ttype: GraphQLInputType,\n\t\tconstraints: readonly Constraint[],\n\t\twhere: string,\n\t): z.ZodType {\n\t\tif (isNonNullType(type))\n\t\t\treturn this.#required(type.ofType, constraints, where);\n\t\tif (isListType(type)) {\n\t\t\t// minItems and maxItems are the list's; every other rule is its items'.\n\t\t\tconst own = constraints.filter(({ rule }) => rule.target === 'list');\n\t\t\tconst items = constraints.filter(({ rule }) => rule.target !== 'list');\n\t\t\tlet list: z.ZodType = z.array(this.#typed(type.ofType, items, where));\n\t\t\tfor (const { rule, value } of own) list = applyRule(rule, list, value);\n\t\t\treturn list;\n\t\t}\n\t\tif (isInputObjectType(type)) {\n\t\t\tconst list = constraints.find(({ rule }) => rule.target === 'list');\n\t\t\tconst first = list ?? constraints[0];\n\t\t\tif (first) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`@constraint(${first.rule.argument}) on ${where} needs ${describeTarget(first.rule.target)}, not ${type.name}.`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn this.#object(type);\n\t\t}\n\t\tconst misplaced = constraints.find(({ rule }) => rule.target === 'list');\n\t\tif (misplaced) {\n\t\t\tthrow new Error(\n\t\t\t\t`@constraint(${misplaced.rule.argument}) on ${where} needs a list, not ${type.name}.`,\n\t\t\t);\n\t\t}\n\t\treturn leafSchema(type, constraints, where);\n\t}\n\n\t#object(type: GraphQLInputObjectType): z.ZodType {\n\t\tif (!this.isConstrained(type)) return z.unknown();\n\t\tconst built = this.#objects.get(type.name);\n\t\tif (built) return built;\n\t\t// Registered before the walk, so a field that comes back here ends.\n\t\tlet schema: z.ZodType | undefined;\n\t\tthis.#objects.set(\n\t\t\ttype.name,\n\t\t\tz.lazy(() => schema as z.ZodType),\n\t\t);\n\t\tschema = z.object(\n\t\t\tObject.fromEntries(\n\t\t\t\tObject.values(type.getFields()).map((field) => [\n\t\t\t\t\tfield.name,\n\t\t\t\t\tthis.of(field, `${type.name}.${field.name}`),\n\t\t\t\t]),\n\t\t\t),\n\t\t);\n\t\tthis.#objects.set(type.name, schema);\n\t\treturn schema;\n\t}\n}\n",
11
- "import {\n\tGraphQLFloat,\n\tGraphQLID,\n\tGraphQLInt,\n\ttype GraphQLLeafType,\n\tGraphQLString,\n} from 'graphql';\nimport { z } from 'zod';\nimport { applyRule } from '../rules';\nimport type { Target } from '../rules/rule';\nimport type { Constraint } from './constraints';\n\n/** The kind of value each built-in scalar holds once graphql parsed it. */\nconst kinds: Record<string, Exclude<Target, 'list'>> = {\n\t[GraphQLString.name]: 'string',\n\t[GraphQLID.name]: 'string',\n\t[GraphQLInt.name]: 'number',\n\t[GraphQLFloat.name]: 'number',\n};\n\nfunction baseOf(type: GraphQLLeafType): z.ZodType {\n\tif (type.name === GraphQLInt.name) return z.number().int();\n\treturn kinds[type.name] === 'number' ? z.number() : z.string();\n}\n\n/**\n * The schema of a scalar or enum value with its constraints applied. A leaf\n * without any is left as graphql parsed it. A constraint that cannot apply\n * — `minLength` on an `Int`, anything on a custom scalar or an enum — fails\n * here, at startup, naming `where`.\n */\nexport function leafSchema(\n\ttype: GraphQLLeafType,\n\tconstraints: readonly Constraint[],\n\twhere: string,\n): z.ZodType {\n\tif (constraints.length === 0) return z.unknown();\n\tconst kind = kinds[type.name];\n\tlet schema = baseOf(type);\n\tfor (const { rule, value } of constraints) {\n\t\tif (rule.target !== kind) {\n\t\t\tthrow new Error(\n\t\t\t\t`@constraint(${rule.argument}) on ${where} needs ${describeTarget(rule.target)}, not ${type.name}.`,\n\t\t\t);\n\t\t}\n\t\tschema = applyRule(rule, schema, value);\n\t}\n\treturn schema;\n}\n\n/** What a rule's target reads as in an error. */\nexport function describeTarget(target: Target): string {\n\tif (target === 'string') return 'a String or an ID';\n\tif (target === 'number') return 'an Int or a Float';\n\treturn 'a list';\n}\n"
7
+ "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 { 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\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 */\nexport function withValidation<S extends GraphQLSchema>(schema: S): S {\n\tconst directive = checkConstraints(schema, (type, field, args) => {\n\t\tconst subscription = type === schema.getSubscriptionType();\n\t\twrap(field, args, `${type.name}.${field.name}`, subscription);\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\treturn schema;\n}\n"
12
8
  ],
13
- "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,eAAsB,SAAS,CAC9B,QACA,MACA,OACA,cACmB;AAAA,EACnB,MAAM,SAAS,MAAM,OAAO,eAAe,IAAI;AAAA,EAC/C,IAAI,CAAC,OAAO;AAAA,IACX,MAAM,QAAQ,OAAO,SAAS,OAAO,OAAO,YAAY,CAAC;AAAA,EAC1D,OAAO,OAAO;AAAA;;AC5Ef;AAyBO,SAAS,SAKf,CACA,QACA,SAWC;AA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A;AAAA;AAYA,IAAM,UAAU,OAAO,IAAI,kCAAkC;AAI7D,SAAS,QAAQ,CAChB,QACA,OACA,SACW;AAAA,EACX,MAAM,QAAkB,OAAO,QAAQ,MAAM,SAAS,SACrD,MAAM,QACL,QACA,MAAM,UAAU,QAAQ,MAAM,OAAO,YAAY,GACjD,SACA,IACD;AAAA,EACD,MAAM,WAAW;AAAA,EACjB,OAAO;AAAA;AASR,SAAS,IAAI,CACZ,OACA,MACA,OACA,cACO;AAAA,EACP,MAAM,YAAa,MAAM,cACvB,eAAe,uBAAuB;AAAA,EACxC,IAAI,WAAW;AAAA,IACd,IAAI,CAAC,UAAU;AAAA,MAAU,MAAM,YAAY,SAAS,MAAM,OAAO,SAAS;AAAA,IAC1E;AAAA,EACD;AAAA,EACA,MAAM,UAAW,MAAM,WAAW;AAAA,EAClC,IAAI,CAAC,QAAQ;AAAA,IAAU,MAAM,UAAU,SAAS,MAAM,OAAO,OAAO;AAAA;AA0B9D,SAAS,cAAuC,CAAC,QAAc;AAAA,EACrE,MAAM,YAAY,kBAAiB,QAAQ,CAAC,MAAM,OAAO,SAAS;AAAA,IACjE,MAAM,eAAe,SAAS,OAAO,oBAAoB;AAAA,IACzD,KAAK,OAAO,MAAM,GAAG,KAAK,QAAQ,MAAM,QAAQ,YAAY;AAAA,GAC5D;AAAA,EACD,IAAI,CAAC,WAAW;AAAA,IACf,MAAM,IAAI,MACT,gHACD;AAAA,EACD;AAAA,EACA,OAAO;AAAA;",
10
+ "debugId": "EAADEEA4CBC8F7A764756E2164756E21",
15
11
  "names": []
16
12
  }
@@ -1,16 +1,16 @@
1
- export * from './contains';
2
- export * from './ends-with';
3
- export * from './exclusive-max';
4
- export * from './exclusive-min';
5
- export * from './format';
6
- export * from './max';
7
- export * from './max-items';
8
- export * from './max-length';
9
- export * from './min';
10
- export * from './min-items';
11
- export * from './min-length';
12
- export * from './multiple-of';
13
- export * from './not-contains';
14
- export * from './pattern';
15
- export * from './starts-with';
1
+ export * from './contains.js';
2
+ export * from './ends-with.js';
3
+ export * from './exclusive-max.js';
4
+ export * from './exclusive-min.js';
5
+ export * from './format.js';
6
+ export * from './max.js';
7
+ export * from './max-items.js';
8
+ export * from './max-length.js';
9
+ export * from './min.js';
10
+ export * from './min-items.js';
11
+ export * from './min-length.js';
12
+ export * from './multiple-of.js';
13
+ export * from './not-contains.js';
14
+ export * from './pattern.js';
15
+ export * from './starts-with.js';
16
16
  //# sourceMappingURL=all.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const containsRule: import("./rule").Rule<"contains", "String", "string">;
1
+ export declare const containsRule: import("./rule.js").Rule<"contains", "String", "string">;
2
2
  //# sourceMappingURL=contains.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const endsWithRule: import("./rule").Rule<"endsWith", "String", "string">;
1
+ export declare const endsWithRule: import("./rule.js").Rule<"endsWith", "String", "string">;
2
2
  //# sourceMappingURL=ends-with.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const exclusiveMaxRule: import("./rule").Rule<"exclusiveMax", "Float", "number">;
1
+ export declare const exclusiveMaxRule: import("./rule.js").Rule<"exclusiveMax", "Float", "number">;
2
2
  //# sourceMappingURL=exclusive-max.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const exclusiveMinRule: import("./rule").Rule<"exclusiveMin", "Float", "number">;
1
+ export declare const exclusiveMinRule: import("./rule.js").Rule<"exclusiveMin", "Float", "number">;
2
2
  //# sourceMappingURL=exclusive-min.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const formatRule: import("./rule").Rule<"format", "String", "string">;
1
+ export declare const formatRule: import("./rule.js").Rule<"format", "String", "string">;
2
2
  //# sourceMappingURL=format.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import type { z } from 'zod';
2
- import * as all from './all';
2
+ import * as all from './all.js';
3
3
  /**
4
4
  * One of the rules, as its own type: a builder narrows it on `target`, so a
5
5
  * number schema never reaches a string rule.
@@ -21,6 +21,8 @@ export declare const rules: { readonly [A in ConstraintArgument]: Extract<Regist
21
21
  * GraphQL types, which the compiler cannot relate.
22
22
  */
23
23
  export declare function applyRule(rule: RegisteredRule, schema: z.ZodType, value: string | number): z.ZodType;
24
+ /** `applyRule` for source: the rule's typed `toCode` on a schema's source. */
25
+ export declare function applyRuleCode(rule: RegisteredRule, source: string, value: string | number): string;
24
26
  /** The `@constraint` argument whose rule refused with this issue, if any. */
25
27
  export declare function constraintOf(issue: z.core.$ZodIssue): ConstraintArgument | undefined;
26
28
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAG7B;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC;AAE5D,uCAAuC;AACvC,MAAM,MAAM,kBAAkB,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;AAE5D;;;;GAIG;AACH,eAAO,MAAM,KAAK,EAEb,EACJ,QAAQ,EAAE,CAAC,IAAI,kBAAkB,GAAG,OAAO,CAAC,cAAc,EAAE;IAAE,QAAQ,EAAE,CAAC,CAAA;CAAE,CAAC,GAC5E,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,SAAS,CACxB,IAAI,EAAE,cAAc,EACpB,MAAM,EAAE,CAAC,CAAC,OAAO,EACjB,KAAK,EAAE,MAAM,GAAG,MAAM,GACpB,CAAC,CAAC,OAAO,CAMX;AAED,6EAA6E;AAC7E,wBAAgB,YAAY,CAC3B,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,GACrB,kBAAkB,GAAG,SAAS,CAGhC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAG7B;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC;AAE5D,uCAAuC;AACvC,MAAM,MAAM,kBAAkB,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;AAE5D;;;;GAIG;AACH,eAAO,MAAM,KAAK,EAEb,EACJ,QAAQ,EAAE,CAAC,IAAI,kBAAkB,GAAG,OAAO,CAAC,cAAc,EAAE;IAAE,QAAQ,EAAE,CAAC,CAAA;CAAE,CAAC,GAC5E,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,SAAS,CACxB,IAAI,EAAE,cAAc,EACpB,MAAM,EAAE,CAAC,CAAC,OAAO,EACjB,KAAK,EAAE,MAAM,GAAG,MAAM,GACpB,CAAC,CAAC,OAAO,CAMX;AAED,8EAA8E;AAC9E,wBAAgB,aAAa,CAC5B,IAAI,EAAE,cAAc,EACpB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GAAG,MAAM,GACpB,MAAM,CAMR;AAED,6EAA6E;AAC7E,wBAAgB,YAAY,CAC3B,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,GACrB,kBAAkB,GAAG,SAAS,CAGhC"}
@@ -1,2 +1,2 @@
1
- export declare const maxItemsRule: import("./rule").Rule<"maxItems", "Int", "list">;
1
+ export declare const maxItemsRule: import("./rule.js").Rule<"maxItems", "Int", "list">;
2
2
  //# sourceMappingURL=max-items.d.ts.map
@@ -1,2 +1,2 @@
1
- export declare const maxLengthRule: import("./rule").Rule<"maxLength", "Int", "string">;
1
+ export declare const maxLengthRule: import("./rule.js").Rule<"maxLength", "Int", "string">;
2
2
  //# sourceMappingURL=max-length.d.ts.map