@fabrika/check 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,108 @@
1
+ // A fixed input that a schema shape admits, written into a generated test as a literal. Deterministic, so the checker
2
+ // regenerates the same text; `undefined` when the scaffold cannot build one (a pattern, contradictory bounds).
3
+
4
+ import type { NumberShape, SchemaShape, StringShape } from '@fabrika/auth-core'
5
+
6
+ export type Sample = null | boolean | number | string | readonly Sample[] | { readonly [key: string]: Sample }
7
+
8
+ /** What a shape contributes to its object: a value, nothing (an optional field), or no sample at all. */
9
+ type Field = { readonly kind: 'value'; readonly value: Sample } | { readonly kind: 'omit' } | undefined
10
+
11
+ const FORMATS: Readonly<Record<NonNullable<StringShape['format']>, string>> = {
12
+ uuid: '00000000-0000-4000-8000-000000000000',
13
+ email: 'sample@example.test',
14
+ url: 'https://example.test/',
15
+ datetime: '2026-01-01T00:00:00.000Z',
16
+ date: '2026-01-01',
17
+ time: '12:00:00',
18
+ duration: 'PT1H',
19
+ }
20
+
21
+ function sampleString(shape: StringShape): string | undefined {
22
+ if (shape.pattern !== null) return undefined
23
+ if (shape.format !== null) return FORMATS[shape.format]
24
+ let value = `${shape.startsWith ?? ''}${shape.includes ?? ''}${shape.endsWith ?? ''}`
25
+ const least = Math.max(shape.minLength ?? 1, 1)
26
+ if (value.length < least) {
27
+ value = `${shape.startsWith ?? ''}${shape.includes ?? ''}${'x'.repeat(least - value.length)}${shape.endsWith ?? ''}`
28
+ }
29
+ return shape.maxLength !== null && value.length > shape.maxLength ? undefined : value
30
+ }
31
+
32
+ function admits(shape: NumberShape, value: number): boolean {
33
+ if (shape.int && !Number.isInteger(value)) return false
34
+ if (shape.min !== null && (shape.exclusiveMin ? value <= shape.min : value < shape.min)) return false
35
+ if (shape.max !== null && (shape.exclusiveMax ? value >= shape.max : value > shape.max)) return false
36
+ return shape.multipleOf === null || Number.isInteger(value / shape.multipleOf)
37
+ }
38
+
39
+ function sampleNumber(shape: NumberShape): number | undefined {
40
+ const step = shape.multipleOf ?? 1
41
+ const start = shape.min ?? (shape.max === null ? 1 : Math.min(1, shape.max))
42
+ const first = Math.ceil(start / step) * step
43
+ return [first, first + step].find((candidate) => admits(shape, candidate))
44
+ }
45
+
46
+ function field(shape: SchemaShape): Field {
47
+ if (shape.type === 'optional') return { kind: 'omit' }
48
+ const value = sample(shape)
49
+ return value === undefined ? undefined : { kind: 'value', value }
50
+ }
51
+
52
+ /** A value `shape` admits, or `undefined`. */
53
+ export function sample(shape: SchemaShape): Sample | undefined {
54
+ switch (shape.type) {
55
+ case 'string':
56
+ return sampleString(shape)
57
+ case 'number':
58
+ return sampleNumber(shape)
59
+ case 'boolean':
60
+ return false
61
+ case 'null':
62
+ return null
63
+ case 'literal':
64
+ return shape.value
65
+ case 'enum':
66
+ return shape.values[0]
67
+ case 'optional':
68
+ return sample(shape.inner)
69
+ case 'nullable':
70
+ return null
71
+ case 'union':
72
+ return shape.options.map(sample).find((value) => value !== undefined)
73
+ case 'record':
74
+ return {}
75
+ case 'array': {
76
+ const count = shape.minItems ?? 0
77
+ const item = count === 0 ? null : sample(shape.items)
78
+ return item === undefined ? undefined : Array.from({ length: count }, () => item)
79
+ }
80
+ case 'tuple': {
81
+ const items = shape.items.map(sample)
82
+ return items.every((item) => item !== undefined) ? items.filter((item) => item !== undefined) : undefined
83
+ }
84
+ case 'object': {
85
+ const entries: [string, Sample][] = []
86
+ for (const [name, inner] of Object.entries(shape.fields).sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))) {
87
+ const result = field(inner)
88
+ if (result === undefined) return undefined
89
+ if (result.kind === 'value') entries.push([name, result.value])
90
+ }
91
+ return Object.fromEntries(entries)
92
+ }
93
+ }
94
+ }
95
+
96
+ const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/
97
+
98
+ const quote = (text: string): string => `'${text.replaceAll('\\', '\\\\').replaceAll("'", "\\'")}'`
99
+
100
+ /** The literal as dprint writes it: single quotes, bare keys where they are identifiers. */
101
+ export function renderSample(value: Sample): string {
102
+ if (value === null || typeof value === 'boolean' || typeof value === 'number') return String(value)
103
+ if (typeof value === 'string') return quote(value)
104
+ if (Array.isArray(value)) return `[${value.map(renderSample).join(', ')}]`
105
+ const entries = Object.entries(value)
106
+ if (entries.length === 0) return '{}'
107
+ return `{ ${entries.map(([key, item]) => `${IDENTIFIER.test(key) ? key : quote(key)}: ${renderSample(item)}`).join(', ')} }`
108
+ }