@fabrika/check 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +48 -5
- package/src/ast.ts +268 -0
- package/src/bun-plugin.ts +63 -0
- package/src/check.ts +183 -0
- package/src/cli.ts +88 -0
- package/src/contract.ts +254 -0
- package/src/endpoint-fixtures.ts +221 -0
- package/src/finding.ts +88 -0
- package/src/graph.ts +206 -0
- package/src/index.ts +15 -0
- package/src/inputs.ts +88 -0
- package/src/jsonc.ts +63 -0
- package/src/layout.ts +115 -0
- package/src/limits.ts +15 -0
- package/src/packed.ts +66 -0
- package/src/reader/problems.ts +11 -0
- package/src/reader/schema.ts +551 -0
- package/src/reader/surface.ts +1087 -0
- package/src/reader/tables.ts +116 -0
- package/src/reader/value.ts +644 -0
- package/src/references.ts +95 -0
- package/src/resolution.ts +178 -0
- package/src/rules/contract.ts +78 -0
- package/src/rules/crontab.ts +147 -0
- package/src/rules/framework.ts +177 -0
- package/src/rules/identity.ts +80 -0
- package/src/rules/isolation.ts +79 -0
- package/src/rules/json.ts +28 -0
- package/src/rules/literal.ts +736 -0
- package/src/rules/migrations.ts +87 -0
- package/src/rules/modules.ts +226 -0
- package/src/rules/packages.ts +168 -0
- package/src/rules/runtime.ts +507 -0
- package/src/rules/structure.ts +299 -0
- package/src/rules/tests.ts +208 -0
- package/src/rules/types.ts +62 -0
- package/src/scaffold-sample.ts +108 -0
- package/src/scaffold.ts +605 -0
- package/src/scope.ts +108 -0
- package/src/slice.ts +339 -0
- package/src/toml.ts +159 -0
- package/src/trust.ts +189 -0
- package/src/version.ts +4 -0
- package/README.md +0 -3
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
// Builds the real zod schema a `z` chain of the contract subset denotes, one factory or method at a time. Each step
|
|
2
|
+
// calls zod itself, so `@fabrika/app`'s `describeContract` reads exactly the schema the evaluated module would hold.
|
|
3
|
+
|
|
4
|
+
import { z } from 'zod/v4'
|
|
5
|
+
import type { LiteralValue } from './value'
|
|
6
|
+
|
|
7
|
+
export type SchemaStep = { readonly factory: string; readonly iso: boolean } | { readonly method: string; readonly receiver: z.ZodType | undefined }
|
|
8
|
+
|
|
9
|
+
export type BuiltSchema = { readonly ok: true; readonly schema: z.ZodType } | {
|
|
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)'
|
|
16
|
+
|
|
17
|
+
/** A refused step; thrown inside this module only, and returned as a problem by `buildSchema`. */
|
|
18
|
+
class Refusal {
|
|
19
|
+
readonly message: string
|
|
20
|
+
readonly allowed: string
|
|
21
|
+
|
|
22
|
+
constructor(message: string, allowed: string) {
|
|
23
|
+
this.message = message
|
|
24
|
+
this.allowed = allowed
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function buildSchema(step: SchemaStep, args: readonly LiteralValue[]): BuiltSchema {
|
|
29
|
+
try {
|
|
30
|
+
const schema = 'factory' in step
|
|
31
|
+
? (step.iso ? isoFormat(step.factory, args) : factory(step.factory, args))
|
|
32
|
+
: method(step.method, step.receiver, args)
|
|
33
|
+
return { ok: true, schema }
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (error instanceof Refusal) {
|
|
36
|
+
return { ok: false, message: error.message, allowed: error.allowed }
|
|
37
|
+
}
|
|
38
|
+
// zod refuses some arguments while building, e.g. `.pick()` of a key the object does not have.
|
|
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)
|
|
336
|
+
}
|
|
337
|
+
return value.value
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function schemaList(call: string, value: LiteralValue | undefined): z.ZodType[] {
|
|
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
|
+
}
|
|
346
|
+
|
|
347
|
+
function schemaShape(call: string, value: LiteralValue | undefined): Record<string, z.ZodType> {
|
|
348
|
+
if (value?.kind !== 'object') {
|
|
349
|
+
throw expected(call, 'an object of schemas', value)
|
|
350
|
+
}
|
|
351
|
+
return Object.fromEntries([...value.entries].map(([key, field]) => [key, schema(call, field)]))
|
|
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)
|
|
357
|
+
}
|
|
358
|
+
return Object.fromEntries([...value.entries.keys()].map((key): [string, true] => [key, true]))
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function objectSchema(call: string, option: z.ZodType): z.ZodObject {
|
|
362
|
+
if (!(option instanceof z.ZodObject)) {
|
|
363
|
+
throw new Refusal(`${call} takes object options`, 'z.discriminatedUnion(<key>, [z.strictObject({ … }), …])')
|
|
364
|
+
}
|
|
365
|
+
return option
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function recordKey(call: string, key: z.ZodType): z.ZodString | z.ZodStringFormat | z.ZodEnum {
|
|
369
|
+
if (key instanceof z.ZodString || key instanceof z.ZodStringFormat || key instanceof z.ZodEnum) {
|
|
370
|
+
return key
|
|
371
|
+
}
|
|
372
|
+
throw new Refusal(`${call} takes a string or enum key`, 'z.record(z.string(), <schema>)')
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
type Primitive = string | number | boolean | null
|
|
376
|
+
|
|
377
|
+
function primitive(call: string, value: LiteralValue | undefined): Primitive {
|
|
378
|
+
switch (value?.kind) {
|
|
379
|
+
case 'string':
|
|
380
|
+
case 'number':
|
|
381
|
+
case 'boolean':
|
|
382
|
+
return value.value
|
|
383
|
+
case 'null':
|
|
384
|
+
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
|
+
case 'array':
|
|
408
|
+
return value.items.map((item) => plain(call, item))
|
|
409
|
+
case 'object':
|
|
410
|
+
return Object.fromEntries([...value.entries].map(([key, entry]) => [key, plain(call, entry)]))
|
|
411
|
+
default:
|
|
412
|
+
return primitive(call, value)
|
|
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)
|
|
450
|
+
}
|
|
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
|
+
}
|