@fougere/schema 0.3.0-alpha.0 → 0.5.0-alpha.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.
Files changed (335) hide show
  1. package/README.md +1 -1
  2. package/dist/EntityAdapters.d.ts +28 -0
  3. package/dist/EntityAdapters.d.ts.map +1 -0
  4. package/dist/EntityAdapters.js +75 -0
  5. package/dist/EntityAdapters.js.map +1 -0
  6. package/dist/EntityDeclarations.d.ts +7 -14
  7. package/dist/EntityDeclarations.d.ts.map +1 -1
  8. package/dist/EntityDeclarations.js +1 -18
  9. package/dist/EntityDeclarations.js.map +1 -1
  10. package/dist/axis/Axis.d.ts +2 -0
  11. package/dist/axis/Axis.d.ts.map +1 -1
  12. package/dist/axis/Axis.js +5 -3
  13. package/dist/axis/Axis.js.map +1 -1
  14. package/dist/axis/boundary/Boundary.d.ts +1 -1
  15. package/dist/axis/boundary/Boundary.d.ts.map +1 -1
  16. package/dist/axis/boundary/{axis.d.ts → BoundaryAxis.d.ts} +1 -1
  17. package/dist/axis/boundary/BoundaryAxis.d.ts.map +1 -0
  18. package/dist/axis/boundary/{axis.js → BoundaryAxis.js} +3 -2
  19. package/dist/axis/boundary/BoundaryAxis.js.map +1 -0
  20. package/dist/axis/lifecycle/Clock.d.ts +18 -0
  21. package/dist/axis/lifecycle/Clock.d.ts.map +1 -0
  22. package/dist/axis/lifecycle/Clock.js +25 -0
  23. package/dist/axis/lifecycle/Clock.js.map +1 -0
  24. package/dist/axis/lifecycle/Generators.d.ts +16 -2
  25. package/dist/axis/lifecycle/Generators.d.ts.map +1 -1
  26. package/dist/axis/lifecycle/Generators.js +35 -5
  27. package/dist/axis/lifecycle/Generators.js.map +1 -1
  28. package/dist/axis/lifecycle/{axis.d.ts → LifecycleAxis.d.ts} +1 -1
  29. package/dist/axis/lifecycle/LifecycleAxis.d.ts.map +1 -0
  30. package/dist/axis/lifecycle/{axis.js → LifecycleAxis.js} +3 -2
  31. package/dist/axis/lifecycle/LifecycleAxis.js.map +1 -0
  32. package/dist/axis/lifecycle/apply.d.ts +1 -3
  33. package/dist/axis/lifecycle/apply.d.ts.map +1 -1
  34. package/dist/axis/lifecycle/apply.js +5 -43
  35. package/dist/axis/lifecycle/apply.js.map +1 -1
  36. package/dist/axis/role/Relation.d.ts +11 -1
  37. package/dist/axis/role/Relation.d.ts.map +1 -1
  38. package/dist/axis/role/Relation.js +18 -3
  39. package/dist/axis/role/Relation.js.map +1 -1
  40. package/dist/axis/role/Role.d.ts +5 -17
  41. package/dist/axis/role/Role.d.ts.map +1 -1
  42. package/dist/axis/role/Role.js +9 -19
  43. package/dist/axis/role/Role.js.map +1 -1
  44. package/dist/axis/role/{axis.d.ts → RoleAxis.d.ts} +1 -1
  45. package/dist/axis/role/RoleAxis.d.ts.map +1 -0
  46. package/dist/axis/role/{axis.js → RoleAxis.js} +5 -4
  47. package/dist/axis/role/RoleAxis.js.map +1 -0
  48. package/dist/card/Bundle.d.ts +15 -0
  49. package/dist/card/Bundle.d.ts.map +1 -0
  50. package/dist/card/Bundle.js +67 -0
  51. package/dist/card/Bundle.js.map +1 -0
  52. package/dist/card/Card.d.ts +27 -0
  53. package/dist/card/Card.d.ts.map +1 -0
  54. package/dist/card/Card.js +230 -0
  55. package/dist/card/Card.js.map +1 -0
  56. package/dist/card/EntityTypeSource.d.ts +12 -0
  57. package/dist/card/EntityTypeSource.d.ts.map +1 -0
  58. package/dist/card/{typescript.js → EntityTypeSource.js} +20 -35
  59. package/dist/card/EntityTypeSource.js.map +1 -0
  60. package/dist/card/FacadeTypeSource.d.ts +19 -0
  61. package/dist/card/FacadeTypeSource.d.ts.map +1 -0
  62. package/dist/card/FacadeTypeSource.js +40 -0
  63. package/dist/card/FacadeTypeSource.js.map +1 -0
  64. package/dist/{judge/form.d.ts → card/admission.d.ts} +2 -4
  65. package/dist/card/admission.d.ts.map +1 -0
  66. package/dist/{judge/form.js → card/admission.js} +1 -3
  67. package/dist/card/admission.js.map +1 -0
  68. package/dist/card/diff.d.ts +11 -55
  69. package/dist/card/diff.d.ts.map +1 -1
  70. package/dist/card/diff.js +1 -122
  71. package/dist/card/diff.js.map +1 -1
  72. package/dist/constraint/FieldGroup.d.ts +2 -2
  73. package/dist/constraint/FieldGroup.d.ts.map +1 -1
  74. package/dist/constraint/FieldGroup.js.map +1 -1
  75. package/dist/entity.d.ts +3 -3
  76. package/dist/entity.d.ts.map +1 -1
  77. package/dist/entity.js +3 -13
  78. package/dist/entity.js.map +1 -1
  79. package/dist/fields/Field.d.ts +24 -0
  80. package/dist/fields/Field.d.ts.map +1 -0
  81. package/dist/{Field.js → fields/Field.js} +17 -15
  82. package/dist/fields/Field.js.map +1 -0
  83. package/dist/fields/FieldSet.d.ts +28 -0
  84. package/dist/fields/FieldSet.d.ts.map +1 -0
  85. package/dist/fields/FieldSet.js +55 -0
  86. package/dist/fields/FieldSet.js.map +1 -0
  87. package/dist/index.d.ts +24 -18
  88. package/dist/index.d.ts.map +1 -1
  89. package/dist/index.js +21 -17
  90. package/dist/index.js.map +1 -1
  91. package/dist/judge/{cases.d.ts → Cases.d.ts} +22 -22
  92. package/dist/judge/Cases.d.ts.map +1 -0
  93. package/dist/judge/{cases.js → Cases.js} +35 -27
  94. package/dist/judge/Cases.js.map +1 -0
  95. package/dist/judge/FieldJudge.d.ts +9 -0
  96. package/dist/judge/FieldJudge.d.ts.map +1 -0
  97. package/dist/judge/FieldJudge.js +54 -0
  98. package/dist/judge/FieldJudge.js.map +1 -0
  99. package/dist/judge/RowJudge.d.ts +12 -0
  100. package/dist/judge/RowJudge.d.ts.map +1 -0
  101. package/dist/judge/RowJudge.js +81 -0
  102. package/dist/judge/RowJudge.js.map +1 -0
  103. package/dist/judge/RowRefusal.d.ts +17 -0
  104. package/dist/judge/RowRefusal.d.ts.map +1 -0
  105. package/dist/judge/RowRefusal.js +16 -0
  106. package/dist/judge/RowRefusal.js.map +1 -0
  107. package/dist/judge/ValueForm.d.ts +3 -0
  108. package/dist/judge/ValueForm.d.ts.map +1 -0
  109. package/dist/judge/ValueForm.js +3 -0
  110. package/dist/judge/ValueForm.js.map +1 -0
  111. package/dist/judge/ValueJudge.d.ts +12 -0
  112. package/dist/judge/ValueJudge.d.ts.map +1 -0
  113. package/dist/judge/ValueJudge.js +60 -0
  114. package/dist/judge/ValueJudge.js.map +1 -0
  115. package/dist/projection/SchemaOrCard.d.ts +7 -0
  116. package/dist/projection/SchemaOrCard.d.ts.map +1 -0
  117. package/dist/projection/{source.js → SchemaOrCard.js} +3 -3
  118. package/dist/projection/SchemaOrCard.js.map +1 -0
  119. package/dist/projection/Visibility.d.ts +10 -0
  120. package/dist/projection/Visibility.d.ts.map +1 -0
  121. package/dist/projection/Visibility.js +55 -0
  122. package/dist/projection/Visibility.js.map +1 -0
  123. package/dist/projection/standard.d.ts +11 -32
  124. package/dist/projection/standard.d.ts.map +1 -1
  125. package/dist/schema/Schema.d.ts +80 -0
  126. package/dist/schema/Schema.d.ts.map +1 -0
  127. package/dist/{Schema.js → schema/Schema.js} +46 -48
  128. package/dist/schema/Schema.js.map +1 -0
  129. package/dist/schema/SchemaDefinition.d.ts +60 -0
  130. package/dist/schema/SchemaDefinition.d.ts.map +1 -0
  131. package/dist/schema/SchemaDefinition.js +85 -0
  132. package/dist/schema/SchemaDefinition.js.map +1 -0
  133. package/dist/schema/SchemaDerivation.d.ts +34 -0
  134. package/dist/schema/SchemaDerivation.d.ts.map +1 -0
  135. package/dist/schema/SchemaDerivation.js +46 -0
  136. package/dist/schema/SchemaDerivation.js.map +1 -0
  137. package/dist/schema/SchemaView.d.ts +25 -0
  138. package/dist/schema/SchemaView.d.ts.map +1 -0
  139. package/dist/{SchemaView.js.map → schema/SchemaView.js.map} +1 -1
  140. package/dist/utils.d.ts +4 -0
  141. package/dist/utils.d.ts.map +1 -0
  142. package/dist/utils.js +13 -0
  143. package/dist/utils.js.map +1 -0
  144. package/dist/vocabulary/bool.d.ts +1 -1
  145. package/dist/vocabulary/bool.d.ts.map +1 -1
  146. package/dist/vocabulary/bool.js +1 -1
  147. package/dist/vocabulary/bool.js.map +1 -1
  148. package/dist/vocabulary/created.d.ts +1 -1
  149. package/dist/vocabulary/created.d.ts.map +1 -1
  150. package/dist/vocabulary/date.d.ts +1 -1
  151. package/dist/vocabulary/date.d.ts.map +1 -1
  152. package/dist/vocabulary/date.js +1 -1
  153. package/dist/vocabulary/date.js.map +1 -1
  154. package/dist/vocabulary/email.d.ts +1 -1
  155. package/dist/vocabulary/email.d.ts.map +1 -1
  156. package/dist/vocabulary/immutable.d.ts +1 -1
  157. package/dist/vocabulary/immutable.d.ts.map +1 -1
  158. package/dist/vocabulary/indexed.d.ts +1 -1
  159. package/dist/vocabulary/indexed.d.ts.map +1 -1
  160. package/dist/vocabulary/json.d.ts +2 -2
  161. package/dist/vocabulary/json.d.ts.map +1 -1
  162. package/dist/vocabulary/json.js +6 -4
  163. package/dist/vocabulary/json.js.map +1 -1
  164. package/dist/vocabulary/list.d.ts +1 -1
  165. package/dist/vocabulary/list.d.ts.map +1 -1
  166. package/dist/vocabulary/list.js +1 -1
  167. package/dist/vocabulary/list.js.map +1 -1
  168. package/dist/vocabulary/many.d.ts +1 -1
  169. package/dist/vocabulary/many.d.ts.map +1 -1
  170. package/dist/vocabulary/many.js +3 -3
  171. package/dist/vocabulary/many.js.map +1 -1
  172. package/dist/vocabulary/nullable.d.ts +1 -1
  173. package/dist/vocabulary/nullable.d.ts.map +1 -1
  174. package/dist/vocabulary/number.d.ts +1 -1
  175. package/dist/vocabulary/number.d.ts.map +1 -1
  176. package/dist/vocabulary/number.js +1 -1
  177. package/dist/vocabulary/number.js.map +1 -1
  178. package/dist/vocabulary/oneOf.d.ts +1 -1
  179. package/dist/vocabulary/oneOf.d.ts.map +1 -1
  180. package/dist/vocabulary/oneOf.js +1 -1
  181. package/dist/vocabulary/oneOf.js.map +1 -1
  182. package/dist/vocabulary/optional.d.ts +1 -1
  183. package/dist/vocabulary/optional.d.ts.map +1 -1
  184. package/dist/vocabulary/primary.d.ts +1 -1
  185. package/dist/vocabulary/primary.d.ts.map +1 -1
  186. package/dist/vocabulary/primary.js +4 -5
  187. package/dist/vocabulary/primary.js.map +1 -1
  188. package/dist/vocabulary/readOnly.d.ts +1 -1
  189. package/dist/vocabulary/readOnly.d.ts.map +1 -1
  190. package/dist/vocabulary/ref.d.ts +1 -1
  191. package/dist/vocabulary/ref.d.ts.map +1 -1
  192. package/dist/vocabulary/ref.js +3 -9
  193. package/dist/vocabulary/ref.js.map +1 -1
  194. package/dist/vocabulary/text.d.ts +1 -1
  195. package/dist/vocabulary/text.d.ts.map +1 -1
  196. package/dist/vocabulary/text.js +1 -1
  197. package/dist/vocabulary/text.js.map +1 -1
  198. package/dist/vocabulary/unique.d.ts +1 -1
  199. package/dist/vocabulary/unique.d.ts.map +1 -1
  200. package/dist/vocabulary/updated.d.ts +1 -1
  201. package/dist/vocabulary/updated.d.ts.map +1 -1
  202. package/dist/vocabulary/url.d.ts +1 -1
  203. package/dist/vocabulary/url.d.ts.map +1 -1
  204. package/dist/vocabulary/vocabulary.d.ts +2 -3
  205. package/dist/vocabulary/vocabulary.d.ts.map +1 -1
  206. package/dist/vocabulary/vocabulary.js +4 -4
  207. package/dist/vocabulary/vocabulary.js.map +1 -1
  208. package/dist/vocabulary/writeOnly.d.ts +1 -1
  209. package/dist/vocabulary/writeOnly.d.ts.map +1 -1
  210. package/package.json +9 -4
  211. package/src/EntityAdapters.ts +100 -0
  212. package/src/EntityDeclarations.ts +19 -0
  213. package/src/axis/Axis.ts +25 -0
  214. package/src/axis/Meta.ts +3 -0
  215. package/src/axis/boundary/Boundaries.ts +44 -0
  216. package/src/axis/boundary/Boundary.ts +69 -0
  217. package/src/axis/boundary/BoundaryAxis.ts +32 -0
  218. package/src/axis/lifecycle/Clock.ts +26 -0
  219. package/src/axis/lifecycle/Generators.ts +46 -0
  220. package/src/axis/lifecycle/Lifecycle.ts +53 -0
  221. package/src/axis/lifecycle/LifecycleAxis.ts +49 -0
  222. package/src/axis/lifecycle/apply.ts +38 -0
  223. package/src/axis/role/Relation.ts +31 -0
  224. package/src/axis/role/Role.ts +56 -0
  225. package/src/axis/role/RoleAxis.ts +115 -0
  226. package/src/axis/shape/Formats.ts +22 -0
  227. package/src/axis/shape/Shape.ts +91 -0
  228. package/src/card/Bundle.ts +80 -0
  229. package/src/card/Card.ts +256 -0
  230. package/src/card/Descriptor.ts +68 -0
  231. package/src/card/EntityTypeSource.ts +90 -0
  232. package/src/card/FacadeTypeSource.ts +55 -0
  233. package/src/card/admission.ts +25 -0
  234. package/src/card/diff.ts +56 -0
  235. package/src/constraint/FieldGroup.ts +91 -0
  236. package/src/constraint/Unique.ts +3 -0
  237. package/src/entity.ts +10 -0
  238. package/src/fields/Field.ts +80 -0
  239. package/src/fields/FieldSet.ts +62 -0
  240. package/src/index.ts +69 -0
  241. package/src/judge/Cases.ts +139 -0
  242. package/src/judge/FieldJudge.ts +61 -0
  243. package/src/judge/RowJudge.ts +84 -0
  244. package/src/judge/RowRefusal.ts +17 -0
  245. package/src/judge/ValueForm.ts +7 -0
  246. package/src/judge/ValueJudge.ts +72 -0
  247. package/src/judge/options.ts +3 -0
  248. package/src/judge/result.ts +10 -0
  249. package/src/projection/SchemaOrCard.ts +18 -0
  250. package/src/projection/Visibility.ts +47 -0
  251. package/src/projection/standard.ts +11 -0
  252. package/src/schema/Schema.ts +197 -0
  253. package/src/schema/SchemaDefinition.ts +120 -0
  254. package/src/schema/SchemaDerivation.ts +57 -0
  255. package/src/schema/SchemaView.ts +27 -0
  256. package/src/utils.ts +13 -0
  257. package/src/vocabulary/bool.ts +9 -0
  258. package/src/vocabulary/created.ts +8 -0
  259. package/src/vocabulary/date.ts +5 -0
  260. package/src/vocabulary/email.ts +6 -0
  261. package/src/vocabulary/immutable.ts +6 -0
  262. package/src/vocabulary/indexed.ts +4 -0
  263. package/src/vocabulary/json.ts +22 -0
  264. package/src/vocabulary/list.ts +13 -0
  265. package/src/vocabulary/many.ts +9 -0
  266. package/src/vocabulary/nullable.ts +7 -0
  267. package/src/vocabulary/number.ts +17 -0
  268. package/src/vocabulary/oneOf.ts +24 -0
  269. package/src/vocabulary/optional.ts +8 -0
  270. package/src/vocabulary/primary.ts +33 -0
  271. package/src/vocabulary/readOnly.ts +7 -0
  272. package/src/vocabulary/ref.ts +13 -0
  273. package/src/vocabulary/text.ts +19 -0
  274. package/src/vocabulary/unique.ts +10 -0
  275. package/src/vocabulary/updated.ts +7 -0
  276. package/src/vocabulary/url.ts +6 -0
  277. package/src/vocabulary/vocabulary.ts +40 -0
  278. package/src/vocabulary/writeOnly.ts +7 -0
  279. package/dist/Field.d.ts +0 -21
  280. package/dist/Field.d.ts.map +0 -1
  281. package/dist/Field.js.map +0 -1
  282. package/dist/Schema.d.ts +0 -67
  283. package/dist/Schema.d.ts.map +0 -1
  284. package/dist/Schema.js.map +0 -1
  285. package/dist/SchemaView.d.ts +0 -18
  286. package/dist/SchemaView.d.ts.map +0 -1
  287. package/dist/axis/boundary/axis.d.ts.map +0 -1
  288. package/dist/axis/boundary/axis.js.map +0 -1
  289. package/dist/axis/lifecycle/axis.d.ts.map +0 -1
  290. package/dist/axis/lifecycle/axis.js.map +0 -1
  291. package/dist/axis/role/axis.d.ts.map +0 -1
  292. package/dist/axis/role/axis.js.map +0 -1
  293. package/dist/card/describe.d.ts +0 -12
  294. package/dist/card/describe.d.ts.map +0 -1
  295. package/dist/card/describe.js +0 -83
  296. package/dist/card/describe.js.map +0 -1
  297. package/dist/card/reconstruct.d.ts +0 -11
  298. package/dist/card/reconstruct.d.ts.map +0 -1
  299. package/dist/card/reconstruct.js +0 -83
  300. package/dist/card/reconstruct.js.map +0 -1
  301. package/dist/card/typescript.d.ts +0 -17
  302. package/dist/card/typescript.d.ts.map +0 -1
  303. package/dist/card/typescript.js.map +0 -1
  304. package/dist/clean.d.ts +0 -2
  305. package/dist/clean.d.ts.map +0 -1
  306. package/dist/clean.js +0 -7
  307. package/dist/clean.js.map +0 -1
  308. package/dist/judge/Judge.d.ts +0 -14
  309. package/dist/judge/Judge.d.ts.map +0 -1
  310. package/dist/judge/Judge.js +0 -161
  311. package/dist/judge/Judge.js.map +0 -1
  312. package/dist/judge/cases.d.ts.map +0 -1
  313. package/dist/judge/cases.js.map +0 -1
  314. package/dist/judge/form.d.ts.map +0 -1
  315. package/dist/judge/form.js.map +0 -1
  316. package/dist/name.d.ts +0 -3
  317. package/dist/name.d.ts.map +0 -1
  318. package/dist/name.js +0 -7
  319. package/dist/name.js.map +0 -1
  320. package/dist/projection/encode.d.ts +0 -3
  321. package/dist/projection/encode.d.ts.map +0 -1
  322. package/dist/projection/encode.js +0 -19
  323. package/dist/projection/encode.js.map +0 -1
  324. package/dist/projection/io.d.ts +0 -4
  325. package/dist/projection/io.d.ts.map +0 -1
  326. package/dist/projection/io.js +0 -29
  327. package/dist/projection/io.js.map +0 -1
  328. package/dist/projection/source.d.ts +0 -7
  329. package/dist/projection/source.d.ts.map +0 -1
  330. package/dist/projection/source.js.map +0 -1
  331. package/dist/same.d.ts +0 -6
  332. package/dist/same.d.ts.map +0 -1
  333. package/dist/same.js +0 -35
  334. package/dist/same.js.map +0 -1
  335. /package/dist/{SchemaView.js → schema/SchemaView.js} +0 -0
@@ -0,0 +1,49 @@
1
+ import type { Axis } from '../Axis.js';
2
+ import type { ValidationError } from '../../judge/result.js';
3
+ import { admitWire } from '../../card/admission.js';
4
+ import { isObject, oneOfTokens } from '../../judge/ValueForm.js';
5
+ import { CREATE_TOKENS, UPDATE_TOKENS, type LifecycleRules } from './Lifecycle.js';
6
+
7
+ export const lifecycleAxis: Axis<LifecycleRules, LifecycleRules> = {
8
+ slot: 'lifecycle',
9
+
10
+ judge(value, errors) {
11
+ if (!isObject(value)) {
12
+ errors.push({ path: 'lifecycle', message: `Expected an object — got ${JSON.stringify(value)}` });
13
+ return;
14
+ }
15
+ if (value.create !== undefined) judgeCreate(value.create, errors);
16
+ if (value.update !== undefined && !oneOfTokens(value.update, UPDATE_TOKENS)) {
17
+ errors.push({
18
+ path: 'lifecycle.update',
19
+ message: `Expected 'now' or 'forbidden' — got ${JSON.stringify(value.update)}`,
20
+ });
21
+ }
22
+ },
23
+
24
+ describe: (value) => value,
25
+ reconstruct: (wire) => {
26
+ admitWire(lifecycleAxis.judge, wire, 'lifecycle');
27
+ return wire;
28
+ },
29
+ };
30
+
31
+ function judgeCreate(rule: unknown, errors: ValidationError[]): void {
32
+ if (oneOfTokens(rule, CREATE_TOKENS)) return;
33
+ if (isObject(rule)) {
34
+ if ('value' in rule) return;
35
+ if ('generate' in rule) {
36
+ if (typeof rule.generate !== 'string') {
37
+ errors.push({
38
+ path: 'lifecycle.create.generate',
39
+ message: `Expected a generator name — got ${JSON.stringify(rule.generate)}`,
40
+ });
41
+ }
42
+ return;
43
+ }
44
+ }
45
+ errors.push({
46
+ path: 'lifecycle.create',
47
+ message: `Expected 'now', 'optional', { value } or { generate } — got ${JSON.stringify(rule)}`,
48
+ });
49
+ }
@@ -0,0 +1,38 @@
1
+ import { Lifecycle } from './Lifecycle.js';
2
+ import { Generators } from './Generators.js';
3
+ import { Clock } from './Clock.js';
4
+ import { type Field, type Fields } from '../../fields/Field.js';
5
+
6
+ type Row = Record<string, unknown>;
7
+
8
+ export function applyCreate(fields: Fields, input: Row): Row {
9
+ const out: Row = { ...input };
10
+ const instant = Clock.now();
11
+
12
+ for (const [name, field] of Object.entries(fields) as [string, Field][]) {
13
+ if (name in out) continue;
14
+ const rule = Lifecycle.of(field);
15
+
16
+ if (rule.create === 'now') out[name] = new Date(instant);
17
+ else if (rule.literal) out[name] = freshValue(rule.literal.value);
18
+ else if (rule.generator) out[name] = Generators.resolve(rule.generator)();
19
+ }
20
+
21
+ return out;
22
+ }
23
+
24
+ function freshValue(value: unknown): unknown {
25
+ if (value === null || typeof value !== 'object') return value;
26
+ return structuredClone(value);
27
+ }
28
+
29
+ export function applyUpdate(fields: Fields, patch: Row): Row {
30
+ const out: Row = { ...patch };
31
+ const instant = Clock.now();
32
+
33
+ for (const [name, field] of Object.entries(fields) as [string, Field][]) {
34
+ if (Lifecycle.of(field).stampedOnUpdate && !(name in out)) out[name] = new Date(instant);
35
+ }
36
+
37
+ return out;
38
+ }
@@ -0,0 +1,31 @@
1
+ export type EntityConstructor = abstract new (...args: any[]) => any;
2
+
3
+ export const RELATION_KINDS = ['one', 'many'] as const;
4
+ export const ON_DELETE = ['cascade', 'restrict', 'set null'] as const;
5
+
6
+ export interface Relation {
7
+ to: () => EntityConstructor;
8
+ kind: (typeof RELATION_KINDS)[number];
9
+ onDelete?: (typeof ON_DELETE)[number];
10
+ }
11
+
12
+ /**
13
+ * How a relation is built — the one place a target becomes a thunk.
14
+ *
15
+ * A target arrives as the class or as a function returning it, because a cycle between two
16
+ * entities can only be written the second way. Recognised by FORM, `getFields` on the
17
+ * value: a class answers it, a thunk does not.
18
+ */
19
+ export const Relation = {
20
+ one(target: EntityConstructor | (() => EntityConstructor), cascade?: boolean): Relation {
21
+ return { to: thunk(target), kind: 'one', onDelete: cascade ? 'cascade' : undefined };
22
+ },
23
+
24
+ many(target: EntityConstructor | (() => EntityConstructor)): Relation {
25
+ return { to: thunk(target), kind: 'many' };
26
+ },
27
+ };
28
+
29
+ function thunk<E extends EntityConstructor>(target: E | (() => E)): () => E {
30
+ return 'getFields' in target ? () => target as E : (target as () => E);
31
+ }
@@ -0,0 +1,56 @@
1
+ import { FieldGroup } from "../../constraint/FieldGroup.js";
2
+ import { Unique } from "../../constraint/Unique.js";
3
+ import type { EntityConstructor, Relation } from "./Relation.js";
4
+
5
+ export interface RoleRules {
6
+ primary?: boolean;
7
+ index?: boolean;
8
+ relation?: Relation;
9
+ rules?: readonly FieldGroup[];
10
+ }
11
+
12
+ export class Role implements RoleRules {
13
+ readonly primary?: boolean;
14
+ readonly index?: boolean;
15
+ readonly relation?: Relation;
16
+ readonly rules?: readonly FieldGroup[];
17
+
18
+ private constructor(declared: RoleRules = {}) {
19
+ this.primary = declared.primary;
20
+ this.index = declared.index;
21
+ this.relation = declared.relation;
22
+ this.rules = declared.rules;
23
+ }
24
+
25
+ static of(field: { role?: RoleRules }): Role {
26
+ return new Role(field.role);
27
+ }
28
+
29
+ /** A group written with no members means "this field": only the key it is declared under resolves it. */
30
+ static resolvedOn(role: RoleRules | undefined, key?: string): RoleRules | undefined {
31
+ let rules = FieldGroup.normalize(role?.rules, (members) => new Unique(members));
32
+ if (rules && key !== undefined && rules.some((group) => group.isSelf))
33
+ rules = rules.map((group) => group.resolvedOn(key));
34
+ return rules === role?.rules ? role : { ...role, rules };
35
+ }
36
+
37
+ get isPrimary(): boolean {
38
+ return this.primary === true;
39
+ }
40
+
41
+ get isIndexed(): boolean {
42
+ return this.index === true;
43
+ }
44
+
45
+ get isCollection(): boolean {
46
+ return this.relation?.kind === "many";
47
+ }
48
+
49
+ get isReference(): boolean {
50
+ return this.relation?.kind === "one";
51
+ }
52
+
53
+ get target(): EntityConstructor | undefined {
54
+ return this.relation?.to();
55
+ }
56
+ }
@@ -0,0 +1,115 @@
1
+ import type { Axis, Resolver } from '../Axis.js';
2
+ import { refuse } from '../../card/admission.js';
3
+ import { isObject, oneOfTokens } from '../../judge/ValueForm.js';
4
+ import type { ValidationError } from '../../judge/result.js';
5
+ import { ON_DELETE, RELATION_KINDS, type EntityConstructor, type Relation } from './Relation.js';
6
+ import { type RoleRules } from './Role.js';
7
+ import { FieldGroup } from '../../constraint/FieldGroup.js';
8
+ import { Unique } from '../../constraint/Unique.js';
9
+ import type { RoleDescriptor } from '../../card/Descriptor.js';
10
+ import { lowerFirst } from '../../utils.js';
11
+
12
+ export const roleAxis: Axis<RoleRules, RoleDescriptor> = {
13
+ slot: 'role',
14
+
15
+ judge(value, errors) {
16
+ if (!isObject(value)) {
17
+ errors.push({ path: 'role', message: `Expected an object — got ${JSON.stringify(value)}` });
18
+ return;
19
+ }
20
+ for (const flag of ['primary', 'index'] as const) {
21
+ if (value[flag] !== undefined && typeof value[flag] !== 'boolean') {
22
+ errors.push({
23
+ path: `role.${flag}`,
24
+ message: `Expected a boolean — got ${JSON.stringify(value[flag])}`,
25
+ });
26
+ }
27
+ }
28
+ if (value.rules !== undefined) {
29
+ const legal =
30
+ Array.isArray(value.rules) &&
31
+ value.rules.every(
32
+ (rule) =>
33
+ rule instanceof FieldGroup ||
34
+ (Array.isArray(rule) && rule.every((member) => typeof member === 'string')),
35
+ );
36
+ if (!legal) {
37
+ errors.push({
38
+ path: 'role.rules',
39
+ message: 'Expected field groups, or the member lists they denote — string[][]',
40
+ });
41
+ }
42
+ }
43
+ if (value.relation !== undefined) judgeRelation(value.relation, errors);
44
+ },
45
+
46
+ describe(role, key) {
47
+ const out: Mutable<RoleDescriptor> = {};
48
+ if (role.primary) out.primary = true;
49
+ const unique = (role.rules ?? []).filter((rule) => rule instanceof Unique);
50
+ if (unique.length) out.unique = unique.map((rule) => [...rule.resolvedOn(key).members]);
51
+ if (role.index) out.index = true;
52
+ if (role.relation) {
53
+ const target = role.relation.to() as { name?: string };
54
+ out.relation = {
55
+ to: lowerFirst(target.name ?? ''),
56
+ kind: role.relation.kind,
57
+ ...(role.relation.onDelete ? { onDelete: role.relation.onDelete } : {}),
58
+ };
59
+ }
60
+ return Object.keys(out).length ? out : undefined;
61
+ },
62
+
63
+ reconstruct(wire, resolve?: Resolver) {
64
+ const out: RoleRules = {};
65
+ if (wire.primary) out.primary = true;
66
+ if (wire.unique?.length) out.rules = wire.unique.map((group) => new Unique(group));
67
+ if (wire.index) out.index = true;
68
+ if (wire.relation) {
69
+ // `judge` cannot serve here: it demands a thunk where a card carries a name.
70
+ if (!oneOfTokens(wire.relation.kind, RELATION_KINDS)) {
71
+ refuse(
72
+ `role.relation.kind is ${JSON.stringify(wire.relation.kind)}`,
73
+ `Expected one of ${RELATION_KINDS.join(', ')}.`,
74
+ );
75
+ }
76
+ if (wire.relation.onDelete !== undefined && !oneOfTokens(wire.relation.onDelete, ON_DELETE)) {
77
+ refuse(
78
+ `role.relation.onDelete is ${JSON.stringify(wire.relation.onDelete)}`,
79
+ `Expected one of ${ON_DELETE.join(', ')}.`,
80
+ );
81
+ }
82
+ const name = wire.relation.to;
83
+ out.relation = {
84
+ to: () => (resolve?.(name) ?? ({ name } as unknown)) as EntityConstructor,
85
+ kind: wire.relation.kind as Relation['kind'],
86
+ ...(wire.relation.onDelete ? { onDelete: wire.relation.onDelete as Relation['onDelete'] } : {}),
87
+ } as RoleRules['relation'];
88
+ }
89
+ return out;
90
+ },
91
+ };
92
+
93
+ function judgeRelation(relation: unknown, errors: ValidationError[]): void {
94
+ if (!isObject(relation)) {
95
+ errors.push({ path: 'role.relation', message: `Expected an object — got ${JSON.stringify(relation)}` });
96
+ return;
97
+ }
98
+ if (!oneOfTokens(relation.kind, RELATION_KINDS)) {
99
+ errors.push({
100
+ path: 'role.relation.kind',
101
+ message: `Expected 'one' or 'many' — got ${JSON.stringify(relation.kind)}`,
102
+ });
103
+ }
104
+ if (typeof relation.to !== 'function') {
105
+ errors.push({ path: 'role.relation.to', message: 'Expected a thunk returning the target entity' });
106
+ }
107
+ if (relation.onDelete !== undefined && !oneOfTokens(relation.onDelete, ON_DELETE)) {
108
+ errors.push({
109
+ path: 'role.relation.onDelete',
110
+ message: `Expected 'cascade', 'restrict' or 'set null' — got ${JSON.stringify(relation.onDelete)}`,
111
+ });
112
+ }
113
+ }
114
+
115
+ type Mutable<T> = { -readonly [K in keyof T]: T[K] };
@@ -0,0 +1,22 @@
1
+ export type StringFormat =
2
+ | 'date-time'
3
+ | 'date'
4
+ | 'time'
5
+ | 'email'
6
+ | 'uuid'
7
+ | 'uri'
8
+ | (string & {});
9
+
10
+ export type FormatPredicate = (value: string) => boolean;
11
+
12
+ export class Formats {
13
+ private static readonly registry = new Map<string, FormatPredicate>();
14
+
15
+ static register(name: string, predicate: FormatPredicate): void {
16
+ this.registry.set(name, predicate);
17
+ }
18
+
19
+ static resolve(name: string): FormatPredicate | undefined {
20
+ return this.registry.get(name);
21
+ }
22
+ }
@@ -0,0 +1,91 @@
1
+ import type { JSONSchema7 } from 'json-schema';
2
+ import type { StringFormat } from './Formats.js';
3
+
4
+ type Nullably<T extends string> = T | readonly [T, 'null'];
5
+
6
+ interface StringConstraints { minLength?: number; maxLength?: number; pattern?: string; enum?: readonly (string | null)[]; format?: StringFormat }
7
+ interface NumericConstraints { minimum?: number; maximum?: number }
8
+ interface ArrayConstraints { items?: Shape; minItems?: number; maxItems?: number }
9
+ interface ObjectConstraints { properties?: Record<string, unknown>; required?: readonly string[] }
10
+
11
+ export type Shape =
12
+ | ({ type: Nullably<'string'> } & StringConstraints)
13
+ | ({ type: Nullably<'number'> | Nullably<'integer'> } & NumericConstraints)
14
+ | { type: Nullably<'boolean'> }
15
+ | ({ type: Nullably<'array'> } & ArrayConstraints)
16
+ | ({ type: Nullably<'object'> } & ObjectConstraints);
17
+
18
+ const SHAPE_TYPES = ['string', 'number', 'integer', 'boolean', 'array', 'object'] as const;
19
+
20
+ function isShapeImpl(value: unknown): value is Shape {
21
+ if (typeof value !== 'object' || value === null) return false;
22
+ const declared = (value as Shape).type;
23
+ const names = Array.isArray(declared) ? declared : [declared];
24
+ return (
25
+ names.some((name) => (SHAPE_TYPES as readonly unknown[]).includes(name)) &&
26
+ names.every((name) => name === 'null' || (SHAPE_TYPES as readonly unknown[]).includes(name))
27
+ );
28
+ }
29
+
30
+ type BaseShape =
31
+ | ({ type: 'string' } & StringConstraints)
32
+ | ({ type: 'number' | 'integer' } & NumericConstraints)
33
+ | { type: 'boolean' }
34
+ | ({ type: 'array' } & ArrayConstraints)
35
+ | ({ type: 'object' } & ObjectConstraints);
36
+
37
+ function nullableShapeImpl(shape: Shape): Shape {
38
+ if (Array.isArray(shape.type)) return shape;
39
+ const out = { ...shape, type: [shape.type, 'null'] } as unknown as Shape;
40
+ if ('enum' in out && out.enum && !out.enum.includes(null)) {
41
+ (out as { enum: readonly (string | null)[] }).enum = [...out.enum, null];
42
+ }
43
+ return out;
44
+ }
45
+
46
+ interface ShapeAnatomy {
47
+ base?: BaseShape;
48
+ nullable: boolean;
49
+ }
50
+
51
+ export class Anatomy {
52
+ static is(value: unknown): value is Shape {
53
+ return isShapeImpl(value);
54
+ }
55
+
56
+ static nullable(shape: Shape): Shape {
57
+ return nullableShapeImpl(shape);
58
+ }
59
+
60
+ private static readonly cache = new WeakMap<object, ShapeAnatomy>();
61
+ private static readonly none: ShapeAnatomy = { base: undefined, nullable: false };
62
+
63
+ static of(shape?: Shape): ShapeAnatomy {
64
+ if (!shape) return this.none;
65
+ let a = this.cache.get(shape);
66
+ if (!a) {
67
+ if (Array.isArray(shape.type)) {
68
+ const baseType = shape.type.find((t) => t !== 'null');
69
+ const base = { ...shape, type: baseType } as BaseShape;
70
+ if ('enum' in base && base.enum) {
71
+ (base as { enum: readonly (string | null)[] }).enum = base.enum.filter((v) => v !== null);
72
+ }
73
+ a = { base, nullable: true };
74
+ } else {
75
+ a = { base: shape as BaseShape, nullable: false };
76
+ }
77
+ this.cache.set(shape, a);
78
+ }
79
+ return a;
80
+ }
81
+
82
+ static isNullable(shape?: Shape): boolean {
83
+ return this.of(shape).nullable;
84
+ }
85
+ }
86
+
87
+ type Assert<T extends true> = T;
88
+ type ShapeKeys<T> = T extends unknown ? keyof T : never;
89
+ type _ShapeConformsToJsonSchema = Assert<
90
+ [Exclude<ShapeKeys<Shape>, keyof JSONSchema7>] extends [never] ? true : false
91
+ >;
@@ -0,0 +1,80 @@
1
+ import type { Resolver } from '../axis/Axis.js';
2
+ import type { EntityConstructor } from '../axis/role/Relation.js';
3
+ import { lowerFirst } from '../utils.js';
4
+ import type { SchemaView } from '../schema/SchemaView.js';
5
+ import { Card } from './Card.js';
6
+ import type { SchemaBundle, SchemaDescriptor } from './Descriptor.js';
7
+ import type { Diff, SetDiff, SetDiffOptions } from './diff.js';
8
+
9
+ type SchemaSet = Record<string, SchemaView> | SchemaView[];
10
+
11
+ /** A portable schema map and the decisions that only a map can make. */
12
+ export class Bundle {
13
+ private constructor(readonly descriptor: SchemaBundle) {}
14
+
15
+ static fromSchemas(schemas: SchemaSet): Bundle {
16
+ const definitions: Record<string, SchemaDescriptor> = {};
17
+ const claimedBy = new Map<string, string>();
18
+ const entries = Array.isArray(schemas)
19
+ ? schemas.map((schema) => {
20
+ const card = Card.fromSchema(schema);
21
+ return { name: card.descriptor.title ?? '', schema };
22
+ })
23
+ : Object.entries(schemas).map(([name, schema]) => ({ name, schema }));
24
+
25
+ for (const entry of entries) {
26
+ const key = lowerFirst(entry.name);
27
+ const previous = claimedBy.get(key);
28
+ if (previous !== undefined) {
29
+ throw new Error(
30
+ `Schemas '${previous}' and '${entry.name}' both claim bundle key '${key}'. `
31
+ + 'Each schema in a bundle must have a distinct registration key.',
32
+ );
33
+ }
34
+ claimedBy.set(key, entry.name);
35
+ definitions[key] = Card.fromSchema(entry.schema, key).descriptor;
36
+ }
37
+
38
+ return new Bundle({
39
+ $defs: definitions,
40
+ 'x-fougere-version': 1,
41
+ 'x-fougere-vendor': 'fougere',
42
+ });
43
+ }
44
+
45
+ static fromDescriptor(descriptor: SchemaBundle): Bundle {
46
+ return new Bundle(descriptor);
47
+ }
48
+
49
+ toSchemas(): Record<string, SchemaView> {
50
+ const map: Record<string, EntityConstructor> = {};
51
+ const resolve: Resolver = (name) => map[name.toLowerCase()];
52
+ const schemas: Record<string, SchemaView> = {};
53
+ for (const [name, descriptor] of Object.entries(this.descriptor.$defs)) {
54
+ const schema = Card.fromDescriptor(descriptor).toSchema(resolve, name);
55
+ map[name.toLowerCase()] = schema as unknown as EntityConstructor;
56
+ schemas[name] = schema;
57
+ }
58
+ return schemas;
59
+ }
60
+
61
+ diff(other: Bundle, options: SetDiffOptions = {}): SetDiff {
62
+ const before = this.descriptor.$defs ?? {};
63
+ const after = other.descriptor.$defs ?? {};
64
+ const entities: Record<string, Diff> = {};
65
+ for (const [name, descriptor] of Object.entries(before)) {
66
+ const target = after[name];
67
+ if (target === undefined) continue;
68
+ const answer = Card.fromDescriptor(descriptor).diff(
69
+ Card.fromDescriptor(target),
70
+ { renamed: options.renamed?.[name] ?? {} },
71
+ );
72
+ if (answer.changes.length > 0 || answer.ambiguous.length > 0) entities[name] = answer;
73
+ }
74
+ return {
75
+ entitiesAdded: Object.keys(after).filter((name) => !(name in before)),
76
+ entitiesRemoved: Object.keys(before).filter((name) => !(name in after)),
77
+ entities,
78
+ };
79
+ }
80
+ }