@fabrika/check 0.9.0-dev.2 → 0.9.0-dev.21

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