@nxgt/graphql-validation 0.1.0 → 0.2.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 +26 -0
- package/dist/builder/check-constraints.d.ts +15 -0
- package/dist/builder/check-constraints.d.ts.map +1 -0
- package/dist/builder/input-code.d.ts +29 -0
- package/dist/builder/input-code.d.ts.map +1 -0
- package/dist/builder/input-schema.d.ts.map +1 -1
- package/dist/builder/leaf.d.ts +12 -2
- package/dist/builder/leaf.d.ts.map +1 -1
- package/dist/chunks/{index-n3hypgpf.js → index-rbxe5m3y.js} +7 -3
- package/dist/chunks/{index-n3hypgpf.js.map → index-rbxe5m3y.js.map} +3 -3
- package/dist/chunks/index-s1kkmg35.js +266 -0
- package/dist/chunks/index-s1kkmg35.js.map +14 -0
- package/dist/cli.js +1 -1
- package/dist/codegen.d.ts +4 -0
- package/dist/codegen.d.ts.map +1 -0
- package/dist/codegen.js +56 -0
- package/dist/codegen.js.map +10 -0
- package/dist/index.js +12 -243
- package/dist/index.js.map +4 -8
- package/dist/rules/index.d.ts +2 -0
- package/dist/rules/index.d.ts.map +1 -1
- package/dist/with-validation.d.ts.map +1 -1
- package/docs/guide/constraints.md +1 -1
- package/docs/roadmap.md +4 -3
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -178,6 +178,32 @@ the directive declared is this package's. Both at once throw
|
|
|
178
178
|
| `ConstraintArgument` | the type of `issues[].constraint`: `'minLength' \| 'format' \| ...` |
|
|
179
179
|
| `ArgsSchema`, `SchemaOf` | the types `validated` accepts |
|
|
180
180
|
|
|
181
|
+
`@nxgt/graphql-validation/codegen` is for a code generator, such as
|
|
182
|
+
[`@nxgt/graphql-codegen-zod`](https://www.npmjs.com/package/@nxgt/graphql-codegen-zod): it writes the schemas `withValidation` builds as source, and
|
|
183
|
+
refuses the schemas `withValidation` refuses. A server does not need it.
|
|
184
|
+
|
|
185
|
+
| Export | Is |
|
|
186
|
+
| --- | --- |
|
|
187
|
+
| `checkConstraints(schema, onArgs?)` | runs every startup check of `withValidation` without wrapping anything; returns the `@constraint` directive, or `undefined` when the schema declares none |
|
|
188
|
+
| `constraintsOn(directive, node)` | the `Constraint`s written on an argument or input field (`arg.astNode`), `format` first |
|
|
189
|
+
| `inputCode(type, constraints, where, named, options?)` | the schema of that argument or field as source, `z` a free identifier: built-in scalars and lists written in full, nullable types `.nullish()`; every other named type (enum, input object, custom scalar) is what `named(type)` returns. A constraint that cannot apply throws the startup message, naming `where`. `options` (`InputCodeOptions`): `list({ type, code, single })` returns each list's source, given `code` (the array with every rule) and `single` (the list's named type, non-null, without rules), e.g. to take a single value as graphql does: `single`, wrapped, piped into `code` |
|
|
190
|
+
| `Constraint` | `{ rule, value }`, one `@constraint` argument. Of `rule`, read `argument` (`'minItems'`, `'format'`, …), `target` (`'list'` for `minItems`/`maxItems`, else what the rule narrows) and `base` (`true` for `format`, which picks the schema the others narrow); the rest is internal |
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { checkConstraints, constraintsOn, inputCode } from '@nxgt/graphql-validation/codegen';
|
|
194
|
+
|
|
195
|
+
const directive = checkConstraints(schema); // throws what withValidation would
|
|
196
|
+
const arg = schema.getMutationType()!.getFields().signUp!.args[0]!;
|
|
197
|
+
const source = inputCode(
|
|
198
|
+
arg.type,
|
|
199
|
+
constraintsOn(directive, arg.astNode),
|
|
200
|
+
'Mutation.signUp(input:)', // named in errors, as at startup
|
|
201
|
+
(type) => `z${type.name}`, // enums, input objects, custom scalars
|
|
202
|
+
);
|
|
203
|
+
// e.g. 'zSignUpInput' — or, for `name: String @constraint(minLength: 2)`,
|
|
204
|
+
// 'z.string().min(2).nullish()'
|
|
205
|
+
```
|
|
206
|
+
|
|
181
207
|
Also shipped, outside `exports`:
|
|
182
208
|
|
|
183
209
|
| File | Is |
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type GraphQLDirective, type GraphQLField, type GraphQLObjectType, type GraphQLSchema } from 'graphql';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
/**
|
|
4
|
+
* Runs every check `withValidation` makes at startup, so a code generator
|
|
5
|
+
* refuses the same schemas: the declaration is `constraintTypeDefs`', every
|
|
6
|
+
* constraint applies where it is written (in every input type), none sits on
|
|
7
|
+
* a directive's argument, objects keep their interfaces' constraints, and no
|
|
8
|
+
* default value breaks its own. `onArgs` receives each object field's
|
|
9
|
+
* arguments schema, for a field with something to check.
|
|
10
|
+
*
|
|
11
|
+
* Returns the `@constraint` directive, or `undefined` when the schema
|
|
12
|
+
* declares none (and so carries no constraint).
|
|
13
|
+
*/
|
|
14
|
+
export declare function checkConstraints(schema: GraphQLSchema, onArgs?: (type: GraphQLObjectType, field: GraphQLField<unknown, unknown>, args: z.ZodType) => void): GraphQLDirective | undefined;
|
|
15
|
+
//# sourceMappingURL=check-constraints.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-constraints.d.ts","sourceRoot":"","sources":["../../src/builder/check-constraints.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,gBAAgB,EACrB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EAGlB,MAAM,SAAS,CAAC;AACjB,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAM7B;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC/B,MAAM,EAAE,aAAa,EACrB,MAAM,CAAC,EAAE,CACR,IAAI,EAAE,iBAAiB,EACvB,KAAK,EAAE,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,EACrC,IAAI,EAAE,CAAC,CAAC,OAAO,KACX,IAAI,GACP,gBAAgB,GAAG,SAAS,CAyB9B"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type GraphQLInputType, type GraphQLList, type GraphQLNamedInputType } from 'graphql';
|
|
2
|
+
import type { Constraint } from './constraints';
|
|
3
|
+
/**
|
|
4
|
+
* The source of the schema of one argument or input field: what
|
|
5
|
+
* `InputSchemas` builds at startup, written for a code generator. The
|
|
6
|
+
* source names zod as a free `z`.
|
|
7
|
+
*
|
|
8
|
+
* The runtime leaves what graphql already checked to graphql; generated
|
|
9
|
+
* code checks every value, so each named type comes from `named`: an input
|
|
10
|
+
* object's or an enum's schema by name, a custom scalar's from a mapping.
|
|
11
|
+
* The built-in scalars are written here, with their constraints. A
|
|
12
|
+
* constraint that cannot apply fails as it does at startup.
|
|
13
|
+
*/
|
|
14
|
+
export declare function inputCode(type: GraphQLInputType, constraints: readonly Constraint[], where: string, named: (type: GraphQLNamedInputType) => string, options?: InputCodeOptions): string;
|
|
15
|
+
/** How `inputCode` writes what a generator may want written otherwise. */
|
|
16
|
+
export interface InputCodeOptions {
|
|
17
|
+
/**
|
|
18
|
+
* A list's schema, given its `type`, `code` (the array with every rule)
|
|
19
|
+
* and `single` (the list's named type, non-null, without rules): to
|
|
20
|
+
* take a single value for a list, as graphql does, `single` wrapped then
|
|
21
|
+
* piped into `code`. Default: `code`.
|
|
22
|
+
*/
|
|
23
|
+
readonly list?: (list: {
|
|
24
|
+
readonly type: GraphQLList<GraphQLInputType>;
|
|
25
|
+
readonly code: string;
|
|
26
|
+
readonly single: string;
|
|
27
|
+
}) => string;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=input-code.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input-code.d.ts","sourceRoot":"","sources":["../../src/builder/input-code.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,qBAAqB,EAK1B,MAAM,SAAS,CAAC;AAEjB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CACxB,IAAI,EAAE,gBAAgB,EACtB,WAAW,EAAE,SAAS,UAAU,EAAE,EAClC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,CAAC,IAAI,EAAE,qBAAqB,KAAK,MAAM,EAC9C,OAAO,GAAE,gBAAqB,GAC5B,MAAM,CAGR;AAED,0EAA0E;AAC1E,MAAM,WAAW,gBAAgB;IAChC;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE;QACtB,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC,gBAAgB,CAAC,CAAC;QAC7C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;KACxB,KAAK,MAAM,CAAC;CACb"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input-schema.d.ts","sourceRoot":"","sources":["../../src/builder/input-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EAEtB,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAMlB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB;;;;;GAKG;AACH,qBAAa,YAAY;;IACxB,QAAQ,CAAC,SAAS,EAAE,gBAAgB,GAAG,SAAS,CAAC;gBAWrC,MAAM,EAAE,aAAa;IAwBjC,2EAA2E;IAC3E,aAAa,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO;IAK9C;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI;IAMrC,gEAAgE;IAChE,UAAU,CAAC,KAAK,EAAE,eAAe,GAAG,iBAAiB,GAAG,OAAO;IAO/D,8EAA8E;IAC9E,EAAE,CAAC,KAAK,EAAE,eAAe,GAAG,iBAAiB,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO;IAYxE;;;;OAIG;IACH,aAAa,IAAI,IAAI;
|
|
1
|
+
{"version":3,"file":"input-schema.d.ts","sourceRoot":"","sources":["../../src/builder/input-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EAEtB,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAMlB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB;;;;;GAKG;AACH,qBAAa,YAAY;;IACxB,QAAQ,CAAC,SAAS,EAAE,gBAAgB,GAAG,SAAS,CAAC;gBAWrC,MAAM,EAAE,aAAa;IAwBjC,2EAA2E;IAC3E,aAAa,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO;IAK9C;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI;IAMrC,gEAAgE;IAChE,UAAU,CAAC,KAAK,EAAE,eAAe,GAAG,iBAAiB,GAAG,OAAO;IAO/D,8EAA8E;IAC9E,EAAE,CAAC,KAAK,EAAE,eAAe,GAAG,iBAAiB,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO;IAYxE;;;;OAIG;IACH,aAAa,IAAI,IAAI;CAoErB"}
|
package/dist/builder/leaf.d.ts
CHANGED
|
@@ -2,13 +2,23 @@ import { type GraphQLLeafType } from 'graphql';
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import type { Target } from '../rules/rule';
|
|
4
4
|
import type { Constraint } from './constraints';
|
|
5
|
+
/**
|
|
6
|
+
* Fails unless every constraint applies to a value of `type`: `minLength` on
|
|
7
|
+
* an `Int`, anything on a custom scalar or an enum, fails, naming `where`.
|
|
8
|
+
* The runtime and the code generator refuse the same schemas with the same
|
|
9
|
+
* message.
|
|
10
|
+
*/
|
|
11
|
+
export declare function assertLeafTargets(type: GraphQLLeafType, constraints: readonly Constraint[], where: string): void;
|
|
12
|
+
/** Fails when an input object carries a constraint: none applies to it. */
|
|
13
|
+
export declare function assertObjectTargets(typeName: string, constraints: readonly Constraint[], where: string): void;
|
|
5
14
|
/**
|
|
6
15
|
* The schema of a scalar or enum value with its constraints applied. A leaf
|
|
7
16
|
* without any is left as graphql parsed it. A constraint that cannot apply
|
|
8
|
-
*
|
|
9
|
-
* here, at startup, naming `where`.
|
|
17
|
+
* fails here, at startup.
|
|
10
18
|
*/
|
|
11
19
|
export declare function leafSchema(type: GraphQLLeafType, constraints: readonly Constraint[], where: string): z.ZodType;
|
|
20
|
+
/** The source of a built-in scalar's schema, before its constraints. */
|
|
21
|
+
export declare function baseCode(type: GraphQLLeafType): string | undefined;
|
|
12
22
|
/** What a rule's target reads as in an error. */
|
|
13
23
|
export declare function describeTarget(target: Target): string;
|
|
14
24
|
//# sourceMappingURL=leaf.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"leaf.d.ts","sourceRoot":"","sources":["../../src/builder/leaf.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"leaf.d.ts","sourceRoot":"","sources":["../../src/builder/leaf.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,KAAK,eAAe,EAEpB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAehD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAChC,IAAI,EAAE,eAAe,EACrB,WAAW,EAAE,SAAS,UAAU,EAAE,EAClC,KAAK,EAAE,MAAM,GACX,IAAI,CAMN;AAED,2EAA2E;AAC3E,wBAAgB,mBAAmB,CAClC,QAAQ,EAAE,MAAM,EAChB,WAAW,EAAE,SAAS,UAAU,EAAE,EAClC,KAAK,EAAE,MAAM,GACX,IAAI,CAIN;AAYD;;;;GAIG;AACH,wBAAgB,UAAU,CACzB,IAAI,EAAE,eAAe,EACrB,WAAW,EAAE,SAAS,UAAU,EAAE,EAClC,KAAK,EAAE,MAAM,GACX,CAAC,CAAC,OAAO,CAQX;AAED,wEAAwE;AACxE,wBAAgB,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,MAAM,GAAG,SAAS,CAMlE;AAED,iDAAiD;AACjD,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAIrD"}
|
|
@@ -277,6 +277,10 @@ function applyRule(rule, schema, value) {
|
|
|
277
277
|
const toZod = rule.toZod;
|
|
278
278
|
return toZod(schema, value);
|
|
279
279
|
}
|
|
280
|
+
function applyRuleCode(rule, source, value) {
|
|
281
|
+
const toCode = rule.toCode;
|
|
282
|
+
return toCode(source, value);
|
|
283
|
+
}
|
|
280
284
|
function constraintOf(issue) {
|
|
281
285
|
const fields = issue;
|
|
282
286
|
return Object.values(rules).find((rule) => rule.owns(fields))?.argument;
|
|
@@ -309,7 +313,7 @@ function assertOwnConstraint(directive) {
|
|
|
309
313
|
}
|
|
310
314
|
}
|
|
311
315
|
|
|
312
|
-
export { rules, applyRule, constraintOf, constraintTypeDefs2, assertOwnConstraint };
|
|
316
|
+
export { rules, applyRule, applyRuleCode, constraintOf, constraintTypeDefs2, assertOwnConstraint };
|
|
313
317
|
|
|
314
|
-
//# debugId=
|
|
315
|
-
//# sourceMappingURL=index-
|
|
318
|
+
//# debugId=0FAF783CE1246B6764756E2164756E21
|
|
319
|
+
//# sourceMappingURL=index-rbxe5m3y.js.map
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"import { defineRule, literal } from './rule';\n\nconst message = (value: string) => `Must not contain ${literal(value)}`;\n\nexport const notContainsRule = defineRule({\n\targument: 'notContains',\n\ttype: 'String',\n\ttarget: 'string',\n\t// `refine` keeps the schema's own type, so later rules still chain.\n\ttoZod: (schema, value) =>\n\t\tschema.refine((input) => !input.includes(value), message(value)),\n\ttoCode: (schema, value) =>\n\t\t`${schema}.refine((input) => !input.includes(${literal(value)}), ${literal(message(value))})`,\n\towns: (issue) => issue.code === 'custom',\n});\n",
|
|
30
30
|
"import { defineRule, literal } from './rule';\n\n/** The pattern as a RegExp, or an error that names the argument. */\nfunction compile(value: string): RegExp {\n\ttry {\n\t\treturn new RegExp(value);\n\t} catch (error) {\n\t\tthrow new Error(\n\t\t\t`Invalid @constraint pattern ${literal(value)}: ${(error as Error).message}`,\n\t\t);\n\t}\n}\n\nexport const patternRule = defineRule({\n\targument: 'pattern',\n\ttype: 'String',\n\ttarget: 'string',\n\ttoZod: (schema, value) => schema.regex(compile(value)),\n\ttoCode: (schema, value) =>\n\t\t`${schema}.regex(new RegExp(${literal(compile(value).source)}))`,\n\towns: (issue) => issue.format === 'regex',\n});\n",
|
|
31
31
|
"import { defineRule, literal } from './rule';\n\nexport const startsWithRule = defineRule({\n\targument: 'startsWith',\n\ttype: 'String',\n\ttarget: 'string',\n\ttoZod: (schema, value) => schema.startsWith(value),\n\ttoCode: (schema, value) => `${schema}.startsWith(${literal(value)})`,\n\towns: (issue) => issue.format === 'starts_with',\n});\n",
|
|
32
|
-
"import type { z } from 'zod';\nimport * as all from './all';\nimport type { IssueFields } from './rule';\n\n/**\n * One of the rules, as its own type: a builder narrows it on `target`, so a\n * number schema never reaches a string rule.\n */\nexport type RegisteredRule = (typeof all)[keyof typeof all];\n\n/** An argument `@constraint` takes. */\nexport type ConstraintArgument = RegisteredRule['argument'];\n\n/**\n * Every rule, keyed by its `@constraint` argument. Derived from what\n * `./all` exports, so a new rule is one file and one line there, and the\n * directive's SDL follows.\n */\nexport const rules = Object.fromEntries(\n\tObject.values(all).map((rule) => [rule.argument, rule]),\n) as {\n\treadonly [A in ConstraintArgument]: Extract<RegisteredRule, { argument: A }>;\n};\n\n/**\n * Applies a rule to a schema its caller has already matched to the rule's\n * target: the one place the rule's typed `toZod` meets a schema built from\n * GraphQL types, which the compiler cannot relate.\n */\nexport function applyRule(\n\trule: RegisteredRule,\n\tschema: z.ZodType,\n\tvalue: string | number,\n): z.ZodType {\n\tconst toZod = rule.toZod as unknown as (\n\t\tschema: z.ZodType,\n\t\tvalue: string | number,\n\t) => z.ZodType;\n\treturn toZod(schema, value);\n}\n\n/** The `@constraint` argument whose rule refused with this issue, if any. */\nexport function constraintOf(\n\tissue: z.core.$ZodIssue,\n): ConstraintArgument | undefined {\n\tconst fields = issue as unknown as IssueFields;\n\treturn Object.values(rules).find((rule) => rule.owns(fields))?.argument;\n}\n"
|
|
32
|
+
"import type { z } from 'zod';\nimport * as all from './all';\nimport type { IssueFields } from './rule';\n\n/**\n * One of the rules, as its own type: a builder narrows it on `target`, so a\n * number schema never reaches a string rule.\n */\nexport type RegisteredRule = (typeof all)[keyof typeof all];\n\n/** An argument `@constraint` takes. */\nexport type ConstraintArgument = RegisteredRule['argument'];\n\n/**\n * Every rule, keyed by its `@constraint` argument. Derived from what\n * `./all` exports, so a new rule is one file and one line there, and the\n * directive's SDL follows.\n */\nexport const rules = Object.fromEntries(\n\tObject.values(all).map((rule) => [rule.argument, rule]),\n) as {\n\treadonly [A in ConstraintArgument]: Extract<RegisteredRule, { argument: A }>;\n};\n\n/**\n * Applies a rule to a schema its caller has already matched to the rule's\n * target: the one place the rule's typed `toZod` meets a schema built from\n * GraphQL types, which the compiler cannot relate.\n */\nexport function applyRule(\n\trule: RegisteredRule,\n\tschema: z.ZodType,\n\tvalue: string | number,\n): z.ZodType {\n\tconst toZod = rule.toZod as unknown as (\n\t\tschema: z.ZodType,\n\t\tvalue: string | number,\n\t) => z.ZodType;\n\treturn toZod(schema, value);\n}\n\n/** `applyRule` for source: the rule's typed `toCode` on a schema's source. */\nexport function applyRuleCode(\n\trule: RegisteredRule,\n\tsource: string,\n\tvalue: string | number,\n): string {\n\tconst toCode = rule.toCode as (\n\t\tschema: string,\n\t\tvalue: string | number,\n\t) => string;\n\treturn toCode(source, value);\n}\n\n/** The `@constraint` argument whose rule refused with this issue, if any. */\nexport function constraintOf(\n\tissue: z.core.$ZodIssue,\n): ConstraintArgument | undefined {\n\tconst fields = issue as unknown as IssueFields;\n\treturn Object.values(rules).find((rule) => rule.owns(fields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AA,QAAM,OAAO,CAAC,YAAY,IAAI,2BAA2B;AAAA,MAC9D,MAAM,OAAO,OAAO,IAAI,IAAI;AAAA,MAC5B,OAAO,SAAS,KAAK,OAClB,CAAC,IACD,CAAC,YAAY,IAAI,SAAS,aAAa,KAAK,MAAM;AAAA,KACrD;AAAA,EACF;AAAA,EACA,IAAI,SAAS,SAAS,GAAG;AAAA,IACxB,MAAM,IAAI,MACT,+DAA+D,SAAS,KAAK,IAAI,wCAClF;AAAA,EACD;AAAA;",
|
|
35
|
+
"debugId": "0FAF783CE1246B6764756E2164756E21",
|
|
36
36
|
"names": []
|
|
37
37
|
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import {
|
|
2
|
+
rules,
|
|
3
|
+
applyRule,
|
|
4
|
+
assertOwnConstraint
|
|
5
|
+
} from "./index-rbxe5m3y.js";
|
|
6
|
+
|
|
7
|
+
// src/builder/constraints.ts
|
|
8
|
+
import {
|
|
9
|
+
getDirectiveValues
|
|
10
|
+
} from "graphql";
|
|
11
|
+
function constraintsOn2(directive, node) {
|
|
12
|
+
if (!directive || !node)
|
|
13
|
+
return [];
|
|
14
|
+
const values = getDirectiveValues(directive, { directives: node.directives ?? [] }) ?? {};
|
|
15
|
+
return Object.entries(values).filter(([, value]) => value !== undefined && value !== null).map(([argument, value]) => ({
|
|
16
|
+
rule: rules[argument],
|
|
17
|
+
value
|
|
18
|
+
})).sort((a, b) => Number(Boolean(b.rule.base)) - Number(Boolean(a.rule.base)));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/builder/check-constraints.ts
|
|
22
|
+
import {
|
|
23
|
+
isInterfaceType,
|
|
24
|
+
isObjectType
|
|
25
|
+
} from "graphql";
|
|
26
|
+
|
|
27
|
+
// src/builder/args-schema.ts
|
|
28
|
+
import { z } from "zod";
|
|
29
|
+
function argsSchemaOf(inputs, parent, field) {
|
|
30
|
+
if (!field.args.some((arg) => inputs.needsCheck(arg)))
|
|
31
|
+
return;
|
|
32
|
+
return z.object(Object.fromEntries(field.args.map((arg) => [
|
|
33
|
+
arg.name,
|
|
34
|
+
inputs.of(arg, `${parent}.${field.name}(${arg.name}:)`)
|
|
35
|
+
])));
|
|
36
|
+
}
|
|
37
|
+
function signature(inputs, arg) {
|
|
38
|
+
return constraintsOn2(inputs.directive, arg.astNode).map(({ rule, value }) => `${rule.argument}: ${JSON.stringify(value)}`).sort().join(", ");
|
|
39
|
+
}
|
|
40
|
+
var written = (signature) => signature ? `@constraint(${signature})` : "no @constraint";
|
|
41
|
+
function assertInterfaceConstraintsKept(inputs, type) {
|
|
42
|
+
for (const contract of type.getInterfaces()) {
|
|
43
|
+
for (const [name, declared] of Object.entries(contract.getFields())) {
|
|
44
|
+
const field = type.getFields()[name];
|
|
45
|
+
for (const arg of declared.args) {
|
|
46
|
+
const wanted = signature(inputs, arg);
|
|
47
|
+
if (!wanted)
|
|
48
|
+
continue;
|
|
49
|
+
const own = field?.args.find(({ name: argName }) => argName === arg.name);
|
|
50
|
+
const found = own ? signature(inputs, own) : wanted;
|
|
51
|
+
if (found !== wanted) {
|
|
52
|
+
const verb = found ? "differs on" : "is not repeated on";
|
|
53
|
+
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.`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/builder/input-schema.ts
|
|
61
|
+
import {
|
|
62
|
+
getNamedType,
|
|
63
|
+
isInputObjectType,
|
|
64
|
+
isListType,
|
|
65
|
+
isNonNullType,
|
|
66
|
+
valueFromAST
|
|
67
|
+
} from "graphql";
|
|
68
|
+
import { z as z3 } from "zod";
|
|
69
|
+
|
|
70
|
+
// src/builder/leaf.ts
|
|
71
|
+
import {
|
|
72
|
+
GraphQLBoolean,
|
|
73
|
+
GraphQLFloat,
|
|
74
|
+
GraphQLID,
|
|
75
|
+
GraphQLInt,
|
|
76
|
+
GraphQLString
|
|
77
|
+
} from "graphql";
|
|
78
|
+
import { z as z2 } from "zod";
|
|
79
|
+
var kinds = {
|
|
80
|
+
[GraphQLString.name]: "string",
|
|
81
|
+
[GraphQLID.name]: "string",
|
|
82
|
+
[GraphQLInt.name]: "number",
|
|
83
|
+
[GraphQLFloat.name]: "number"
|
|
84
|
+
};
|
|
85
|
+
function baseOf(type) {
|
|
86
|
+
if (type.name === GraphQLInt.name)
|
|
87
|
+
return z2.number().int();
|
|
88
|
+
return kinds[type.name] === "number" ? z2.number() : z2.string();
|
|
89
|
+
}
|
|
90
|
+
function assertLeafTargets(type, constraints, where) {
|
|
91
|
+
const misplaced = constraints.find(({ rule }) => rule.target === "list");
|
|
92
|
+
if (misplaced)
|
|
93
|
+
throw misplacedOn(misplaced, where, type.name);
|
|
94
|
+
const kind = kinds[type.name];
|
|
95
|
+
const wrong = constraints.find(({ rule }) => rule.target !== kind);
|
|
96
|
+
if (wrong)
|
|
97
|
+
throw misplacedOn(wrong, where, type.name);
|
|
98
|
+
}
|
|
99
|
+
function assertObjectTargets(typeName, constraints, where) {
|
|
100
|
+
const first = constraints.find(({ rule }) => rule.target === "list") ?? constraints[0];
|
|
101
|
+
if (first)
|
|
102
|
+
throw misplacedOn(first, where, typeName);
|
|
103
|
+
}
|
|
104
|
+
function misplacedOn({ rule }, where, typeName) {
|
|
105
|
+
return new Error(`@constraint(${rule.argument}) on ${where} needs ${describeTarget(rule.target)}, not ${typeName}.`);
|
|
106
|
+
}
|
|
107
|
+
function leafSchema(type, constraints, where) {
|
|
108
|
+
if (constraints.length === 0)
|
|
109
|
+
return z2.unknown();
|
|
110
|
+
assertLeafTargets(type, constraints, where);
|
|
111
|
+
let schema = baseOf(type);
|
|
112
|
+
for (const { rule, value } of constraints) {
|
|
113
|
+
schema = applyRule(rule, schema, value);
|
|
114
|
+
}
|
|
115
|
+
return schema;
|
|
116
|
+
}
|
|
117
|
+
function baseCode(type) {
|
|
118
|
+
if (type.name === GraphQLInt.name)
|
|
119
|
+
return "z.int32()";
|
|
120
|
+
if (type.name === GraphQLFloat.name)
|
|
121
|
+
return "z.number()";
|
|
122
|
+
if (type.name === GraphQLBoolean.name)
|
|
123
|
+
return "z.boolean()";
|
|
124
|
+
return kinds[type.name] === "string" ? "z.string()" : undefined;
|
|
125
|
+
}
|
|
126
|
+
function describeTarget(target) {
|
|
127
|
+
if (target === "string")
|
|
128
|
+
return "a String or an ID";
|
|
129
|
+
if (target === "number")
|
|
130
|
+
return "an Int or a Float";
|
|
131
|
+
return "a list";
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/builder/input-schema.ts
|
|
135
|
+
class InputSchemas {
|
|
136
|
+
directive;
|
|
137
|
+
#objects = new Map;
|
|
138
|
+
#defaults = [];
|
|
139
|
+
#constrained = new Set;
|
|
140
|
+
constructor(schema) {
|
|
141
|
+
this.directive = schema.getDirective("constraint") ?? undefined;
|
|
142
|
+
const objects = Object.values(schema.getTypeMap()).filter(isInputObjectType);
|
|
143
|
+
for (let changed = true;changed; ) {
|
|
144
|
+
changed = false;
|
|
145
|
+
for (const type of objects) {
|
|
146
|
+
if (this.#constrained.has(type.name))
|
|
147
|
+
continue;
|
|
148
|
+
const constrained = Object.values(type.getFields()).some((field) => constraintsOn2(this.directive, field.astNode).length > 0 || this.isConstrained(field.type));
|
|
149
|
+
if (constrained) {
|
|
150
|
+
this.#constrained.add(type.name);
|
|
151
|
+
changed = true;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
isConstrained(type) {
|
|
157
|
+
const named = getNamedType(type);
|
|
158
|
+
return isInputObjectType(named) && this.#constrained.has(named.name);
|
|
159
|
+
}
|
|
160
|
+
buildAll(schema) {
|
|
161
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
162
|
+
if (isInputObjectType(type))
|
|
163
|
+
this.#object(type);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
needsCheck(input) {
|
|
167
|
+
return constraintsOn2(this.directive, input.astNode).length > 0 || this.isConstrained(input.type);
|
|
168
|
+
}
|
|
169
|
+
of(input, where) {
|
|
170
|
+
const schema = this.#typed(input.type, constraintsOn2(this.directive, input.astNode), where);
|
|
171
|
+
const literal = input.astNode?.defaultValue;
|
|
172
|
+
if (literal)
|
|
173
|
+
this.#defaults.push({ schema, literal, type: input.type, where });
|
|
174
|
+
return schema;
|
|
175
|
+
}
|
|
176
|
+
checkDefaults() {
|
|
177
|
+
for (const { schema, literal, type, where } of this.#defaults.splice(0)) {
|
|
178
|
+
const value = valueFromAST(literal, type);
|
|
179
|
+
if (value === undefined)
|
|
180
|
+
continue;
|
|
181
|
+
const result = schema.safeParse(value);
|
|
182
|
+
if (!result.success) {
|
|
183
|
+
throw new Error(`The default value of ${where} breaks its @constraint: ${result.error.issues[0]?.message}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
#typed(type, constraints, where) {
|
|
188
|
+
if (isNonNullType(type))
|
|
189
|
+
return this.#required(type.ofType, constraints, where);
|
|
190
|
+
return this.#required(type, constraints, where).nullish();
|
|
191
|
+
}
|
|
192
|
+
#required(type, constraints, where) {
|
|
193
|
+
if (isNonNullType(type))
|
|
194
|
+
return this.#required(type.ofType, constraints, where);
|
|
195
|
+
if (isListType(type)) {
|
|
196
|
+
const own = constraints.filter(({ rule }) => rule.target === "list");
|
|
197
|
+
const items = constraints.filter(({ rule }) => rule.target !== "list");
|
|
198
|
+
let list = z3.array(this.#typed(type.ofType, items, where));
|
|
199
|
+
for (const { rule, value } of own)
|
|
200
|
+
list = applyRule(rule, list, value);
|
|
201
|
+
return list;
|
|
202
|
+
}
|
|
203
|
+
if (isInputObjectType(type)) {
|
|
204
|
+
assertObjectTargets(type.name, constraints, where);
|
|
205
|
+
return this.#object(type);
|
|
206
|
+
}
|
|
207
|
+
return leafSchema(type, constraints, where);
|
|
208
|
+
}
|
|
209
|
+
#object(type) {
|
|
210
|
+
if (!this.isConstrained(type))
|
|
211
|
+
return z3.unknown();
|
|
212
|
+
const built = this.#objects.get(type.name);
|
|
213
|
+
if (built)
|
|
214
|
+
return built;
|
|
215
|
+
let schema;
|
|
216
|
+
this.#objects.set(type.name, z3.lazy(() => schema));
|
|
217
|
+
schema = z3.object(Object.fromEntries(Object.values(type.getFields()).map((field) => [
|
|
218
|
+
field.name,
|
|
219
|
+
this.of(field, `${type.name}.${field.name}`)
|
|
220
|
+
])));
|
|
221
|
+
this.#objects.set(type.name, schema);
|
|
222
|
+
return schema;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/builder/check-constraints.ts
|
|
227
|
+
function checkConstraints2(schema, onArgs) {
|
|
228
|
+
const inputs = new InputSchemas(schema);
|
|
229
|
+
if (!inputs.directive)
|
|
230
|
+
return;
|
|
231
|
+
assertOwnConstraint(inputs.directive);
|
|
232
|
+
inputs.buildAll(schema);
|
|
233
|
+
assertNoDirectiveArgumentConstrained(inputs, schema);
|
|
234
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
235
|
+
if (type.name.startsWith("__"))
|
|
236
|
+
continue;
|
|
237
|
+
if (isInterfaceType(type)) {
|
|
238
|
+
for (const field of Object.values(type.getFields()))
|
|
239
|
+
argsSchemaOf(inputs, type.name, field);
|
|
240
|
+
}
|
|
241
|
+
if (!isObjectType(type))
|
|
242
|
+
continue;
|
|
243
|
+
assertInterfaceConstraintsKept(inputs, type);
|
|
244
|
+
for (const field of Object.values(type.getFields())) {
|
|
245
|
+
const args = argsSchemaOf(inputs, type.name, field);
|
|
246
|
+
if (args)
|
|
247
|
+
onArgs?.(type, field, args);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
inputs.checkDefaults();
|
|
251
|
+
return inputs.directive;
|
|
252
|
+
}
|
|
253
|
+
function assertNoDirectiveArgumentConstrained(inputs, schema) {
|
|
254
|
+
for (const directive of schema.getDirectives()) {
|
|
255
|
+
for (const arg of directive.args) {
|
|
256
|
+
if (constraintsOn2(inputs.directive, arg.astNode).length > 0) {
|
|
257
|
+
throw new Error(`@constraint on @${directive.name}(${arg.name}:) checks nothing: a directive's argument reaches no resolver. Remove it.`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export { constraintsOn2, assertLeafTargets, assertObjectTargets, baseCode, checkConstraints2 };
|
|
264
|
+
|
|
265
|
+
//# debugId=7C317F3D691975BE64756E2164756E21
|
|
266
|
+
//# sourceMappingURL=index-s1kkmg35.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/builder/constraints.ts", "../src/builder/check-constraints.ts", "../src/builder/args-schema.ts", "../src/builder/input-schema.ts", "../src/builder/leaf.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"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",
|
|
6
|
+
"import {\n\ttype GraphQLDirective,\n\ttype GraphQLField,\n\ttype GraphQLObjectType,\n\ttype GraphQLSchema,\n\tisInterfaceType,\n\tisObjectType,\n} from 'graphql';\nimport type { z } from 'zod';\nimport { assertOwnConstraint } from '../constraint-directive';\nimport { argsSchemaOf, assertInterfaceConstraintsKept } from './args-schema';\nimport { constraintsOn } from './constraints';\nimport { InputSchemas } from './input-schema';\n\n/**\n * Runs every check `withValidation` makes at startup, so a code generator\n * refuses the same schemas: the declaration is `constraintTypeDefs`', every\n * constraint applies where it is written (in every input type), none sits on\n * a directive's argument, objects keep their interfaces' constraints, and no\n * default value breaks its own. `onArgs` receives each object field's\n * arguments schema, for a field with something to check.\n *\n * Returns the `@constraint` directive, or `undefined` when the schema\n * declares none (and so carries no constraint).\n */\nexport function checkConstraints(\n\tschema: GraphQLSchema,\n\tonArgs?: (\n\t\ttype: GraphQLObjectType,\n\t\tfield: GraphQLField<unknown, unknown>,\n\t\targs: z.ZodType,\n\t) => void,\n): GraphQLDirective | undefined {\n\tconst inputs = new InputSchemas(schema);\n\tif (!inputs.directive) return undefined;\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) onArgs?.(type, field, args);\n\t\t}\n\t}\n\tinputs.checkDefaults();\n\treturn inputs.directive;\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",
|
|
7
|
+
"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",
|
|
8
|
+
"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 { assertObjectTargets, 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\tassertObjectTargets(type.name, constraints, where);\n\t\t\treturn this.#object(type);\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",
|
|
9
|
+
"import {\n\tGraphQLBoolean,\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 * Fails unless every constraint applies to a value of `type`: `minLength` on\n * an `Int`, anything on a custom scalar or an enum, fails, naming `where`.\n * The runtime and the code generator refuse the same schemas with the same\n * message.\n */\nexport function assertLeafTargets(\n\ttype: GraphQLLeafType,\n\tconstraints: readonly Constraint[],\n\twhere: string,\n): void {\n\tconst misplaced = constraints.find(({ rule }) => rule.target === 'list');\n\tif (misplaced) throw misplacedOn(misplaced, where, type.name);\n\tconst kind = kinds[type.name];\n\tconst wrong = constraints.find(({ rule }) => rule.target !== kind);\n\tif (wrong) throw misplacedOn(wrong, where, type.name);\n}\n\n/** Fails when an input object carries a constraint: none applies to it. */\nexport function assertObjectTargets(\n\ttypeName: string,\n\tconstraints: readonly Constraint[],\n\twhere: string,\n): void {\n\tconst first =\n\t\tconstraints.find(({ rule }) => rule.target === 'list') ?? constraints[0];\n\tif (first) throw misplacedOn(first, where, typeName);\n}\n\nfunction misplacedOn(\n\t{ rule }: Constraint,\n\twhere: string,\n\ttypeName: string,\n): Error {\n\treturn new Error(\n\t\t`@constraint(${rule.argument}) on ${where} needs ${describeTarget(rule.target)}, not ${typeName}.`,\n\t);\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 * fails here, at startup.\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\tassertLeafTargets(type, constraints, where);\n\tlet schema = baseOf(type);\n\tfor (const { rule, value } of constraints) {\n\t\tschema = applyRule(rule, schema, value);\n\t}\n\treturn schema;\n}\n\n/** The source of a built-in scalar's schema, before its constraints. */\nexport function baseCode(type: GraphQLLeafType): string | undefined {\n\t// graphql's Int is 32-bit: the client refuses what it refuses.\n\tif (type.name === GraphQLInt.name) return 'z.int32()';\n\tif (type.name === GraphQLFloat.name) return 'z.number()';\n\tif (type.name === GraphQLBoolean.name) return 'z.boolean()';\n\treturn kinds[type.name] === 'string' ? 'z.string()' : undefined;\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';\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,IAAI;AAAA,IAC3C,WAAW,SAAS,OAAO,OAAO,KAAK,UAAU,CAAC,GAG7C;AAAA,MACJ,MAAM,OAAO,aAAa,QAAQ,KAAK,MAAM,KAAK;AAAA,MAClD,IAAI;AAAA,QAAM,SAAS,MAAM,OAAO,IAAI;AAAA,IACrC;AAAA,EACD;AAAA,EACA,OAAO,cAAc;AAAA,EACrB,OAAO,OAAO;AAAA;AAQf,SAAS,oCAAoC,CAC5C,QACA,QACO;AAAA,EACP,WAAW,aAAa,OAAO,cAAc,GAAG;AAAA,IAC/C,WAAW,OAAO,UAAU,MAAM;AAAA,MACjC,IAAI,eAAc,OAAO,WAAW,IAAI,OAAO,EAAE,SAAS,GAAG;AAAA,QAC5D,MAAM,IAAI,MACT,mBAAmB,UAAU,QAAQ,IAAI,+EAC1C;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAAA;",
|
|
12
|
+
"debugId": "7C317F3D691975BE64756E2164756E21",
|
|
13
|
+
"names": []
|
|
14
|
+
}
|
package/dist/cli.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../src/codegen.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,KAAK,UAAU,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,EAAE,KAAK,gBAAgB,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/codegen.js
ADDED
|
@@ -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;AAAA;AAwBO,SAAS,SAAS,CACxB,MACA,aACA,OACA,OACA,UAA4B,CAAC,GACpB;AAAA,EACT,MAAM,QAAe,EAAE,OAAO,OAAO,QAAQ;AAAA,EAC7C,OAAO,UAAU,OAAO,MAAM,WAAW;AAAA;AAwB1C,SAAS,SAAS,CACjB,OACA,MACA,aACS;AAAA,EACT,IAAI,cAAc,IAAI;AAAA,IAAG,OAAO,aAAa,OAAO,KAAK,QAAQ,WAAW;AAAA,EAC5E,OAAO,GAAG,aAAa,OAAO,MAAM,WAAW;AAAA;AAGhD,SAAS,YAAY,CACpB,OACA,MACA,aACS;AAAA,EACT,IAAI,cAAc,IAAI;AAAA,IAAG,OAAO,aAAa,OAAO,KAAK,QAAQ,WAAW;AAAA,EAC5E,IAAI,WAAW,IAAI,GAAG;AAAA,IAErB,MAAM,MAAM,YAAY,OAAO,GAAG,WAAW,KAAK,WAAW,MAAM;AAAA,IACnE,MAAM,QAAQ,YAAY,OAAO,GAAG,WAAW,KAAK,WAAW,MAAM;AAAA,IACrE,MAAM,OAAO,UACZ,WAAW,UAAU,OAAO,KAAK,QAAQ,KAAK,MAC9C,GACD;AAAA,IACA,IAAI,CAAC,MAAM,QAAQ;AAAA,MAAM,OAAO;AAAA,IAChC,MAAM,SAAS,aAAa,OAAO,aAAa,IAAI,GAAG,CAAC,CAAC;AAAA,IACzD,OAAO,MAAM,QAAQ,KAAK,EAAE,MAAM,MAAM,OAAO,CAAC;AAAA,EACjD;AAAA,EACA,IAAI,kBAAkB,IAAI,GAAG;AAAA,IAC5B,oBAAoB,KAAK,MAAM,aAAa,MAAM,KAAK;AAAA,IACvD,OAAO,MAAM,MAAM,IAAI;AAAA,EACxB;AAAA,EACA,kBAAkB,MAAM,aAAa,MAAM,KAAK;AAAA,EAChD,OAAO,UAAU,SAAS,IAAI,KAAK,MAAM,MAAM,IAAI,GAAG,WAAW;AAAA;AAGlE,SAAS,SAAS,CAAC,QAAgB,aAA4C;AAAA,EAC9E,OAAO,YAAY,OAClB,CAAC,QAAQ,MAAM,YAAY,cAAc,MAAM,MAAM,KAAK,GAC1D,MACD;AAAA;",
|
|
8
|
+
"debugId": "CF7DECB6FA09613D64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
rules,
|
|
3
|
-
applyRule,
|
|
4
2
|
constraintOf,
|
|
5
|
-
constraintTypeDefs2
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
280
|
-
|
|
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=
|
|
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"
|
|
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
|
|
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,U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K,WAAW,QAAQ,KAAK;AAAA,IAC9C,MAAM,SAAU,MAAM,UAAU,SAAS,MAAM,KAAK;AAAA,IAGpD,OAAO,MAAM,QAAQ,QAAQ,QAAQ,SAAS,IAAI;AAAA;AAAA;;ACtDpD;AAAA;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
|
}
|
package/dist/rules/index.d.ts
CHANGED
|
@@ -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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-validation.d.ts","sourceRoot":"","sources":["../src/with-validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,KAAK,aAAa,
|
|
1
|
+
{"version":3,"file":"with-validation.d.ts","sourceRoot":"","sources":["../src/with-validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,KAAK,aAAa,EAClB,MAAM,SAAS,CAAC;AAiDjB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,aAAa,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAWpE"}
|
|
@@ -237,7 +237,7 @@ and list `generated/graphql/constraint.graphqls` in `graphql.config.yml` in plac
|
|
|
237
237
|
`--out <file>` writes `<file>`, creating the folder, and without it the SDL goes
|
|
238
238
|
to stdout (`--help` or `-h`, anywhere on the line, lists the flags). It exits 0 when done, 1 when the file
|
|
239
239
|
cannot be written (`typedefs failed: <message>` on stderr), and 2 on a usage
|
|
240
|
-
error (`Unknown command "<x>".`, `Unexpected arguments: …`, or
|
|
240
|
+
error (`Unknown command "<x>".`, `Unexpected arguments: …`, or `No command.`). Regenerate the file after upgrading the
|
|
241
241
|
package.
|
|
242
242
|
|
|
243
243
|
The schema must declare `@constraint` once. Pick one source for the server:
|
package/docs/roadmap.md
CHANGED
|
@@ -19,12 +19,13 @@ What `@nxgt/graphql-validation` is heading for, phrased as what you get.
|
|
|
19
19
|
|
|
20
20
|
## Next
|
|
21
21
|
|
|
22
|
-
- **A Zod codegen plugin** that writes the same schemas as source, from the
|
|
23
|
-
same rules, so the types your resolvers receive come from your SDL.
|
|
24
22
|
- **Your own formats**, named in `@constraint(format: "...")`.
|
|
25
23
|
- **Schemas that read the context**, `(args, context) => schema`, for checks
|
|
26
24
|
that need the request (uniqueness in a database, permissions).
|
|
27
25
|
|
|
28
26
|
## Shipped
|
|
29
27
|
|
|
30
|
-
|
|
28
|
+
- **A Zod codegen plugin**, [`@nxgt/graphql-codegen-zod`](https://www.npmjs.com/package/@nxgt/graphql-codegen-zod):
|
|
29
|
+
it writes the same schemas as source, from the same rules, so the types your
|
|
30
|
+
resolvers receive and the variables a client sends come from your SDL. This
|
|
31
|
+
package gains `@nxgt/graphql-validation/codegen` for it.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxgt/graphql-validation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Validate GraphQL arguments and input fields with @constraint directives, checked by Zod schemas, for graphql 16 and 17",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -23,6 +23,11 @@
|
|
|
23
23
|
"import": "./dist/index.js",
|
|
24
24
|
"default": "./dist/index.js"
|
|
25
25
|
},
|
|
26
|
+
"./codegen": {
|
|
27
|
+
"types": "./dist/codegen.d.ts",
|
|
28
|
+
"import": "./dist/codegen.js",
|
|
29
|
+
"default": "./dist/codegen.js"
|
|
30
|
+
},
|
|
26
31
|
"./package.json": "./package.json"
|
|
27
32
|
},
|
|
28
33
|
"keywords": [
|
|
@@ -52,6 +57,7 @@
|
|
|
52
57
|
"nxgt": {
|
|
53
58
|
"entrypoints": [
|
|
54
59
|
"src/index.ts",
|
|
60
|
+
"src/codegen.ts",
|
|
55
61
|
"src/cli.ts"
|
|
56
62
|
]
|
|
57
63
|
},
|