@fougere/schema 0.3.0-alpha.0 → 0.4.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 (334) hide show
  1. package/README.md +1 -1
  2. package/dist/EntityAdapters.d.ts +26 -0
  3. package/dist/EntityAdapters.d.ts.map +1 -0
  4. package/dist/EntityAdapters.js +71 -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 +8 -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 +23 -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 +2 -2
  124. package/dist/projection/standard.d.ts.map +1 -1
  125. package/dist/{Schema.d.ts → schema/Schema.d.ts} +30 -34
  126. package/dist/schema/Schema.d.ts.map +1 -0
  127. package/dist/{Schema.js → schema/Schema.js} +25 -48
  128. package/dist/schema/Schema.js.map +1 -0
  129. package/dist/schema/SchemaDefinition.d.ts +46 -0
  130. package/dist/schema/SchemaDefinition.d.ts.map +1 -0
  131. package/dist/schema/SchemaDefinition.js +64 -0
  132. package/dist/schema/SchemaDefinition.js.map +1 -0
  133. package/dist/schema/SchemaDerivation.d.ts +28 -0
  134. package/dist/schema/SchemaDerivation.d.ts.map +1 -0
  135. package/dist/schema/SchemaDerivation.js +37 -0
  136. package/dist/schema/SchemaDerivation.js.map +1 -0
  137. package/dist/schema/SchemaView.d.ts +23 -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 +8 -4
  211. package/src/EntityAdapters.ts +95 -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 +15 -0
  238. package/src/fields/Field.ts +80 -0
  239. package/src/fields/FieldSet.ts +62 -0
  240. package/src/index.ts +64 -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 +47 -0
  252. package/src/schema/Schema.ts +167 -0
  253. package/src/schema/SchemaDefinition.ts +87 -0
  254. package/src/schema/SchemaDerivation.ts +46 -0
  255. package/src/schema/SchemaView.ts +25 -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.map +0 -1
  283. package/dist/Schema.js.map +0 -1
  284. package/dist/SchemaView.d.ts +0 -18
  285. package/dist/SchemaView.d.ts.map +0 -1
  286. package/dist/axis/boundary/axis.d.ts.map +0 -1
  287. package/dist/axis/boundary/axis.js.map +0 -1
  288. package/dist/axis/lifecycle/axis.d.ts.map +0 -1
  289. package/dist/axis/lifecycle/axis.js.map +0 -1
  290. package/dist/axis/role/axis.d.ts.map +0 -1
  291. package/dist/axis/role/axis.js.map +0 -1
  292. package/dist/card/describe.d.ts +0 -12
  293. package/dist/card/describe.d.ts.map +0 -1
  294. package/dist/card/describe.js +0 -83
  295. package/dist/card/describe.js.map +0 -1
  296. package/dist/card/reconstruct.d.ts +0 -11
  297. package/dist/card/reconstruct.d.ts.map +0 -1
  298. package/dist/card/reconstruct.js +0 -83
  299. package/dist/card/reconstruct.js.map +0 -1
  300. package/dist/card/typescript.d.ts +0 -17
  301. package/dist/card/typescript.d.ts.map +0 -1
  302. package/dist/card/typescript.js.map +0 -1
  303. package/dist/clean.d.ts +0 -2
  304. package/dist/clean.d.ts.map +0 -1
  305. package/dist/clean.js +0 -7
  306. package/dist/clean.js.map +0 -1
  307. package/dist/judge/Judge.d.ts +0 -14
  308. package/dist/judge/Judge.d.ts.map +0 -1
  309. package/dist/judge/Judge.js +0 -161
  310. package/dist/judge/Judge.js.map +0 -1
  311. package/dist/judge/cases.d.ts.map +0 -1
  312. package/dist/judge/cases.js.map +0 -1
  313. package/dist/judge/form.d.ts.map +0 -1
  314. package/dist/judge/form.js.map +0 -1
  315. package/dist/name.d.ts +0 -3
  316. package/dist/name.d.ts.map +0 -1
  317. package/dist/name.js +0 -7
  318. package/dist/name.js.map +0 -1
  319. package/dist/projection/encode.d.ts +0 -3
  320. package/dist/projection/encode.d.ts.map +0 -1
  321. package/dist/projection/encode.js +0 -19
  322. package/dist/projection/encode.js.map +0 -1
  323. package/dist/projection/io.d.ts +0 -4
  324. package/dist/projection/io.d.ts.map +0 -1
  325. package/dist/projection/io.js +0 -29
  326. package/dist/projection/io.js.map +0 -1
  327. package/dist/projection/source.d.ts +0 -7
  328. package/dist/projection/source.d.ts.map +0 -1
  329. package/dist/projection/source.js.map +0 -1
  330. package/dist/same.d.ts +0 -6
  331. package/dist/same.d.ts.map +0 -1
  332. package/dist/same.js +0 -35
  333. package/dist/same.js.map +0 -1
  334. /package/dist/{SchemaView.js → schema/SchemaView.js} +0 -0
@@ -1,6 +1,6 @@
1
- import { EXTENSION_AXES } from '../axis/Axis.js';
2
- import { same } from '../same.js';
3
- const MEMBER_SLOTS = [...EXTENSION_AXES.map((axis) => axis.slot), 'meta'];
1
+ import { EXTENSION_SLOTS } from '../axis/Axis.js';
2
+ import { dequal } from 'dequal';
3
+ const MEMBER_SLOTS = [...EXTENSION_SLOTS, 'meta'];
4
4
  export function vocabulary(name, states) {
5
5
  return (field) => field.with(merge(name, field, states(field)));
6
6
  }
@@ -19,7 +19,7 @@ function merge(name, field, stated) {
19
19
  }
20
20
  for (const [member, value] of Object.entries(members)) {
21
21
  const previous = held?.[member];
22
- if (previous === undefined || same(previous, value))
22
+ if (previous === undefined || dequal(previous, value))
23
23
  continue;
24
24
  throw new Error(`vocabulary: \`${name}\` states ${slot}.${member} = ${JSON.stringify(value)}, but the ` +
25
25
  `field already states ${JSON.stringify(previous)}. Apply one or the other, not both.`);
@@ -1 +1 @@
1
- {"version":3,"file":"vocabulary.js","sourceRoot":"","sources":["../../src/vocabulary/vocabulary.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAElC,MAAM,YAAY,GAAG,CAAC,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,CAAU,CAAC;AAEnF,MAAM,UAAU,UAAU,CACxB,IAAY,EACZ,MAA4C;IAE5C,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAClE,CAAC;AAID,SAAS,KAAK,CAAC,IAAY,EAAE,KAAY,EAAE,MAA0B;IACnE,MAAM,GAAG,GAA4B,EAAE,CAAC;IACxC,IAAI,OAAO,IAAI,MAAM;QAAE,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IAEhD,KAAK,MAAM,IAAI,IAAI,YAAY,EAAE,CAAC;QAChC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QAC7B,IAAI,OAAO,KAAK,SAAS;YAAE,SAAS;QACpC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAwC,CAAC;QAChE,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAChF,GAAG,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;YACpB,SAAS;QACX,CAAC;QACD,KAAK,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACtD,MAAM,QAAQ,GAAG,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC;YAChC,IAAI,QAAQ,KAAK,SAAS,IAAI,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC;gBAAE,SAAS;YAC9D,MAAM,IAAI,KAAK,CACb,iBAAiB,IAAI,aAAa,IAAI,IAAI,MAAM,MAAM,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,YAAY;gBACrF,wBAAwB,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,qCAAqC,CACxF,CAAC;QACJ,CAAC;QACD,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC;IACtC,CAAC;IACD,OAAO,GAAyB,CAAC;AACnC,CAAC"}
1
+ {"version":3,"file":"vocabulary.js","sourceRoot":"","sources":["../../src/vocabulary/vocabulary.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,YAAY,GAAG,CAAC,GAAG,eAAe,EAAE,MAAM,CAAU,CAAC;AAE3D,MAAM,UAAU,UAAU,CACxB,IAAY,EACZ,MAAwC;IAExC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAClE,CAAC;AAID,SAAS,KAAK,CAAC,IAAY,EAAE,KAAY,EAAE,MAAsB;IAC/D,MAAM,GAAG,GAA4B,EAAE,CAAC;IACxC,IAAI,OAAO,IAAI,MAAM;QAAE,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IAEhD,KAAK,MAAM,IAAI,IAAI,YAAY,EAAE,CAAC;QAChC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QAC7B,IAAI,OAAO,KAAK,SAAS;YAAE,SAAS;QACpC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAwC,CAAC;QAChE,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAChF,GAAG,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;YACpB,SAAS;QACX,CAAC;QACD,KAAK,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACtD,MAAM,QAAQ,GAAG,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC;YAChC,IAAI,QAAQ,KAAK,SAAS,IAAI,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC;gBAAE,SAAS;YAChE,MAAM,IAAI,KAAK,CACb,iBAAiB,IAAI,aAAa,IAAI,IAAI,MAAM,MAAM,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,YAAY;gBACrF,wBAAwB,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,qCAAqC,CACxF,CAAC;QACJ,CAAC;QACD,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC;IACtC,CAAC;IACD,OAAO,GAAqB,CAAC;AAC/B,CAAC"}
@@ -1,3 +1,3 @@
1
- import { Field } from '../Field.js';
1
+ import { Field } from '../fields/Field.js';
2
2
  export declare const writeOnly: <T>(field: Field<T>) => Field<T>;
3
3
  //# sourceMappingURL=writeOnly.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"writeOnly.d.ts","sourceRoot":"","sources":["../../src/vocabulary/writeOnly.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpC,eAAO,MAAM,SAAS,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAEpD,CAAC"}
1
+ {"version":3,"file":"writeOnly.d.ts","sourceRoot":"","sources":["../../src/vocabulary/writeOnly.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,eAAO,MAAM,SAAS,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAEpD,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fougere/schema",
3
- "version": "0.3.0-alpha.0",
4
- "description": "The entity and its 4 axes — shape, role, lifecycle, boundary. One judge, edge-safe.",
3
+ "version": "0.4.0-alpha.0",
4
+ "description": "The entity and its 4 axes — shape, role, lifecycle, boundary. Subject-owned judgments, edge-safe.",
5
5
  "keywords": [
6
6
  "fougere",
7
7
  "typescript",
@@ -29,13 +29,17 @@
29
29
  }
30
30
  },
31
31
  "files": [
32
- "dist"
32
+ "dist",
33
+ "src"
33
34
  ],
34
35
  "dependencies": {
35
36
  "@cfworker/json-schema": "^4.1.1",
36
- "@paralleldrive/cuid2": "^3.3.0"
37
+ "@paralleldrive/cuid2": "^3.3.0",
38
+ "dequal": "^2.0.3"
37
39
  },
38
40
  "devDependencies": {
41
+ "@tanstack/form-core": "^1.33.5",
42
+ "@trpc/server": "^11.18.0",
39
43
  "@types/json-schema": "^7.0.15"
40
44
  },
41
45
  "publishConfig": {
@@ -0,0 +1,95 @@
1
+ import type { FieldName, Fields } from './fields/Field.js';
2
+ import { isObject } from './judge/ValueForm.js';
3
+
4
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-object-type
5
+ export interface FougereEntityAdapters<K extends string> {}
6
+
7
+ export type EntityAdapters<TFields extends Fields> = Partial<FougereEntityAdapters<FieldName<TFields>>>;
8
+
9
+ /** What `cut` REQUIRES of an augmentation, and the open registry cannot state: addressed by field. */
10
+ type FieldEntries = Record<FieldName<Fields>, unknown>;
11
+
12
+ type AdapterEntries = Record<string, FieldEntries>;
13
+
14
+ /**
15
+ * What one schema states per adapter: an adapter name, a field name, then that adapter's own value.
16
+ */
17
+ export class EntityAdapterSet {
18
+ private constructor(private readonly byAdapter: AdapterEntries) {}
19
+
20
+ static of(declared: EntityAdapters<Fields> | undefined): EntityAdapterSet | undefined {
21
+ if (declared === undefined) return undefined;
22
+
23
+ if (!isObject(declared)) {
24
+ throw new Error(
25
+ `adapters: expected an object keyed by adapter name, got ${typeof declared}.`,
26
+ );
27
+ }
28
+
29
+ const byAdapter: AdapterEntries = {};
30
+
31
+ for (const [adapter, perField] of Object.entries(declared as AdapterEntries)) {
32
+ if (perField === undefined) continue;
33
+
34
+ if (!isObject(perField)) {
35
+ throw new Error(
36
+ `adapters.${adapter}: expected an object keyed by field name, got ${typeof perField}. ` +
37
+ `What an adapter is handed is addressed by the field it applies to.`,
38
+ );
39
+ }
40
+
41
+ if (Object.keys(perField).length) byAdapter[adapter] = perField;
42
+ }
43
+
44
+ return EntityAdapterSet.made(byAdapter);
45
+ }
46
+
47
+ /** Several schemas folded into one set; a later source wins, per adapter and per field. */
48
+ static merged(sources: readonly (EntityAdapters<Fields> | undefined)[]): EntityAdapterSet | undefined {
49
+ const out: AdapterEntries = {};
50
+
51
+ for (const source of sources) {
52
+ const set = EntityAdapterSet.of(source);
53
+ if (!set) continue;
54
+
55
+ for (const [adapter, perField] of Object.entries(set.byAdapter)) {
56
+ out[adapter] = { ...out[adapter], ...perField };
57
+ }
58
+ }
59
+
60
+ return EntityAdapterSet.made(out);
61
+ }
62
+
63
+ /** The plain object, which is what a reader outside this package receives. */
64
+ get stated(): EntityAdapters<Fields> {
65
+ return this.byAdapter as EntityAdapters<Fields>;
66
+ }
67
+
68
+ /**
69
+ * The entries whose field survives a cut, re-addressed by that field's new name.
70
+ *
71
+ * The adapter name is not a field and does not move; only the level below it does. An
72
+ * entry left on the old name is not an error anywhere downstream — `adapter/sql` looks
73
+ * it up by the field it is iterating and simply finds nothing.
74
+ */
75
+ cut(survives: (key: string) => string | undefined): EntityAdapterSet | undefined {
76
+ const out: AdapterEntries = {};
77
+
78
+ for (const [adapter, perField] of Object.entries(this.byAdapter)) {
79
+ const mapped: FieldEntries = {};
80
+
81
+ for (const [key, entry] of Object.entries(perField)) {
82
+ const next = survives(key);
83
+ if (next !== undefined) mapped[next] = entry;
84
+ }
85
+
86
+ if (Object.keys(mapped).length) out[adapter] = mapped;
87
+ }
88
+
89
+ return EntityAdapterSet.made(out);
90
+ }
91
+
92
+ private static made(byAdapter: AdapterEntries): EntityAdapterSet | undefined {
93
+ return Object.keys(byAdapter).length ? new EntityAdapterSet(byAdapter) : undefined;
94
+ }
95
+ }
@@ -0,0 +1,19 @@
1
+ import type { FieldName, Fields } from './fields/Field.js';
2
+ import type { EntityAdapters } from './EntityAdapters.js';
3
+
4
+ export type CompositeUnique<TFields extends Fields> = readonly (readonly FieldName<TFields>[])[];
5
+
6
+ /** New name to old name: the field states what it WAS, which is how a human writes it. */
7
+ export type PreviousNames<TFields extends Fields> = Partial<
8
+ Record<FieldName<TFields>, string>
9
+ >;
10
+
11
+ export interface EntityDeclarations<TFields extends Fields> {
12
+ unique?: CompositeUnique<TFields>;
13
+ adapters?: EntityAdapters<TFields>;
14
+ /**
15
+ * What a field used to be called — read by `fougere freeze` and by nothing else.
16
+ * It answers the one question two shapes cannot, and is meant to be deleted after.
17
+ */
18
+ previous?: PreviousNames<TFields>;
19
+ }
@@ -0,0 +1,25 @@
1
+ import { roleAxis } from './role/RoleAxis.js';
2
+ import { lifecycleAxis } from './lifecycle/LifecycleAxis.js';
3
+ import { boundaryAxis } from './boundary/BoundaryAxis.js';
4
+ import type { ValidationError } from '../judge/result.js';
5
+
6
+ export interface Axis<Declared = unknown, Wire = unknown> {
7
+ readonly slot: 'role' | 'lifecycle' | 'boundary';
8
+
9
+ judge(value: unknown, errors: ValidationError[]): void;
10
+
11
+ describe(value: Declared, key: string): Wire | undefined;
12
+
13
+ reconstruct(wire: Wire, resolve?: Resolver): Declared;
14
+ }
15
+
16
+ export type Resolver = (name: string) => (abstract new (...args: never[]) => unknown) | undefined;
17
+
18
+ export const EXTENSION_AXES: readonly Axis[] = [
19
+ roleAxis,
20
+ lifecycleAxis,
21
+ boundaryAxis,
22
+ ];
23
+
24
+ /** The slots they occupy: the keys of `x-fougere` on a field descriptor. */
25
+ export const EXTENSION_SLOTS = EXTENSION_AXES.map((axis) => axis.slot);
@@ -0,0 +1,3 @@
1
+ export interface Meta {
2
+ description?: string;
3
+ }
@@ -0,0 +1,44 @@
1
+ import type { BoundaryRules } from './Boundary.js';
2
+
3
+ export type Decoder = (value: unknown) => { value: unknown } | { error: string };
4
+ export type Encoder = (value: unknown) => unknown;
5
+
6
+ export class Boundaries {
7
+ private static readonly decoders = new Map<string, Decoder>();
8
+ private static readonly encoders = new Map<string, Encoder>();
9
+ private static readonly aliases = new Map<string, BoundaryRules>();
10
+
11
+ static registerDecoder(name: string, fn: Decoder): void { this.decoders.set(name, fn); }
12
+ static registerEncoder(name: string, fn: Encoder): void { this.encoders.set(name, fn); }
13
+
14
+ static registerAlias(name: string, boundary: BoundaryRules): void { this.aliases.set(name, boundary); }
15
+
16
+ static alias(name: string): BoundaryRules | undefined { return this.aliases.get(name); }
17
+
18
+ static decoder(name: string): Decoder { return this.named(this.decoders, name, 'decoder'); }
19
+ static encoder(name: string): Encoder { return this.named(this.encoders, name, 'encoder'); }
20
+
21
+ private static named<T>(registry: Map<string, T>, name: string, kind: string): T {
22
+ const fn = registry.get(name);
23
+ if (!fn) {
24
+ throw new Error(
25
+ `Unknown boundary ${kind}: '${name}'. Register it with ` +
26
+ `Boundaries.register${kind[0].toUpperCase()}${kind.slice(1)}('${name}', …).`,
27
+ );
28
+ }
29
+ return fn;
30
+ }
31
+ }
32
+
33
+ Boundaries.registerDecoder('isoDate', (value) => {
34
+ if (value instanceof Date) return { value };
35
+ if (typeof value === 'string') {
36
+ const d = new Date(value);
37
+ return Number.isNaN(d.getTime()) ? { error: 'Invalid date' } : { value: d };
38
+ }
39
+ return { error: 'Expected a date' };
40
+ });
41
+ Boundaries.registerEncoder('isoDate', (value) =>
42
+ value instanceof Date ? value.toISOString() : value,
43
+ );
44
+ Boundaries.registerAlias('isoDate', { in: { decode: 'isoDate' }, out: { encode: 'isoDate' } });
@@ -0,0 +1,69 @@
1
+ import type { Decoder, Encoder } from './Boundaries.js';
2
+ import { Boundaries } from './Boundaries.js';
3
+ import type { Field } from '../../fields/Field.js';
4
+ import { Anatomy, type Shape } from '../shape/Shape.js';
5
+
6
+ export interface BoundaryRules {
7
+ in?: 'closed' | { decode: string };
8
+ out?: 'closed' | { encode: string };
9
+ }
10
+
11
+ export type BoundaryRef = 'isoDate' | (string & {}) | BoundaryRules;
12
+
13
+ const identityDecoder: Decoder = (value) => ({ value });
14
+ const identityEncoder: Encoder = (value) => value;
15
+
16
+ export class Boundary implements BoundaryRules {
17
+ readonly in?: BoundaryRules['in'];
18
+ readonly out?: BoundaryRules['out'];
19
+ readonly decode: Decoder;
20
+ readonly encode: Encoder;
21
+
22
+ private constructor(rules: BoundaryRules = {}, codecs?: { decode: Decoder; encode: Encoder }) {
23
+ this.in = rules.in;
24
+ this.out = rules.out;
25
+ this.decode = codecs?.decode ?? identityDecoder;
26
+ this.encode = codecs?.encode ?? identityEncoder;
27
+ }
28
+
29
+ static declared(field: Field): Boundary {
30
+ const ref = field.boundary;
31
+ if (ref === undefined) return new Boundary();
32
+ if (typeof ref !== 'string') return new Boundary(ref);
33
+
34
+ const alias = Boundaries.alias(ref);
35
+ if (!alias) throw new Error(`Unknown boundary alias: '${ref}'`);
36
+ return new Boundary(alias);
37
+ }
38
+
39
+ static of(field: Field): Boundary {
40
+ const declared = Boundary.declared(field);
41
+ const derived = Boundary.forShape(field.shape);
42
+ const rules: BoundaryRules = { in: declared.in ?? derived.in, out: declared.out ?? derived.out };
43
+ return new Boundary(rules, {
44
+ decode: typeof rules.in === 'object' ? Boundaries.decoder(rules.in.decode) : identityDecoder,
45
+ encode: typeof rules.out === 'object' ? Boundaries.encoder(rules.out.encode) : identityEncoder,
46
+ });
47
+ }
48
+
49
+ static forShape(shape: Shape | undefined): Boundary {
50
+ const base = Anatomy.of(shape).base;
51
+ if (base?.type === 'string' && base.format === 'date-time') {
52
+ return new Boundary(Boundaries.alias('isoDate')!);
53
+ }
54
+ return new Boundary();
55
+ }
56
+
57
+ with(overrides: BoundaryRules): Boundary {
58
+ return new Boundary({ in: overrides.in ?? this.in, out: overrides.out ?? this.out });
59
+ }
60
+
61
+ get readOnly(): boolean {
62
+ return this.in === 'closed';
63
+ }
64
+
65
+ get writeOnly(): boolean {
66
+ return this.out === 'closed';
67
+ }
68
+
69
+ }
@@ -0,0 +1,32 @@
1
+ import type { Axis } from '../Axis.js';
2
+ import { admitWire } from '../../card/admission.js';
3
+ import { isObject } from '../../judge/ValueForm.js';
4
+ import type { BoundaryRef } from './Boundary.js';
5
+
6
+ export const boundaryAxis: Axis<BoundaryRef, BoundaryRef> = {
7
+ slot: 'boundary',
8
+
9
+ judge(value, errors) {
10
+ if (typeof value === 'string') return;
11
+ if (!isObject(value)) {
12
+ errors.push({
13
+ path: 'boundary',
14
+ message: `Expected an alias name or { in, out } — got ${JSON.stringify(value)}`,
15
+ });
16
+ return;
17
+ }
18
+ for (const [side, verb] of [['in', 'decode'], ['out', 'encode']] as const) {
19
+ const rule = value[side];
20
+ if (rule === undefined || rule === 'closed') continue;
21
+ if (!isObject(rule) || typeof rule[verb] !== 'string') {
22
+ errors.push({ path: `boundary.${side}`, message: `Expected 'closed' or { ${verb}: <name> }` });
23
+ }
24
+ }
25
+ },
26
+
27
+ describe: (value) => value,
28
+ reconstruct: (wire) => {
29
+ admitWire(boundaryAxis.judge, wire, 'boundary');
30
+ return wire;
31
+ },
32
+ };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * What "now" means, for the two functions that stamp it.
3
+ *
4
+ * The ONE place either of them reads the time, which is what makes it substitutable at
5
+ * all: `created()`, `updated()` and `create: 'now'` are realized through this and nowhere
6
+ * else, so a test that needs a stable instant sets it here instead of intercepting `Date`
7
+ * globally — where Rails had to build `travel_to` over the language.
8
+ *
9
+ * A value rather than a mock: nothing here is a channel, and a frozen clock is a fact
10
+ * about the run, not an interception of a call.
11
+ */
12
+ export class Clock {
13
+ private static reading: () => number = Date.now;
14
+
15
+ static now(): number {
16
+ return this.reading();
17
+ }
18
+
19
+ /** Freeze the instant this clock reads. Returns the gesture that restores it. */
20
+ static freeze(at: number | Date): () => void {
21
+ const previous = this.reading;
22
+ const instant = at instanceof Date ? at.getTime() : at;
23
+ this.reading = () => instant;
24
+ return () => { this.reading = previous; };
25
+ }
26
+ }
@@ -0,0 +1,46 @@
1
+ import { createId } from '@paralleldrive/cuid2';
2
+
3
+ export type GeneratorRef = 'cuid2' | 'uuid' | 'nanoid' | (string & {});
4
+
5
+ const NANOID_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
6
+
7
+ function nanoid(): string {
8
+ const bytes = new Uint8Array(21);
9
+ globalThis.crypto.getRandomValues(bytes);
10
+ return Array.from(bytes, (byte) => NANOID_ALPHABET[byte & 63]).join('');
11
+ }
12
+
13
+ /**
14
+ * Who answers a `generate` by name — one registry per process, like `Formats` and
15
+ * `Boundaries`.
16
+ *
17
+ * The three builtins are REGISTERED rather than switched on, which is what brings the
18
+ * refusal back inside: a registry whose unknown name is refused by a `default:` branch in
19
+ * its caller does not know what it holds, so it cannot say so.
20
+ */
21
+ export class Generators {
22
+ private static readonly registry = new Map<string, () => string>([
23
+ ['cuid2', createId],
24
+ ['uuid', () => globalThis.crypto.randomUUID()],
25
+ ['nanoid', nanoid],
26
+ ]);
27
+
28
+ static register(name: string, fn: () => string): void {
29
+ this.registry.set(name, fn);
30
+ }
31
+
32
+ /** The generator this name stands for, refused by name when nothing does. */
33
+ static resolve(ref: GeneratorRef): () => string {
34
+ const found = this.registry.get(ref);
35
+ if (found) return found;
36
+ throw new Error(
37
+ `Unknown generator '${ref}' — register it with Generators.register('${ref}', fn). ` +
38
+ `This process answers ${[...this.registry.keys()].join(', ')}.`,
39
+ );
40
+ }
41
+
42
+ /** Whether a name is answered, for a caller that must not throw to find out. */
43
+ static answers(ref: GeneratorRef): boolean {
44
+ return this.registry.has(ref);
45
+ }
46
+ }
@@ -0,0 +1,53 @@
1
+ import type { GeneratorRef } from './Generators.js';
2
+
3
+ export const CREATE_TOKENS = ['now', 'optional'] as const;
4
+ export const UPDATE_TOKENS = ['now', 'forbidden'] as const;
5
+
6
+ export interface LifecycleRules {
7
+ create?: { value: unknown } | { generate: GeneratorRef } | (typeof CREATE_TOKENS)[number];
8
+ update?: (typeof UPDATE_TOKENS)[number];
9
+ }
10
+
11
+ export class Lifecycle implements LifecycleRules {
12
+ readonly create?: LifecycleRules['create'];
13
+ readonly update?: LifecycleRules['update'];
14
+
15
+ private constructor(rules: LifecycleRules = {}) {
16
+ this.create = rules.create;
17
+ this.update = rules.update;
18
+ }
19
+
20
+ static of(field: { lifecycle?: LifecycleRules }): Lifecycle {
21
+ return new Lifecycle(field.lifecycle);
22
+ }
23
+
24
+ get requiredAtCreate(): boolean {
25
+ return this.create === undefined;
26
+ }
27
+
28
+ get stampedOnce(): boolean {
29
+ return this.create === 'now' && this.update !== 'now';
30
+ }
31
+
32
+ get immutable(): boolean {
33
+ return this.update === 'forbidden';
34
+ }
35
+
36
+ get stampedOnUpdate(): boolean {
37
+ return this.update === 'now';
38
+ }
39
+
40
+ get literal(): { value: unknown } | undefined {
41
+ const rule = this.create;
42
+ return typeof rule === 'object' && rule !== null && 'value' in rule
43
+ ? { value: (rule as { value: unknown }).value }
44
+ : undefined;
45
+ }
46
+
47
+ get generator(): GeneratorRef | undefined {
48
+ const rule = this.create;
49
+ return typeof rule === 'object' && rule !== null && 'generate' in rule
50
+ ? (rule as { generate: GeneratorRef }).generate
51
+ : undefined;
52
+ }
53
+ }
@@ -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
+ }