@mapomodule/form 2.0.0-beta.9 → 2.0.0-experimental.2

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 (145) hide show
  1. package/README.md +38 -119
  2. package/dist/index.d.mts +2 -1
  3. package/dist/index.d.ts +3 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.mjs +1 -0
  6. package/dist/module.d.mts +1 -0
  7. package/dist/module.d.ts.map +1 -1
  8. package/dist/module.json +1 -1
  9. package/dist/module.mjs +9 -0
  10. package/dist/runtime/components/MapoFocusPortal.vue +9 -2
  11. package/dist/runtime/components/MapoFocusPortal.vue.d.ts.map +1 -1
  12. package/dist/runtime/components/MapoForm.vue +28 -6
  13. package/dist/runtime/components/MapoForm.vue.d.ts +13 -1
  14. package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
  15. package/dist/runtime/components/MapoFormAnnotation.vue +79 -0
  16. package/dist/runtime/components/MapoFormAnnotation.vue.d.ts +27 -0
  17. package/dist/runtime/components/MapoFormAnnotation.vue.d.ts.map +1 -0
  18. package/dist/runtime/components/MapoFormAnnotationBadges.vue +28 -0
  19. package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts +13 -0
  20. package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts.map +1 -0
  21. package/dist/runtime/components/MapoFormField.vue +47 -11
  22. package/dist/runtime/components/MapoFormField.vue.d.ts +15 -9
  23. package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
  24. package/dist/runtime/components/MapoFormGroup.vue +18 -2
  25. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  26. package/dist/runtime/components/MapoFormTabs.vue +50 -8
  27. package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
  28. package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
  29. package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
  30. package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  31. package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
  32. package/dist/runtime/components/fields/MapoRepeater.vue +73 -14
  33. package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
  34. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +167 -0
  35. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
  36. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
  37. package/dist/runtime/components/fields/MapoRepeaterItem.vue +46 -10
  38. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
  39. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  40. package/dist/runtime/components/fields/MapoWygEditor.vue +19 -13
  41. package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
  42. package/dist/runtime/composables/useFieldAnnotation.d.ts +18 -0
  43. package/dist/runtime/composables/useFieldAnnotation.d.ts.map +1 -0
  44. package/dist/runtime/composables/useFieldAnnotation.js +16 -0
  45. package/dist/runtime/composables/useFocusMode.d.ts +3 -1
  46. package/dist/runtime/composables/useFocusMode.d.ts.map +1 -1
  47. package/dist/runtime/composables/useFormFromSchema.d.ts +23 -1
  48. package/dist/runtime/composables/useFormFromSchema.d.ts.map +1 -1
  49. package/dist/runtime/composables/useFormFromSchema.js +171 -32
  50. package/dist/runtime/composables/useMapoForm.d.ts +21 -3
  51. package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
  52. package/dist/runtime/composables/useMapoForm.js +49 -15
  53. package/dist/runtime/types/annotation.d.ts +43 -0
  54. package/dist/runtime/types/annotation.d.ts.map +1 -0
  55. package/dist/runtime/types/annotation.js +0 -0
  56. package/dist/runtime/types/descriptor.d.ts +81 -4
  57. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  58. package/dist/runtime/types/index.d.ts +2 -1
  59. package/dist/runtime/types/index.d.ts.map +1 -1
  60. package/dist/runtime/utils/annotations.d.ts +40 -0
  61. package/dist/runtime/utils/annotations.d.ts.map +1 -0
  62. package/dist/runtime/utils/annotations.js +78 -0
  63. package/dist/runtime/utils/draft.d.ts +21 -0
  64. package/dist/runtime/utils/draft.d.ts.map +1 -0
  65. package/dist/runtime/utils/draft.js +108 -0
  66. package/dist/runtime/utils/fieldComponent.d.ts +9 -0
  67. package/dist/runtime/utils/fieldComponent.d.ts.map +1 -0
  68. package/dist/runtime/utils/fieldComponent.js +12 -0
  69. package/dist/runtime/utils/fieldFocus.d.ts +24 -0
  70. package/dist/runtime/utils/fieldFocus.d.ts.map +1 -0
  71. package/dist/runtime/utils/fieldFocus.js +41 -0
  72. package/dist/runtime/utils/fieldsToJsonSchema.d.ts +38 -0
  73. package/dist/runtime/utils/fieldsToJsonSchema.d.ts.map +1 -0
  74. package/dist/runtime/utils/fieldsToJsonSchema.js +248 -0
  75. package/dist/runtime/utils/repeaterTemplates.d.ts +26 -0
  76. package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
  77. package/dist/runtime/utils/repeaterTemplates.js +51 -0
  78. package/dist/runtime/utils/reveal.d.ts +34 -0
  79. package/dist/runtime/utils/reveal.d.ts.map +1 -0
  80. package/dist/runtime/utils/reveal.js +29 -0
  81. package/dist/runtime/utils/tabVisibility.d.ts +18 -0
  82. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
  83. package/dist/runtime/utils/tabVisibility.js +18 -0
  84. package/package.json +5 -5
  85. package/src/__tests__/MapoFocusPortal.component.test.ts +97 -0
  86. package/src/__tests__/MapoForm.component.test.ts +79 -0
  87. package/src/__tests__/MapoFormAnnotation.component.test.ts +244 -0
  88. package/src/__tests__/MapoFormField.component.test.ts +508 -0
  89. package/src/__tests__/MapoFormGroup.component.test.ts +141 -0
  90. package/src/__tests__/MapoFormTabs.component.test.ts +254 -0
  91. package/src/__tests__/MapoRepeaterItem.component.test.ts +286 -0
  92. package/src/__tests__/MapoWygEditor.component.test.ts +95 -0
  93. package/src/__tests__/annotations.test.ts +227 -0
  94. package/src/__tests__/draft.test.ts +221 -0
  95. package/src/__tests__/fieldComponent.test.ts +31 -0
  96. package/src/__tests__/fieldExtensions.test.ts +63 -0
  97. package/src/__tests__/fieldFocus.test.ts +178 -0
  98. package/src/__tests__/fieldsToJsonSchema.test.ts +630 -0
  99. package/src/__tests__/headlessRenderer.ts +85 -0
  100. package/src/__tests__/repeaterTemplates.test.ts +154 -0
  101. package/src/__tests__/reveal.test.ts +89 -0
  102. package/src/__tests__/schemaSources.test.ts +132 -0
  103. package/src/__tests__/tabVisibility.test.ts +102 -0
  104. package/src/__tests__/useFormFromSchema.test.ts +387 -0
  105. package/src/__tests__/useMapoForm.test.ts +213 -2
  106. package/src/index.ts +9 -0
  107. package/src/module.ts +9 -0
  108. package/src/runtime/components/MapoFocusPortal.vue +15 -2
  109. package/src/runtime/components/MapoForm.vue +47 -4
  110. package/src/runtime/components/MapoFormAnnotation.vue +113 -0
  111. package/src/runtime/components/MapoFormAnnotationBadges.vue +41 -0
  112. package/src/runtime/components/MapoFormField.vue +72 -11
  113. package/src/runtime/components/MapoFormGroup.vue +20 -2
  114. package/src/runtime/components/MapoFormTabs.vue +64 -8
  115. package/src/runtime/components/fields/MapoMediaField.vue +5 -3
  116. package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  117. package/src/runtime/components/fields/MapoRepeater.vue +82 -14
  118. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +188 -0
  119. package/src/runtime/components/fields/MapoRepeaterItem.vue +56 -8
  120. package/src/runtime/components/fields/MapoWygEditor.vue +22 -14
  121. package/src/runtime/composables/useFieldAnnotation.ts +38 -0
  122. package/src/runtime/composables/useFocusMode.ts +7 -1
  123. package/src/runtime/composables/useFormFromSchema.ts +303 -41
  124. package/src/runtime/composables/useMapoForm.ts +103 -23
  125. package/src/runtime/types/annotation.ts +53 -0
  126. package/src/runtime/types/descriptor.ts +94 -4
  127. package/src/runtime/types/index.ts +10 -0
  128. package/src/runtime/utils/annotations.ts +169 -0
  129. package/src/runtime/utils/draft.ts +158 -0
  130. package/src/runtime/utils/fieldComponent.ts +34 -0
  131. package/src/runtime/utils/fieldFocus.ts +73 -0
  132. package/src/runtime/utils/fieldsToJsonSchema.ts +405 -0
  133. package/src/runtime/utils/repeaterTemplates.ts +103 -0
  134. package/src/runtime/utils/reveal.ts +68 -0
  135. package/src/runtime/utils/tabVisibility.ts +44 -0
  136. package/dist/__tests__/__mocks__/nuxt-imports.d.ts +0 -4
  137. package/dist/__tests__/__mocks__/nuxt-imports.d.ts.map +0 -1
  138. package/dist/__tests__/camomillaRoundTrip.test.d.ts +0 -2
  139. package/dist/__tests__/camomillaRoundTrip.test.d.ts.map +0 -1
  140. package/dist/__tests__/registry.test.d.ts +0 -2
  141. package/dist/__tests__/registry.test.d.ts.map +0 -1
  142. package/dist/__tests__/useFormFromSchema.test.d.ts +0 -2
  143. package/dist/__tests__/useFormFromSchema.test.d.ts.map +0 -1
  144. package/dist/__tests__/useMapoForm.test.d.ts +0 -2
  145. package/dist/__tests__/useMapoForm.test.d.ts.map +0 -1
@@ -0,0 +1,630 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { JSONSchema } from "@mapomodule/utils";
3
+
4
+ import { useFormFromSchema } from "../runtime/composables/useFormFromSchema.js";
5
+ import { defaultRegistry } from "../runtime/registry/defaults.js";
6
+ import { KnownFieldType } from "../runtime/types/index.js";
7
+ import type {
8
+ AnyFieldDescriptor,
9
+ RepeaterTemplate,
10
+ } from "../runtime/types/index.js";
11
+ import { fieldsToJsonSchema } from "../runtime/utils/fieldsToJsonSchema.js";
12
+ import { normalizeTemplates } from "../runtime/utils/repeaterTemplates.js";
13
+
14
+ /** The schema of the only property of a one-field form. */
15
+ function propertyOf(field: AnyFieldDescriptor): JSONSchema {
16
+ const schema = fieldsToJsonSchema([field]);
17
+ return schema.properties![String(field.key)]!;
18
+ }
19
+
20
+ /** `attrs` is not on every member of the descriptor union. */
21
+ function attrsOf(field: AnyFieldDescriptor): Record<string, unknown> {
22
+ return (field as { attrs?: Record<string, unknown> }).attrs ?? {};
23
+ }
24
+
25
+ /**
26
+ * A select's choices as `{ label, value, ...others }`, whatever the shape they
27
+ * were given in. The label is the one the select shows: `text`, then `label`.
28
+ */
29
+ function choicesOf(attrs: Record<string, unknown>) {
30
+ const list = (attrs.items ?? attrs.options) as unknown[] | undefined;
31
+ return list?.map((item) => {
32
+ if (typeof item !== "object" || item === null)
33
+ return { label: String(item), value: item };
34
+ const { text, label, value, ...others } = item as Record<string, unknown>;
35
+ return { label: text ?? label, value, ...others };
36
+ });
37
+ }
38
+
39
+ /**
40
+ * What renders a field: its type, flags and attrs after the registry defaults,
41
+ * with choices and templates in one shape. Layout and functions are left out,
42
+ * as `fieldsToJsonSchema()` leaves them out.
43
+ */
44
+ function effective(field: AnyFieldDescriptor): Record<string, unknown> {
45
+ const type = String(field.type);
46
+ const {
47
+ items: _items,
48
+ options: _options,
49
+ templates,
50
+ ...attrs
51
+ } = {
52
+ ...defaultRegistry.attrs.All,
53
+ ...defaultRegistry.attrs[type],
54
+ ...attrsOf(field),
55
+ };
56
+ const choices = choicesOf(attrsOf(field));
57
+ const fields = (field as { fields?: AnyFieldDescriptor[] }).fields;
58
+ return {
59
+ key: field.key,
60
+ type,
61
+ ...(field.label ? { label: field.label } : {}),
62
+ required: !!field.required,
63
+ readonly: !!field.readonly,
64
+ translatable: !!field.translatable,
65
+ noDraft: !!field.noDraft,
66
+ attrs: {
67
+ ...attrs,
68
+ ...(choices ? { choices } : {}),
69
+ ...(templates
70
+ ? {
71
+ templates: normalizeTemplates(
72
+ templates as Record<
73
+ string,
74
+ AnyFieldDescriptor[] | RepeaterTemplate
75
+ >,
76
+ ).map((template) => ({
77
+ ...template,
78
+ fields: template.fields.map(effective),
79
+ })),
80
+ }
81
+ : {}),
82
+ },
83
+ ...(fields ? { fields: fields.map(effective) } : {}),
84
+ };
85
+ }
86
+
87
+ // One rich descriptor per built-in type, plus the cases that need a mark.
88
+ const roundTrips: AnyFieldDescriptor[] = [
89
+ {
90
+ key: "title",
91
+ type: "text",
92
+ label: "Title",
93
+ required: true,
94
+ translatable: true,
95
+ attrs: { placeholder: "Chi siamo", minLength: 3, maxLength: 120 },
96
+ },
97
+ { key: "subtitle", type: "text", attrs: { maxLength: 300 } },
98
+ {
99
+ key: "summary",
100
+ type: "textarea",
101
+ label: "Summary",
102
+ attrs: { rows: 4, maxLength: 2000 },
103
+ },
104
+ { key: "email", type: "email", label: "Email", attrs: { maxLength: 254 } },
105
+ { key: "website", type: "url", label: "Website" },
106
+ {
107
+ key: "price",
108
+ type: "number",
109
+ label: "Price",
110
+ attrs: { min: 0, max: 1000, step: 0.5 },
111
+ },
112
+ { key: "rating", type: "slider", attrs: { min: 1, max: 5, step: 1 } },
113
+ { key: "is_public", type: "boolean", label: "Public", readonly: true },
114
+ { key: "featured", type: "switch", label: "Featured" },
115
+ { key: "accent", type: "color", label: "Accent" },
116
+ {
117
+ key: "attachment",
118
+ type: "file",
119
+ attrs: { accept: "application/pdf", maxSize: 1_000_000 },
120
+ },
121
+ {
122
+ key: "status",
123
+ type: "select",
124
+ label: "Status",
125
+ attrs: {
126
+ placeholder: "Choose",
127
+ items: [
128
+ { label: "Draft", value: "draft" },
129
+ { label: "Published", value: "published" },
130
+ ],
131
+ },
132
+ },
133
+ {
134
+ key: "topics",
135
+ type: "select",
136
+ attrs: {
137
+ multiple: true,
138
+ options: [
139
+ { text: "Art", value: "art" },
140
+ { text: "Music", value: "music" },
141
+ ],
142
+ },
143
+ },
144
+ { key: "size", type: "select", attrs: { items: ["S", "M", "L"] } },
145
+ {
146
+ key: "visibility",
147
+ type: "select",
148
+ attrs: {
149
+ items: [
150
+ { label: "Public", value: "public", icon: "i-lucide-globe" },
151
+ { label: "Private", value: "private", disabled: true },
152
+ ],
153
+ },
154
+ },
155
+ { key: "priority", type: "select", attrs: { items: [1, 2, 3] } },
156
+ {
157
+ key: "author",
158
+ type: "fks",
159
+ label: "Author",
160
+ attrs: { endpoint: "/api/users/", itemText: "username" },
161
+ },
162
+ {
163
+ key: "category",
164
+ type: "fks",
165
+ attrs: {
166
+ endpoint: "/api/categories/",
167
+ returnObject: false,
168
+ itemValue: "pk",
169
+ },
170
+ },
171
+ {
172
+ key: "editors",
173
+ type: "fks",
174
+ attrs: { endpoint: "/api/users/", multiple: true },
175
+ },
176
+ {
177
+ key: "tags",
178
+ type: "m2m",
179
+ label: "Tags",
180
+ attrs: { endpoint: "/api/tags/" },
181
+ },
182
+ {
183
+ key: "starts_on",
184
+ type: "date",
185
+ attrs: { min: "2026-01-01", max: "2026-12-31" },
186
+ },
187
+ { key: "opens_at", type: "time" },
188
+ { key: "published_at", type: "datetime", attrs: { tz: "utc" } },
189
+ { key: "body", type: "editor", label: "Body", translatable: true },
190
+ { key: "seo", type: "seo", label: "SEO" },
191
+ {
192
+ key: "location",
193
+ type: "map",
194
+ attrs: { defaultLat: 45.46, defaultLng: 9.19, zoom: 12 },
195
+ },
196
+ {
197
+ key: "faq",
198
+ type: "repeater",
199
+ label: "FAQ",
200
+ fields: [
201
+ { key: "question", type: "text", required: true },
202
+ { key: "answer", type: "textarea" },
203
+ ],
204
+ attrs: { minItems: 1, maxItems: 10, confirmDelete: true },
205
+ },
206
+ {
207
+ key: "blocks",
208
+ type: "repeater",
209
+ attrs: {
210
+ picker: "modal",
211
+ templates: {
212
+ hero: {
213
+ label: "Hero",
214
+ description: "A big title over an image",
215
+ icon: "i-lucide-image",
216
+ category: "Intro",
217
+ fields: [
218
+ { key: "title", type: "text", required: true },
219
+ { key: "image", type: "media" },
220
+ ],
221
+ },
222
+ quote: [{ key: "text", type: "textarea" }],
223
+ },
224
+ },
225
+ },
226
+ { key: "cover", type: "media", label: "Cover", attrs: { mime: "image/*" } },
227
+ {
228
+ key: "gallery",
229
+ type: "media-m2m",
230
+ attrs: { mime: "image/*", maxItems: 12 },
231
+ },
232
+ { key: "hero_image", type: "enhanced-media" },
233
+ { key: "mood", type: "rating", label: "Mood", attrs: { stars: 5 } },
234
+ { key: "meta", type: "object:meta" },
235
+ { key: "api_key", type: "text", noDraft: true },
236
+ { key: "seo.title", type: "text", label: "SEO title" },
237
+ ];
238
+
239
+ describe("fieldsToJsonSchema — round trip", () => {
240
+ it("has a case for every built-in type", () => {
241
+ const covered = new Set(roundTrips.map((field) => field.type));
242
+ for (const type of Object.values(KnownFieldType))
243
+ expect(covered, type).toContain(type);
244
+ });
245
+
246
+ it.each(roundTrips.map((field) => [`${field.type} ${field.key}`, field]))(
247
+ "reads back %s as an equivalent descriptor",
248
+ (_name, field) => {
249
+ const [back] = useFormFromSchema(fieldsToJsonSchema([field]));
250
+ expect(effective(back!)).toEqual(effective(field));
251
+ },
252
+ );
253
+
254
+ it("reads a whole form back in order", () => {
255
+ const back = useFormFromSchema(fieldsToJsonSchema(roundTrips));
256
+ expect(back.map(effective)).toEqual(roundTrips.map(effective));
257
+ });
258
+
259
+ it("writes JSON only, without sharing objects with the descriptors", () => {
260
+ const schema = fieldsToJsonSchema(roundTrips);
261
+ expect(JSON.parse(JSON.stringify(schema))).toStrictEqual(schema);
262
+
263
+ const status = roundTrips.find((field) => field.key === "status")!;
264
+ const written = schema.properties!.status!;
265
+ (written["x-mapo-attrs"] as Record<string, unknown>).placeholder = "x";
266
+ expect(attrsOf(status).placeholder).toBe("Choose");
267
+ });
268
+ });
269
+
270
+ describe("fieldsToJsonSchema — properties", () => {
271
+ it("writes one property per field, with the required ones listed", () => {
272
+ const schema = fieldsToJsonSchema([
273
+ { key: "title", type: "text", label: "Title", required: true },
274
+ { key: "body", type: "editor" },
275
+ ]);
276
+ expect(schema).toEqual({
277
+ type: "object",
278
+ properties: {
279
+ title: { title: "Title", type: "string" },
280
+ body: { type: "string", contentMediaType: "text/html" },
281
+ },
282
+ required: ["title"],
283
+ });
284
+ });
285
+
286
+ it("keeps a dotted key as one property and flags translatable fields", () => {
287
+ const schema = fieldsToJsonSchema([
288
+ { key: "seo.title", type: "text", translatable: true },
289
+ ]);
290
+ expect(Object.keys(schema.properties!)).toEqual(["seo.title"]);
291
+ expect(schema.properties!["seo.title"]!.translatable).toBe(true);
292
+ });
293
+
294
+ it("writes readonly and noDraft as readOnly and writeOnly", () => {
295
+ expect(
296
+ propertyOf({ key: "id", type: "number", readonly: true }),
297
+ ).toMatchObject({ readOnly: true });
298
+ expect(
299
+ propertyOf({ key: "password", type: "text", noDraft: true }),
300
+ ).toMatchObject({ writeOnly: true });
301
+ });
302
+
303
+ it("keeps the first field of a key that appears twice", () => {
304
+ const schema = fieldsToJsonSchema([
305
+ { key: "price", type: "number", label: "Price" },
306
+ { key: "price", type: "text", label: "Price as text" },
307
+ ]);
308
+ expect(schema.properties!.price).toEqual({
309
+ title: "Price",
310
+ type: "number",
311
+ });
312
+ });
313
+ });
314
+
315
+ describe("fieldsToJsonSchema — values", () => {
316
+ it.each([
317
+ ["text", { type: "string" }],
318
+ ["textarea", { type: "string", format: "textarea" }],
319
+ ["email", { type: "string", format: "email" }],
320
+ ["url", { type: "string", format: "uri" }],
321
+ ["number", { type: "number" }],
322
+ ["boolean", { type: "boolean" }],
323
+ ["color", { type: "string", format: "color" }],
324
+ ["file", { type: "string", format: "binary" }],
325
+ ["date", { type: "string", format: "date" }],
326
+ ["time", { type: "string", format: "time" }],
327
+ ["datetime", { type: "string", format: "date-time" }],
328
+ ["editor", { type: "string", contentMediaType: "text/html" }],
329
+ ])("writes %s with standard keywords", (type, expected) => {
330
+ expect(propertyOf({ key: "value", type })).toEqual(expected);
331
+ });
332
+
333
+ it("writes the limits a keyword expresses as that keyword", () => {
334
+ expect(
335
+ propertyOf({ key: "price", type: "number", attrs: { min: 0, max: 10 } }),
336
+ ).toEqual({ type: "number", minimum: 0, maximum: 10 });
337
+ expect(
338
+ propertyOf({ key: "title", type: "text", attrs: { maxLength: 80 } }),
339
+ ).toEqual({ type: "string", maxLength: 80 });
340
+ });
341
+
342
+ it("writes choices as an enum, or oneOf when they have labels", () => {
343
+ expect(
344
+ propertyOf({ key: "size", type: "select", attrs: { items: ["S", "M"] } }),
345
+ ).toEqual({ type: "string", enum: ["S", "M"] });
346
+ expect(
347
+ propertyOf({
348
+ key: "status",
349
+ type: "select",
350
+ attrs: { options: [{ text: "Draft", value: "draft" }] },
351
+ }),
352
+ ).toEqual({
353
+ type: "string",
354
+ oneOf: [{ const: "draft", title: "Draft" }],
355
+ });
356
+ });
357
+
358
+ it("writes a multiple select as an array of choices", () => {
359
+ expect(
360
+ propertyOf({
361
+ key: "sizes",
362
+ type: "select",
363
+ attrs: { multiple: true, items: ["S", "M"] },
364
+ }),
365
+ ).toEqual({
366
+ type: "array",
367
+ items: { type: "string", enum: ["S", "M"] },
368
+ uniqueItems: true,
369
+ });
370
+ });
371
+
372
+ it("writes relations in the relation dialect", () => {
373
+ expect(
374
+ propertyOf({
375
+ key: "tags",
376
+ type: "m2m",
377
+ attrs: { endpoint: "/api/tags/" },
378
+ }),
379
+ ).toEqual({ type: "relation", endpoint: "/api/tags/", multiple: true });
380
+ });
381
+
382
+ it("writes repeater items as an object, and templates as oneOf on _template", () => {
383
+ expect(
384
+ propertyOf({
385
+ key: "faq",
386
+ type: "repeater",
387
+ fields: [{ key: "question", type: "text", required: true }],
388
+ }),
389
+ ).toEqual({
390
+ type: "array",
391
+ items: {
392
+ type: "object",
393
+ properties: { question: { type: "string" } },
394
+ required: ["question"],
395
+ },
396
+ });
397
+
398
+ const blocks = propertyOf({
399
+ key: "blocks",
400
+ type: "repeater",
401
+ attrs: {
402
+ templates: {
403
+ quote: { label: "Quote", fields: [{ key: "text", type: "text" }] },
404
+ },
405
+ },
406
+ });
407
+ expect(blocks).toEqual({
408
+ type: "array",
409
+ items: {
410
+ oneOf: [
411
+ {
412
+ type: "object",
413
+ title: "Quote",
414
+ properties: {
415
+ _template: { const: "quote" },
416
+ text: { type: "string" },
417
+ },
418
+ required: ["_template"],
419
+ },
420
+ ],
421
+ discriminator: { propertyName: "_template" },
422
+ },
423
+ });
424
+ });
425
+ });
426
+
427
+ describe("fieldsToJsonSchema — Mapo keywords", () => {
428
+ it("names the type only when the other keywords read back as another", () => {
429
+ expect(propertyOf({ key: "a", type: "text" })["x-mapo-field"]).toBe(
430
+ undefined,
431
+ );
432
+ expect(propertyOf({ key: "a", type: "switch" })["x-mapo-field"]).toBe(
433
+ "switch",
434
+ );
435
+ // A long text would read back as a textarea.
436
+ expect(
437
+ propertyOf({ key: "a", type: "text", attrs: { maxLength: 300 } })[
438
+ "x-mapo-field"
439
+ ],
440
+ ).toBe("text");
441
+ // A custom type: Mapo does not know its value.
442
+ expect(propertyOf({ key: "a", type: "rating" })).toEqual({
443
+ "x-mapo-field": "rating",
444
+ });
445
+ });
446
+
447
+ it("keeps the attrs no keyword expresses in x-mapo-attrs", () => {
448
+ expect(
449
+ propertyOf({
450
+ key: "author",
451
+ type: "fks",
452
+ attrs: { endpoint: "/api/users/", itemText: "username" },
453
+ }),
454
+ ).toEqual({
455
+ type: "relation",
456
+ endpoint: "/api/users/",
457
+ "x-mapo-attrs": { itemText: "username" },
458
+ });
459
+ });
460
+
461
+ it("leaves out functions, class instances and undefined values, one attr at a time", () => {
462
+ class Extension {}
463
+ const property = propertyOf({
464
+ key: "body",
465
+ type: "repeater",
466
+ fields: [],
467
+ attrs: {
468
+ previewLabel: (item: unknown) => String(item),
469
+ confirmDelete: true,
470
+ defaultExpanded: undefined,
471
+ custom: { size: 2, skipped: undefined },
472
+ extensions: [new Extension()],
473
+ },
474
+ });
475
+ expect(property["x-mapo-attrs"]).toEqual({
476
+ confirmDelete: true,
477
+ custom: { size: 2 },
478
+ });
479
+ });
480
+
481
+ it("does not write layout, functions or components", () => {
482
+ const property = propertyOf({
483
+ key: "title",
484
+ type: "text",
485
+ tab: "Content",
486
+ group: "Main",
487
+ cols: 6,
488
+ class: "font-bold",
489
+ expandable: true,
490
+ visible: () => true,
491
+ validate: () => null,
492
+ is: { name: "Custom" },
493
+ });
494
+ expect(property).toEqual({ type: "string" });
495
+ });
496
+ });
497
+
498
+ describe("fieldsToJsonSchema — the property option", () => {
499
+ it("lets the caller change the schema of every field, nested ones included", () => {
500
+ const seen: string[] = [];
501
+ const schema = fieldsToJsonSchema(
502
+ [
503
+ { key: "title", type: "text" },
504
+ {
505
+ key: "blocks",
506
+ type: "repeater",
507
+ fields: [{ key: "caption", type: "text" }],
508
+ },
509
+ ],
510
+ {
511
+ property: (written, field) => {
512
+ seen.push(String(field.key));
513
+ return { ...written, description: `About ${String(field.key)}` };
514
+ },
515
+ },
516
+ );
517
+ expect(seen).toEqual(["title", "caption", "blocks"]);
518
+ expect(schema.properties!.title!.description).toBe("About title");
519
+ expect(
520
+ schema.properties!.blocks!.items!.properties!.caption!.description,
521
+ ).toBe("About caption");
522
+ });
523
+
524
+ it("leaves out a field when it returns undefined", () => {
525
+ const schema = fieldsToJsonSchema(
526
+ [
527
+ { key: "title", type: "text", required: true },
528
+ { key: "password", type: "text", required: true, noDraft: true },
529
+ ],
530
+ {
531
+ property: (written, field) => (field.noDraft ? undefined : written),
532
+ },
533
+ );
534
+ expect(Object.keys(schema.properties!)).toEqual(["title"]);
535
+ expect(schema.required).toEqual(["title"]);
536
+ });
537
+ });
538
+
539
+ describe("fieldsToJsonSchema — inputs that could break it", () => {
540
+ it("writes a repeater that contains itself once", () => {
541
+ const section: AnyFieldDescriptor & { fields: AnyFieldDescriptor[] } = {
542
+ key: "blocks",
543
+ type: "repeater",
544
+ fields: [{ key: "title", type: "text" }],
545
+ };
546
+ section.fields.push(section);
547
+ const schema = propertyOf(section);
548
+ expect(schema.items!.properties!.blocks).toEqual({
549
+ type: "array",
550
+ items: { type: "object", properties: {} },
551
+ });
552
+ });
553
+
554
+ it("leaves out an attr that contains itself", () => {
555
+ const loop: Record<string, unknown> = { size: 2 };
556
+ loop.self = loop;
557
+ const property = propertyOf({
558
+ key: "mood",
559
+ type: "rating",
560
+ attrs: { loop, stars: 5 },
561
+ });
562
+ expect(property["x-mapo-attrs"]).toStrictEqual({ stars: 5 });
563
+ });
564
+
565
+ it("leaves out only the choice whose value JSON cannot hold", () => {
566
+ const property = propertyOf({
567
+ key: "size",
568
+ type: "select",
569
+ attrs: { items: [{ label: "Small", value: "s" }, { label: "None" }] },
570
+ });
571
+ expect(property).toEqual({
572
+ type: "string",
573
+ oneOf: [{ const: "s", title: "Small" }],
574
+ });
575
+ });
576
+
577
+ it("labels a choice as the select does: text before label", () => {
578
+ const property = propertyOf({
579
+ key: "size",
580
+ type: "select",
581
+ attrs: { options: [{ text: "Shown", label: "Hidden", value: "s" }] },
582
+ });
583
+ expect(property.oneOf).toEqual([{ const: "s", title: "Shown" }]);
584
+ });
585
+
586
+ it("does not write attr names that could run script", () => {
587
+ const property = propertyOf({
588
+ key: "title",
589
+ type: "text",
590
+ attrs: { placeholder: "Title", onfocus: "alert(1)", innerHTML: "<b>" },
591
+ });
592
+ expect(property["x-mapo-attrs"]).toStrictEqual({ placeholder: "Title" });
593
+ });
594
+ });
595
+
596
+ describe("fieldsToJsonSchema — the property option and the type", () => {
597
+ it("decides x-mapo-field on the schema the option returns", () => {
598
+ const schema = fieldsToJsonSchema([{ key: "title", type: "text" }], {
599
+ property: (written) => ({ ...written, maxLength: 1000 }),
600
+ });
601
+ expect(schema.properties!.title).toEqual({
602
+ type: "string",
603
+ maxLength: 1000,
604
+ "x-mapo-field": "text",
605
+ });
606
+ const [back] = useFormFromSchema(schema);
607
+ expect(back?.type).toBe("text");
608
+ });
609
+
610
+ it("keeps a type the option names itself", () => {
611
+ const schema = fieldsToJsonSchema([{ key: "title", type: "text" }], {
612
+ property: (written) => ({ ...written, "x-mapo-field": "slug" }),
613
+ });
614
+ expect(schema.properties!.title!["x-mapo-field"]).toBe("slug");
615
+ });
616
+
617
+ it("writes the next field with a key when the option leaves out the first", () => {
618
+ const schema = fieldsToJsonSchema(
619
+ [
620
+ { key: "price", type: "number", noDraft: true },
621
+ { key: "price", type: "text", label: "Price as text" },
622
+ ],
623
+ { property: (written, field) => (field.noDraft ? undefined : written) },
624
+ );
625
+ expect(schema.properties!.price).toEqual({
626
+ title: "Price as text",
627
+ type: "string",
628
+ });
629
+ });
630
+ });