@fabrika/check 0.9.0-dev.3 → 0.9.0-dev.31
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/package.json +12 -7
- package/src/ast.ts +69 -18
- package/src/bun-plugin.ts +4 -27
- package/src/check.ts +157 -44
- package/src/cli.ts +34 -8
- package/src/contract.ts +68 -11
- package/src/dialect.ts +29 -0
- package/src/finding.ts +21 -8
- package/src/graph.ts +49 -12
- package/src/index.ts +4 -12
- package/src/inputs.ts +5 -2
- package/src/layout.ts +41 -12
- package/src/limits.ts +15 -5
- package/src/migration-base.ts +85 -0
- package/src/reader/schema.ts +42 -512
- package/src/reader/surface.ts +48 -7
- package/src/reader/value.ts +41 -8
- package/src/references.ts +16 -1
- package/src/resolution.ts +2 -2
- package/src/rules/cloudflare.ts +59 -0
- package/src/rules/compile.ts +32 -0
- package/src/rules/contract.ts +3 -2
- package/src/rules/framework.ts +2 -2
- package/src/rules/identity.ts +6 -9
- package/src/rules/literal.ts +42 -9
- package/src/rules/migration-history.ts +70 -0
- package/src/rules/migrations.ts +248 -9
- package/src/rules/modules.ts +9 -3
- package/src/rules/platform-migrations.ts +100 -0
- package/src/rules/runtime.ts +45 -14
- package/src/rules/structure.ts +172 -19
- package/src/rules/tests.ts +9 -60
- package/src/rules/types.ts +17 -16
- package/src/shell-modules.ts +53 -0
- package/src/slice.ts +6 -5
- package/src/source-maps.ts +2 -0
- package/src/trust.ts +8 -4
- package/src/vite-plugin.ts +65 -0
- package/types/src/ast.d.ts +6 -2
- package/types/src/check.d.ts +3 -0
- package/types/src/contract.d.ts +13 -1
- package/types/src/dialect.d.ts +8 -0
- package/types/src/finding.d.ts +7 -6
- package/types/src/graph.d.ts +12 -2
- package/types/src/index.d.ts +4 -3
- package/types/src/layout.d.ts +25 -10
- package/types/src/limits.d.ts +14 -4
- package/types/src/migration-base.d.ts +9 -0
- package/types/src/reader/schema.d.ts +5 -2
- package/types/src/reader/value.d.ts +3 -3
- package/types/src/references.d.ts +6 -1
- package/types/src/rules/cloudflare.d.ts +9 -0
- package/types/src/rules/compile.d.ts +7 -0
- package/types/src/rules/framework.d.ts +1 -1
- package/types/src/rules/literal.d.ts +1 -1
- package/types/src/rules/migration-history.d.ts +12 -0
- package/types/src/rules/migrations.d.ts +28 -0
- package/types/src/rules/platform-migrations.d.ts +15 -0
- package/types/src/rules/runtime.d.ts +7 -2
- package/types/src/rules/structure.d.ts +18 -4
- package/types/src/rules/tests.d.ts +6 -14
- package/types/src/rules/types.d.ts +2 -0
- package/types/src/shell-modules.d.ts +7 -0
- package/types/src/slice.d.ts +8 -2
- package/types/src/vite-plugin.d.ts +27 -0
- package/src/endpoint-fixtures.ts +0 -221
- package/src/rules/crontab.ts +0 -147
- package/src/rules/isolation.ts +0 -79
- package/src/scaffold-sample.ts +0 -111
- package/src/scaffold.ts +0 -650
- package/types/src/endpoint-fixtures.d.ts +0 -36
- package/types/src/rules/crontab.d.ts +0 -15
- package/types/src/rules/isolation.d.ts +0 -18
- package/types/src/scaffold-sample.d.ts +0 -8
- package/types/src/scaffold.d.ts +0 -28
package/src/reader/schema.ts
CHANGED
|
@@ -1,551 +1,81 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
1
|
+
// A `z` chain of the contract subset read as the schema `@fabrika/app/schema` holds as data, one factory or method at a
|
|
2
|
+
// time. Each step is replayed against zod at once, through the same replay the runtime builds with, so a refused step
|
|
3
|
+
// is reported where it stands, and `@fabrika/app`'s `describeContract` reads exactly the schema the evaluated module
|
|
4
|
+
// would hold. The reader keeps the recipe, not the zod: what it builds stays in the bounded cache of the replay.
|
|
3
5
|
|
|
6
|
+
import { DataEnum, DataObject, DataSchema, SCHEMA_SUBSET, SchemaRefusal, Unreadable, zodSchemaOf } from '@fabrika/app/schema'
|
|
4
7
|
import { z } from 'zod/v4'
|
|
5
8
|
import type { LiteralValue } from './value'
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
/** A schema the reader holds: one it read from a chain, or a framework schema (`signedToken()`, `file()`). */
|
|
11
|
+
export type ReadSchema = DataSchema | z.ZodType
|
|
8
12
|
|
|
9
|
-
export type
|
|
10
|
-
readonly ok: false
|
|
11
|
-
readonly message: string
|
|
12
|
-
readonly allowed: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const SUBSET = 'a schema of the supported z subset (packages/check/CLAUDE.md)'
|
|
13
|
+
export type SchemaStep = { readonly factory: string; readonly iso: boolean } | { readonly method: string; readonly receiver: ReadSchema | undefined }
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
export type BuiltSchema = { readonly ok: true; readonly schema: DataSchema } | {
|
|
16
|
+
readonly ok: false
|
|
19
17
|
readonly message: string
|
|
20
18
|
readonly allowed: string
|
|
21
|
-
|
|
22
|
-
constructor(message: string, allowed: string) {
|
|
23
|
-
this.message = message
|
|
24
|
-
this.allowed = allowed
|
|
25
|
-
}
|
|
26
19
|
}
|
|
27
20
|
|
|
28
21
|
export function buildSchema(step: SchemaStep, args: readonly LiteralValue[]): BuiltSchema {
|
|
29
22
|
try {
|
|
30
|
-
const
|
|
31
|
-
? (step.
|
|
32
|
-
:
|
|
33
|
-
|
|
23
|
+
const node = 'factory' in step
|
|
24
|
+
? nodeOf(step.factory, step.iso, null, args.map(argument))
|
|
25
|
+
: nodeOf(step.method, false, receiverOf(step.receiver), args.map(argument))
|
|
26
|
+
// Built now, so a refusal points at its step; the build stays cached for the step that follows.
|
|
27
|
+
zodSchemaOf(node)
|
|
28
|
+
return { ok: true, schema: node }
|
|
34
29
|
} catch (error) {
|
|
35
|
-
if (error instanceof
|
|
30
|
+
if (error instanceof SchemaRefusal) {
|
|
36
31
|
return { ok: false, message: error.message, allowed: error.allowed }
|
|
37
32
|
}
|
|
38
|
-
|
|
39
|
-
const message = error instanceof Error ? (error.message.split('\n')[0] ?? error.message) : 'zod refused the schema'
|
|
40
|
-
return { ok: false, message: `zod refuses this schema: ${message}`, allowed: SUBSET }
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function factory(name: string, args: readonly LiteralValue[]): z.ZodType {
|
|
45
|
-
const call = `z.${name}()`
|
|
46
|
-
switch (name) {
|
|
47
|
-
case 'string':
|
|
48
|
-
return z.string(messageParams(call, only(call, args, 0)))
|
|
49
|
-
case 'number':
|
|
50
|
-
return z.number(messageParams(call, only(call, args, 0)))
|
|
51
|
-
case 'int':
|
|
52
|
-
return z.int(messageParams(call, only(call, args, 0)))
|
|
53
|
-
case 'boolean':
|
|
54
|
-
return z.boolean(messageParams(call, only(call, args, 0)))
|
|
55
|
-
case 'null':
|
|
56
|
-
return z.null(messageParams(call, only(call, args, 0)))
|
|
57
|
-
case 'uuid':
|
|
58
|
-
return z.uuid(uuidParams(call, only(call, args, 0)))
|
|
59
|
-
case 'email':
|
|
60
|
-
return z.email(messageParams(call, only(call, args, 0)))
|
|
61
|
-
case 'url':
|
|
62
|
-
return z.url(messageParams(call, only(call, args, 0)))
|
|
63
|
-
case 'literal': {
|
|
64
|
-
const [value, params] = between(call, args, 1, 2)
|
|
65
|
-
const literal = literalValues(call, value)
|
|
66
|
-
return Array.isArray(literal) ? z.literal(literal, messageParams(call, params)) : z.literal(literal, messageParams(call, params))
|
|
67
|
-
}
|
|
68
|
-
case 'enum': {
|
|
69
|
-
const [values, params] = between(call, args, 1, 2)
|
|
70
|
-
const entries = enumEntries(call, values)
|
|
71
|
-
return Array.isArray(entries) ? z.enum(entries, messageParams(call, params)) : z.enum(entries, messageParams(call, params))
|
|
72
|
-
}
|
|
73
|
-
case 'strictObject': {
|
|
74
|
-
const [shape, params] = between(call, args, 1, 2)
|
|
75
|
-
return z.strictObject(schemaShape(call, shape), messageParams(call, params))
|
|
76
|
-
}
|
|
77
|
-
case 'array': {
|
|
78
|
-
const [element, params] = between(call, args, 1, 2)
|
|
79
|
-
return z.array(schema(call, element), messageParams(call, params))
|
|
80
|
-
}
|
|
81
|
-
case 'tuple':
|
|
82
|
-
return tuple(call, args)
|
|
83
|
-
case 'union': {
|
|
84
|
-
const [options, params] = between(call, args, 1, 2)
|
|
85
|
-
return z.union(schemaList(call, options), messageParams(call, params))
|
|
86
|
-
}
|
|
87
|
-
case 'discriminatedUnion': {
|
|
88
|
-
const [discriminator, options, params] = between(call, args, 2, 3)
|
|
89
|
-
const [first, ...rest] = schemaList(call, options).map((option) => objectSchema(call, option))
|
|
90
|
-
if (first === undefined) {
|
|
91
|
-
throw new Refusal(`${call} has no option`, 'z.discriminatedUnion(<key>, [z.strictObject({ … }), …])')
|
|
92
|
-
}
|
|
93
|
-
return z.discriminatedUnion(text(call, discriminator), [first, ...rest], messageParams(call, params))
|
|
94
|
-
}
|
|
95
|
-
case 'record': {
|
|
96
|
-
const [key, value, params] = between(call, args, 2, 3)
|
|
97
|
-
return z.record(recordKey(call, schema(call, key)), schema(call, value), messageParams(call, params))
|
|
98
|
-
}
|
|
99
|
-
case 'optional':
|
|
100
|
-
return z.optional(schema(call, exactly(call, args, 1)[0]))
|
|
101
|
-
case 'nullable':
|
|
102
|
-
return z.nullable(schema(call, exactly(call, args, 1)[0]))
|
|
103
|
-
default:
|
|
104
|
-
throw new Refusal(`'${name}' is not in the supported schema subset`, SUBSET)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function isoFormat(name: string, args: readonly LiteralValue[]): z.ZodType {
|
|
109
|
-
const call = `z.iso.${name}()`
|
|
110
|
-
switch (name) {
|
|
111
|
-
case 'date':
|
|
112
|
-
return z.iso.date(messageParams(call, only(call, args, 0)))
|
|
113
|
-
case 'datetime':
|
|
114
|
-
return z.iso.datetime(datetimeParams(call, only(call, args, 0)))
|
|
115
|
-
case 'time':
|
|
116
|
-
return z.iso.time(timeParams(call, only(call, args, 0)))
|
|
117
|
-
case 'duration':
|
|
118
|
-
return z.iso.duration(messageParams(call, only(call, args, 0)))
|
|
119
|
-
default:
|
|
120
|
-
throw new Refusal(`'iso.${name}' is not in the supported schema subset`, SUBSET)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function method(name: string, receiver: z.ZodType | undefined, args: readonly LiteralValue[]): z.ZodType {
|
|
125
|
-
const call = `.${name}()`
|
|
126
|
-
if (receiver === undefined) {
|
|
127
|
-
throw new Refusal('a schema method without a schema', SUBSET)
|
|
128
|
-
}
|
|
129
|
-
switch (name) {
|
|
130
|
-
case 'min':
|
|
131
|
-
case 'max':
|
|
132
|
-
case 'length': {
|
|
133
|
-
const [bound, params] = between(call, args, 1, 2)
|
|
134
|
-
const value = number(call, bound)
|
|
135
|
-
const message = messageParams(call, params)
|
|
136
|
-
if (receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat) {
|
|
137
|
-
return name === 'min' ? receiver.min(value, message) : name === 'max' ? receiver.max(value, message) : receiver.length(value, message)
|
|
138
|
-
}
|
|
139
|
-
if (receiver instanceof z.ZodArray) {
|
|
140
|
-
return name === 'min' ? receiver.min(value, message) : name === 'max' ? receiver.max(value, message) : receiver.length(value, message)
|
|
141
|
-
}
|
|
142
|
-
if (receiver instanceof z.ZodNumber && name !== 'length') {
|
|
143
|
-
return name === 'min' ? receiver.min(value, message) : receiver.max(value, message)
|
|
144
|
-
}
|
|
145
|
-
throw unsupported(call, receiver)
|
|
146
|
-
}
|
|
147
|
-
case 'nonempty': {
|
|
148
|
-
const message = messageParams(call, only(call, args, 0))
|
|
149
|
-
if (receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat || receiver instanceof z.ZodArray) {
|
|
150
|
-
return receiver.nonempty(message)
|
|
151
|
-
}
|
|
152
|
-
throw unsupported(call, receiver)
|
|
153
|
-
}
|
|
154
|
-
case 'email':
|
|
155
|
-
case 'url':
|
|
156
|
-
case 'uuid': {
|
|
157
|
-
const params = only(call, args, 0)
|
|
158
|
-
if (!(receiver instanceof z.ZodString)) {
|
|
159
|
-
throw unsupported(call, receiver)
|
|
160
|
-
}
|
|
161
|
-
return name === 'email'
|
|
162
|
-
? receiver.email(messageParams(call, params))
|
|
163
|
-
: name === 'url'
|
|
164
|
-
? receiver.url(messageParams(call, params))
|
|
165
|
-
: receiver.uuid(uuidParams(call, params))
|
|
166
|
-
}
|
|
167
|
-
case 'startsWith':
|
|
168
|
-
case 'endsWith':
|
|
169
|
-
case 'includes':
|
|
170
|
-
case 'regex': {
|
|
171
|
-
const [argument, params] = between(call, args, 1, 2)
|
|
172
|
-
if (!(receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat)) {
|
|
173
|
-
throw unsupported(call, receiver)
|
|
174
|
-
}
|
|
175
|
-
switch (name) {
|
|
176
|
-
case 'startsWith':
|
|
177
|
-
return receiver.startsWith(text(call, argument), messageParams(call, params))
|
|
178
|
-
case 'endsWith':
|
|
179
|
-
return receiver.endsWith(text(call, argument), messageParams(call, params))
|
|
180
|
-
case 'includes':
|
|
181
|
-
return receiver.includes(text(call, argument), includesParams(call, params))
|
|
182
|
-
default:
|
|
183
|
-
return receiver.regex(regex(call, argument), messageParams(call, params))
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
case 'int':
|
|
187
|
-
case 'positive':
|
|
188
|
-
case 'negative':
|
|
189
|
-
case 'nonnegative':
|
|
190
|
-
case 'nonpositive': {
|
|
191
|
-
const message = messageParams(call, only(call, args, 0))
|
|
192
|
-
if (!(receiver instanceof z.ZodNumber)) {
|
|
193
|
-
throw unsupported(call, receiver)
|
|
194
|
-
}
|
|
195
|
-
switch (name) {
|
|
196
|
-
case 'int':
|
|
197
|
-
return receiver.int(message)
|
|
198
|
-
case 'positive':
|
|
199
|
-
return receiver.positive(message)
|
|
200
|
-
case 'negative':
|
|
201
|
-
return receiver.negative(message)
|
|
202
|
-
case 'nonnegative':
|
|
203
|
-
return receiver.nonnegative(message)
|
|
204
|
-
default:
|
|
205
|
-
return receiver.nonpositive(message)
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
case 'gt':
|
|
209
|
-
case 'gte':
|
|
210
|
-
case 'lt':
|
|
211
|
-
case 'lte':
|
|
212
|
-
case 'multipleOf': {
|
|
213
|
-
const [bound, params] = between(call, args, 1, 2)
|
|
214
|
-
const value = number(call, bound)
|
|
215
|
-
const message = messageParams(call, params)
|
|
216
|
-
if (!(receiver instanceof z.ZodNumber)) {
|
|
217
|
-
throw unsupported(call, receiver)
|
|
218
|
-
}
|
|
219
|
-
switch (name) {
|
|
220
|
-
case 'gt':
|
|
221
|
-
return receiver.gt(value, message)
|
|
222
|
-
case 'gte':
|
|
223
|
-
return receiver.gte(value, message)
|
|
224
|
-
case 'lt':
|
|
225
|
-
return receiver.lt(value, message)
|
|
226
|
-
case 'lte':
|
|
227
|
-
return receiver.lte(value, message)
|
|
228
|
-
default:
|
|
229
|
-
return receiver.multipleOf(value, message)
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
case 'optional':
|
|
233
|
-
exactly(call, args, 0)
|
|
234
|
-
return receiver.optional()
|
|
235
|
-
case 'nullable':
|
|
236
|
-
exactly(call, args, 0)
|
|
237
|
-
return receiver.nullable()
|
|
238
|
-
case 'nullish':
|
|
239
|
-
exactly(call, args, 0)
|
|
240
|
-
return receiver.nullish()
|
|
241
|
-
case 'array':
|
|
242
|
-
exactly(call, args, 0)
|
|
243
|
-
return receiver.array()
|
|
244
|
-
case 'default':
|
|
245
|
-
return receiver.default(plain(call, exactly(call, args, 1)[0]))
|
|
246
|
-
case 'describe':
|
|
247
|
-
return receiver.describe(text(call, exactly(call, args, 1)[0]))
|
|
248
|
-
case 'or':
|
|
249
|
-
return receiver.or(schema(call, exactly(call, args, 1)[0]))
|
|
250
|
-
case 'extend':
|
|
251
|
-
case 'pick':
|
|
252
|
-
case 'omit':
|
|
253
|
-
case 'partial': {
|
|
254
|
-
if (!(receiver instanceof z.ZodObject)) {
|
|
255
|
-
throw unsupported(call, receiver)
|
|
256
|
-
}
|
|
257
|
-
if (name === 'extend') {
|
|
258
|
-
return receiver.extend(schemaShape(call, exactly(call, args, 1)[0]))
|
|
259
|
-
}
|
|
260
|
-
if (name === 'partial') {
|
|
261
|
-
const [mask] = between(call, args, 0, 1)
|
|
262
|
-
return mask === undefined ? receiver.partial() : receiver.partial(keyMask(call, mask))
|
|
263
|
-
}
|
|
264
|
-
const mask = keyMask(call, exactly(call, args, 1)[0])
|
|
265
|
-
return name === 'pick' ? receiver.pick(mask) : receiver.omit(mask)
|
|
266
|
-
}
|
|
267
|
-
default:
|
|
268
|
-
throw new Refusal(`'${name}' is not in the supported schema subset`, SUBSET)
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function tuple(call: string, args: readonly LiteralValue[]): z.ZodType {
|
|
273
|
-
const [items, second, third] = between(call, args, 1, 3)
|
|
274
|
-
const [first, ...rest] = schemaList(call, items)
|
|
275
|
-
const restSchema = second?.kind === 'schema' ? second.schema : undefined
|
|
276
|
-
const params = messageParams(call, restSchema === undefined ? second : third)
|
|
277
|
-
if (restSchema === undefined && third !== undefined) {
|
|
278
|
-
throw new Refusal(`${call} takes its items, an optional rest schema and options`, 'z.tuple([<schemas>], <rest schema>)')
|
|
279
|
-
}
|
|
280
|
-
if (first === undefined) {
|
|
281
|
-
if (restSchema !== undefined) {
|
|
282
|
-
throw new Refusal(`${call} with a rest and no item`, 'z.array(<schema>)')
|
|
283
|
-
}
|
|
284
|
-
return z.tuple([], params)
|
|
285
|
-
}
|
|
286
|
-
return restSchema === undefined ? z.tuple([first, ...rest], params) : z.tuple([first, ...rest], restSchema, params)
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// ── Arguments ───────────────────────────────────────────────────────────────────────────────────
|
|
290
|
-
|
|
291
|
-
function between(call: string, args: readonly LiteralValue[], min: number, max: number): readonly (LiteralValue | undefined)[] {
|
|
292
|
-
if (args.length < min || args.length > max) {
|
|
293
|
-
const count = min === max ? `${min}` : `${min} to ${max}`
|
|
294
|
-
throw new Refusal(`${call} takes ${count} arguments, not ${args.length}`, SUBSET)
|
|
295
|
-
}
|
|
296
|
-
return Array.from({ length: max }, (_, index) => args[index])
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
function exactly(call: string, args: readonly LiteralValue[], count: number): readonly (LiteralValue | undefined)[] {
|
|
300
|
-
return between(call, args, count, count)
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/** The one optional argument of a call that takes only options. */
|
|
304
|
-
function only(call: string, args: readonly LiteralValue[], required: 0): LiteralValue | undefined {
|
|
305
|
-
return between(call, args, required, 1)[0]
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
function expected(call: string, what: string, value: LiteralValue | undefined): Refusal {
|
|
309
|
-
return new Refusal(`${call} takes ${what}${value === undefined ? '' : `, not a ${value.kind}`}`, SUBSET)
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function schema(call: string, value: LiteralValue | undefined): z.ZodType {
|
|
313
|
-
if (value?.kind !== 'schema') {
|
|
314
|
-
throw expected(call, 'a schema', value)
|
|
315
|
-
}
|
|
316
|
-
return value.schema
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function number(call: string, value: LiteralValue | undefined): number {
|
|
320
|
-
if (value?.kind !== 'number') {
|
|
321
|
-
throw expected(call, 'a number', value)
|
|
322
|
-
}
|
|
323
|
-
return value.value
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
function text(call: string, value: LiteralValue | undefined): string {
|
|
327
|
-
if (value?.kind !== 'string') {
|
|
328
|
-
throw expected(call, 'a string', value)
|
|
329
|
-
}
|
|
330
|
-
return value.value
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
function regex(call: string, value: LiteralValue | undefined): RegExp {
|
|
334
|
-
if (value?.kind !== 'regex') {
|
|
335
|
-
throw expected(call, 'a regular expression literal', value)
|
|
33
|
+
throw error
|
|
336
34
|
}
|
|
337
|
-
return value.value
|
|
338
35
|
}
|
|
339
36
|
|
|
340
|
-
|
|
341
|
-
if (value?.kind !== 'array') {
|
|
342
|
-
throw expected(call, 'an array of schemas', value)
|
|
343
|
-
}
|
|
344
|
-
return value.items.map((item) => schema(call, item))
|
|
345
|
-
}
|
|
37
|
+
const OBJECT_METHODS: ReadonlySet<string> = new Set(['extend', 'pick', 'omit', 'partial'])
|
|
346
38
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
39
|
+
/** The node of the class `@fabrika/app/schema`'s builder makes for the same call: an object, an enum, or any schema. */
|
|
40
|
+
function nodeOf(name: string, iso: boolean, receiver: DataSchema | null, args: readonly unknown[]): DataSchema {
|
|
41
|
+
const object = receiver === null ? name === 'strictObject' : OBJECT_METHODS.has(name) || (name === 'describe' && isObject(receiver))
|
|
42
|
+
if (object) {
|
|
43
|
+
return new DataObject<z.core.$ZodShape, z.core.$ZodObjectConfig>(name, iso, receiver, args)
|
|
350
44
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
function keyMask(call: string, value: LiteralValue | undefined): Record<string, true> {
|
|
355
|
-
if (value?.kind !== 'object' || [...value.entries.values()].some((entry) => entry.kind !== 'boolean' || !entry.value)) {
|
|
356
|
-
throw expected(call, 'an object of keys set to true', value)
|
|
45
|
+
if ((receiver === null && name === 'enum') || (name === 'describe' && receiver instanceof DataEnum)) {
|
|
46
|
+
return new DataEnum<z.core.util.EnumLike>(name, iso, receiver, args)
|
|
357
47
|
}
|
|
358
|
-
return
|
|
48
|
+
return new DataSchema(name, iso, receiver, args)
|
|
359
49
|
}
|
|
360
50
|
|
|
361
|
-
function
|
|
362
|
-
|
|
363
|
-
throw new Refusal(`${call} takes object options`, 'z.discriminatedUnion(<key>, [z.strictObject({ … }), …])')
|
|
364
|
-
}
|
|
365
|
-
return option
|
|
51
|
+
function isObject(schema: DataSchema): boolean {
|
|
52
|
+
return schema instanceof DataObject || schema.wrapped instanceof z.ZodObject
|
|
366
53
|
}
|
|
367
54
|
|
|
368
|
-
function
|
|
369
|
-
if (
|
|
370
|
-
|
|
55
|
+
function receiverOf(receiver: ReadSchema | undefined): DataSchema {
|
|
56
|
+
if (receiver === undefined) {
|
|
57
|
+
throw new SchemaRefusal('a schema method without a schema', SCHEMA_SUBSET)
|
|
371
58
|
}
|
|
372
|
-
|
|
59
|
+
return receiver instanceof DataSchema ? receiver : DataSchema.wrap(receiver)
|
|
373
60
|
}
|
|
374
61
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
switch (value?.kind) {
|
|
62
|
+
/** A read argument as the evaluated call would get it; any other kind stays one the replay refuses by its name. */
|
|
63
|
+
function argument(value: LiteralValue): unknown {
|
|
64
|
+
switch (value.kind) {
|
|
379
65
|
case 'string':
|
|
380
66
|
case 'number':
|
|
381
67
|
case 'boolean':
|
|
68
|
+
case 'regex':
|
|
382
69
|
return value.value
|
|
383
70
|
case 'null':
|
|
384
71
|
return null
|
|
385
|
-
default:
|
|
386
|
-
throw expected(call, 'a string, number, boolean or null', value)
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
function literalValues(call: string, value: LiteralValue | undefined): Primitive | Primitive[] {
|
|
391
|
-
return value?.kind === 'array' ? value.items.map((item) => primitive(call, item)) : primitive(call, value)
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
function enumEntries(call: string, value: LiteralValue | undefined): string[] | Record<string, string | number> {
|
|
395
|
-
if (value?.kind === 'array') {
|
|
396
|
-
return value.items.map((item) => text(call, item))
|
|
397
|
-
}
|
|
398
|
-
if (value?.kind === 'object') {
|
|
399
|
-
return Object.fromEntries([...value.entries].map(([key, entry]) => [key, entry.kind === 'number' ? entry.value : text(call, entry)]))
|
|
400
|
-
}
|
|
401
|
-
throw expected(call, 'an array of strings or an object', value)
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
/** A value with no schema or builder in it, as the evaluated literal would be. */
|
|
405
|
-
function plain(call: string, value: LiteralValue | undefined): unknown {
|
|
406
|
-
switch (value?.kind) {
|
|
407
72
|
case 'array':
|
|
408
|
-
return value.items.map(
|
|
73
|
+
return value.items.map(argument)
|
|
409
74
|
case 'object':
|
|
410
|
-
return Object.fromEntries([...value.entries].map(([key, entry]) => [key,
|
|
75
|
+
return Object.fromEntries([...value.entries].map(([key, entry]) => [key, argument(entry)]))
|
|
76
|
+
case 'schema':
|
|
77
|
+
return value.schema
|
|
411
78
|
default:
|
|
412
|
-
return
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// ── Options ─────────────────────────────────────────────────────────────────────────────────────
|
|
417
|
-
|
|
418
|
-
/** An error message changes no shape; `error` and `message` are the only options every step takes. */
|
|
419
|
-
interface MessageParams {
|
|
420
|
-
message?: string
|
|
421
|
-
error?: string
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
const MESSAGE_KEYS = ['message', 'error'] as const
|
|
425
|
-
|
|
426
|
-
function options(call: string, value: LiteralValue | undefined, keys: readonly string[]): ReadonlyMap<string, LiteralValue> | string | undefined {
|
|
427
|
-
if (value === undefined || value.kind === 'string') {
|
|
428
|
-
return value?.value
|
|
429
|
-
}
|
|
430
|
-
if (value.kind !== 'object') {
|
|
431
|
-
throw expected(call, 'an error message or an options object', value)
|
|
432
|
-
}
|
|
433
|
-
for (const key of value.entries.keys()) {
|
|
434
|
-
if (!keys.includes(key)) {
|
|
435
|
-
throw new Refusal(`'${key}' is not an option the contract reads for ${call}`, `options among ${keys.join(', ')}`)
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
return value.entries
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
function messageFields(call: string, entries: ReadonlyMap<string, LiteralValue>): MessageParams {
|
|
442
|
-
const params: MessageParams = {}
|
|
443
|
-
const message = entries.get('message')
|
|
444
|
-
const error = entries.get('error')
|
|
445
|
-
if (message !== undefined) {
|
|
446
|
-
params.message = text(call, message)
|
|
447
|
-
}
|
|
448
|
-
if (error !== undefined) {
|
|
449
|
-
params.error = text(call, error)
|
|
79
|
+
return new Unreadable(value.kind)
|
|
450
80
|
}
|
|
451
|
-
return params
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
function messageParams(call: string, value: LiteralValue | undefined): string | MessageParams | undefined {
|
|
455
|
-
const read = options(call, value, MESSAGE_KEYS)
|
|
456
|
-
return read === undefined || typeof read === 'string' ? read : messageFields(call, read)
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
const UUID_VERSIONS = ['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8'] as const
|
|
460
|
-
|
|
461
|
-
type UuidVersion = (typeof UUID_VERSIONS)[number]
|
|
462
|
-
|
|
463
|
-
function isUuidVersion(version: string): version is UuidVersion {
|
|
464
|
-
return UUID_VERSIONS.some((known) => known === version)
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
function uuidParams(call: string, value: LiteralValue | undefined): string | (MessageParams & { version?: UuidVersion }) | undefined {
|
|
468
|
-
const read = options(call, value, [...MESSAGE_KEYS, 'version'])
|
|
469
|
-
if (read === undefined || typeof read === 'string') {
|
|
470
|
-
return read
|
|
471
|
-
}
|
|
472
|
-
const params: MessageParams & { version?: UuidVersion } = messageFields(call, read)
|
|
473
|
-
const version = read.get('version')
|
|
474
|
-
if (version !== undefined) {
|
|
475
|
-
const name = text(call, version)
|
|
476
|
-
if (!isUuidVersion(name)) {
|
|
477
|
-
throw new Refusal(`'${name}' is not a UUID version`, `one of ${UUID_VERSIONS.join(', ')}`)
|
|
478
|
-
}
|
|
479
|
-
params.version = name
|
|
480
|
-
}
|
|
481
|
-
return params
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function precision(call: string, value: LiteralValue): number | null {
|
|
485
|
-
return value.kind === 'null' ? null : number(call, value)
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
function datetimeParams(
|
|
489
|
-
call: string,
|
|
490
|
-
value: LiteralValue | undefined,
|
|
491
|
-
): string | (MessageParams & { offset?: boolean; local?: boolean; precision?: number | null }) | undefined {
|
|
492
|
-
const read = options(call, value, [...MESSAGE_KEYS, 'offset', 'local', 'precision'])
|
|
493
|
-
if (read === undefined || typeof read === 'string') {
|
|
494
|
-
return read
|
|
495
|
-
}
|
|
496
|
-
const params: MessageParams & { offset?: boolean; local?: boolean; precision?: number | null } = messageFields(call, read)
|
|
497
|
-
const offset = read.get('offset')
|
|
498
|
-
const local = read.get('local')
|
|
499
|
-
const digits = read.get('precision')
|
|
500
|
-
if (offset !== undefined) {
|
|
501
|
-
params.offset = flag(call, offset)
|
|
502
|
-
}
|
|
503
|
-
if (local !== undefined) {
|
|
504
|
-
params.local = flag(call, local)
|
|
505
|
-
}
|
|
506
|
-
if (digits !== undefined) {
|
|
507
|
-
params.precision = precision(call, digits)
|
|
508
|
-
}
|
|
509
|
-
return params
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
function timeParams(call: string, value: LiteralValue | undefined): string | (MessageParams & { precision?: number | null }) | undefined {
|
|
513
|
-
const read = options(call, value, [...MESSAGE_KEYS, 'precision'])
|
|
514
|
-
if (read === undefined || typeof read === 'string') {
|
|
515
|
-
return read
|
|
516
|
-
}
|
|
517
|
-
const params: MessageParams & { precision?: number | null } = messageFields(call, read)
|
|
518
|
-
const digits = read.get('precision')
|
|
519
|
-
if (digits !== undefined) {
|
|
520
|
-
params.precision = precision(call, digits)
|
|
521
|
-
}
|
|
522
|
-
return params
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
/** `.includes()` takes its options only as an object. */
|
|
526
|
-
function includesParams(call: string, value: LiteralValue | undefined): (MessageParams & { position?: number }) | undefined {
|
|
527
|
-
const read = options(call, value, [...MESSAGE_KEYS, 'position'])
|
|
528
|
-
if (typeof read === 'string') {
|
|
529
|
-
throw new Refusal(`${call} takes its options as an object`, "{ message: '…' }")
|
|
530
|
-
}
|
|
531
|
-
if (read === undefined) {
|
|
532
|
-
return undefined
|
|
533
|
-
}
|
|
534
|
-
const params: MessageParams & { position?: number } = messageFields(call, read)
|
|
535
|
-
const position = read.get('position')
|
|
536
|
-
if (position !== undefined) {
|
|
537
|
-
params.position = number(call, position)
|
|
538
|
-
}
|
|
539
|
-
return params
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
function flag(call: string, value: LiteralValue): boolean {
|
|
543
|
-
if (value.kind !== 'boolean') {
|
|
544
|
-
throw expected(call, 'a boolean option', value)
|
|
545
|
-
}
|
|
546
|
-
return value.value
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
function unsupported(call: string, receiver: z.ZodType): Refusal {
|
|
550
|
-
return new Refusal(`${call} is not a method of a ${receiver._zod.def.type} schema in the contract subset`, SUBSET)
|
|
551
81
|
}
|