@formspec/build 0.1.0-alpha.10 → 0.1.0-alpha.11

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 (107) hide show
  1. package/dist/browser.cjs +549 -0
  2. package/dist/browser.cjs.map +1 -0
  3. package/dist/browser.d.ts +4 -2
  4. package/dist/browser.d.ts.map +1 -1
  5. package/dist/browser.js +498 -45
  6. package/dist/browser.js.map +1 -1
  7. package/dist/build.d.ts +343 -33
  8. package/dist/cli.cjs +2267 -0
  9. package/dist/cli.cjs.map +1 -0
  10. package/dist/cli.js +2204 -101
  11. package/dist/cli.js.map +1 -1
  12. package/dist/generators/class-schema.d.ts +5 -9
  13. package/dist/generators/class-schema.d.ts.map +1 -1
  14. package/dist/index.cjs +2093 -0
  15. package/dist/index.cjs.map +1 -0
  16. package/dist/index.d.ts +4 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +2024 -114
  19. package/dist/index.js.map +1 -1
  20. package/dist/internals.cjs +1345 -0
  21. package/dist/internals.cjs.map +1 -0
  22. package/dist/internals.d.ts +1 -0
  23. package/dist/internals.d.ts.map +1 -1
  24. package/dist/internals.js +1297 -21
  25. package/dist/internals.js.map +1 -1
  26. package/dist/json-schema/generator.d.ts.map +1 -1
  27. package/dist/json-schema/schema.d.ts +16 -0
  28. package/dist/json-schema/schema.d.ts.map +1 -0
  29. package/dist/ui-schema/generator.d.ts +15 -0
  30. package/dist/ui-schema/generator.d.ts.map +1 -1
  31. package/dist/ui-schema/schema.d.ts +357 -0
  32. package/dist/ui-schema/schema.d.ts.map +1 -0
  33. package/dist/ui-schema/types.d.ts +8 -73
  34. package/dist/ui-schema/types.d.ts.map +1 -1
  35. package/package.json +14 -9
  36. package/dist/__tests__/analyzer-edge-cases.test.js +0 -376
  37. package/dist/__tests__/analyzer-edge-cases.test.js.map +0 -1
  38. package/dist/__tests__/analyzer.test.js +0 -190
  39. package/dist/__tests__/analyzer.test.js.map +0 -1
  40. package/dist/__tests__/cli.test.js +0 -178
  41. package/dist/__tests__/cli.test.js.map +0 -1
  42. package/dist/__tests__/codegen.test.js +0 -506
  43. package/dist/__tests__/codegen.test.js.map +0 -1
  44. package/dist/__tests__/decorator-pipeline.test.js +0 -460
  45. package/dist/__tests__/decorator-pipeline.test.js.map +0 -1
  46. package/dist/__tests__/edge-cases.test.js +0 -215
  47. package/dist/__tests__/edge-cases.test.js.map +0 -1
  48. package/dist/__tests__/fixtures/edge-cases.js +0 -137
  49. package/dist/__tests__/fixtures/edge-cases.js.map +0 -1
  50. package/dist/__tests__/fixtures/example-a-builtins.js +0 -100
  51. package/dist/__tests__/fixtures/example-a-builtins.js.map +0 -1
  52. package/dist/__tests__/fixtures/example-b-decorators.js +0 -5
  53. package/dist/__tests__/fixtures/example-b-decorators.js.map +0 -1
  54. package/dist/__tests__/fixtures/example-b-extended.js +0 -60
  55. package/dist/__tests__/fixtures/example-b-extended.js.map +0 -1
  56. package/dist/__tests__/fixtures/example-c-custom.js +0 -61
  57. package/dist/__tests__/fixtures/example-c-custom.js.map +0 -1
  58. package/dist/__tests__/fixtures/example-c-decorators.js +0 -4
  59. package/dist/__tests__/fixtures/example-c-decorators.js.map +0 -1
  60. package/dist/__tests__/fixtures/example-d-mixed-decorators.js +0 -75
  61. package/dist/__tests__/fixtures/example-d-mixed-decorators.js.map +0 -1
  62. package/dist/__tests__/fixtures/example-e-decorators.js +0 -10
  63. package/dist/__tests__/fixtures/example-e-decorators.js.map +0 -1
  64. package/dist/__tests__/fixtures/example-e-no-namespace.js +0 -61
  65. package/dist/__tests__/fixtures/example-e-no-namespace.js.map +0 -1
  66. package/dist/__tests__/fixtures/example-interface-types.js +0 -8
  67. package/dist/__tests__/fixtures/example-interface-types.js.map +0 -1
  68. package/dist/__tests__/fixtures/example-jsdoc-constraints.js +0 -98
  69. package/dist/__tests__/fixtures/example-jsdoc-constraints.js.map +0 -1
  70. package/dist/__tests__/fixtures/example-nested-class.js +0 -248
  71. package/dist/__tests__/fixtures/example-nested-class.js.map +0 -1
  72. package/dist/__tests__/fixtures/sample-forms.js +0 -78
  73. package/dist/__tests__/fixtures/sample-forms.js.map +0 -1
  74. package/dist/__tests__/generator.test.js +0 -234
  75. package/dist/__tests__/generator.test.js.map +0 -1
  76. package/dist/__tests__/integration.test.js +0 -161
  77. package/dist/__tests__/integration.test.js.map +0 -1
  78. package/dist/__tests__/interface-types.test.js +0 -404
  79. package/dist/__tests__/interface-types.test.js.map +0 -1
  80. package/dist/__tests__/jsdoc-constraints.test.js +0 -465
  81. package/dist/__tests__/jsdoc-constraints.test.js.map +0 -1
  82. package/dist/__tests__/write-schemas.test.js +0 -198
  83. package/dist/__tests__/write-schemas.test.js.map +0 -1
  84. package/dist/analyzer/class-analyzer.js +0 -377
  85. package/dist/analyzer/class-analyzer.js.map +0 -1
  86. package/dist/analyzer/decorator-extractor.js +0 -336
  87. package/dist/analyzer/decorator-extractor.js.map +0 -1
  88. package/dist/analyzer/jsdoc-constraints.js +0 -153
  89. package/dist/analyzer/jsdoc-constraints.js.map +0 -1
  90. package/dist/analyzer/program.js +0 -114
  91. package/dist/analyzer/program.js.map +0 -1
  92. package/dist/analyzer/type-converter.js +0 -474
  93. package/dist/analyzer/type-converter.js.map +0 -1
  94. package/dist/codegen/index.js +0 -597
  95. package/dist/codegen/index.js.map +0 -1
  96. package/dist/generators/class-schema.js +0 -140
  97. package/dist/generators/class-schema.js.map +0 -1
  98. package/dist/generators/method-schema.js +0 -108
  99. package/dist/generators/method-schema.js.map +0 -1
  100. package/dist/json-schema/generator.js +0 -166
  101. package/dist/json-schema/generator.js.map +0 -1
  102. package/dist/json-schema/types.js +0 -33
  103. package/dist/json-schema/types.js.map +0 -1
  104. package/dist/ui-schema/generator.js +0 -148
  105. package/dist/ui-schema/generator.js.map +0 -1
  106. package/dist/ui-schema/types.js +0 -8
  107. package/dist/ui-schema/types.js.map +0 -1
package/dist/browser.d.ts CHANGED
@@ -20,9 +20,11 @@ import { generateJsonSchema } from "./json-schema/generator.js";
20
20
  import { generateUiSchema } from "./ui-schema/generator.js";
21
21
  export type { JSONSchema7, JSONSchemaType, ExtendedJSONSchema7, FormSpecSchemaExtensions, } from "./json-schema/types.js";
22
22
  export { setSchemaExtension, getSchemaExtension } from "./json-schema/types.js";
23
- export type { UISchema, UISchemaElement, UISchemaElementBase, UISchemaElementType, ControlElement, VerticalLayout, HorizontalLayout, GroupLayout, Rule, RuleEffect, RuleConditionSchema, SchemaBasedCondition, } from "./ui-schema/types.js";
23
+ export type { UISchema, UISchemaElement, UISchemaElementBase, UISchemaElementType, ControlElement, VerticalLayout, HorizontalLayout, GroupLayout, Categorization, Category, LabelElement, Rule, RuleEffect, RuleConditionSchema, SchemaBasedCondition, } from "./ui-schema/types.js";
24
+ export { ruleEffectSchema, uiSchemaElementTypeSchema, ruleConditionSchema, schemaBasedConditionSchema, ruleSchema, controlSchema, verticalLayoutSchema, horizontalLayoutSchema, groupLayoutSchema, categorizationSchema, categorySchema, labelElementSchema, uiSchemaElementSchema, uiSchema as uiSchemaSchema, } from "./ui-schema/schema.js";
25
+ export { jsonSchemaTypeSchema, jsonSchema7Schema } from "./json-schema/schema.js";
24
26
  export { generateJsonSchema } from "./json-schema/generator.js";
25
- export { generateUiSchema } from "./ui-schema/generator.js";
27
+ export { generateUiSchema, generateUiSchemaFromFields } from "./ui-schema/generator.js";
26
28
  /**
27
29
  * Result of building form schemas.
28
30
  */
@@ -1 +1 @@
1
- {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAG5D,YAAY,EACV,WAAW,EACX,cAAc,EACd,mBAAmB,EACnB,wBAAwB,GACzB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAEhF,YAAY,EACV,QAAQ,EACR,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,WAAW,EACX,IAAI,EACJ,UAAU,EACV,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,iCAAiC;IACjC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;IAC3D,yCAAyC;IACzC,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC;CACxD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,SAAS,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,WAAW,CAKjG"}
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAG5D,YAAY,EACV,WAAW,EACX,cAAc,EACd,mBAAmB,EACnB,wBAAwB,GACzB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAEhF,YAAY,EACV,QAAQ,EACR,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,WAAW,EACX,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,IAAI,EACJ,UAAU,EACV,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACL,gBAAgB,EAChB,yBAAyB,EACzB,mBAAmB,EACnB,0BAA0B,EAC1B,UAAU,EACV,aAAa,EACb,oBAAoB,EACpB,sBAAsB,EACtB,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,QAAQ,IAAI,cAAc,GAC3B,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAGlF,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAExF;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,iCAAiC;IACjC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;IAC3D,yCAAyC;IACzC,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC;CACxD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,SAAS,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,WAAW,CAKjG"}
package/dist/browser.js CHANGED
@@ -1,48 +1,501 @@
1
- /**
2
- * Browser-safe exports for `@formspec/build`.
3
- *
4
- * This entry point excludes Node.js-specific functions like `writeSchemas`
5
- * that use `node:fs` and `node:path`, making it suitable for browser environments.
6
- *
7
- * @example
8
- * ```typescript
9
- * // In browser code (e.g., playground)
10
- * import { buildFormSchemas, generateJsonSchema, generateUiSchema } from "@formspec/build/browser";
11
- *
12
- * const form = formspec(field.text("name"));
13
- * const { jsonSchema, uiSchema } = buildFormSchemas(form);
14
- * ```
15
- *
16
- * @packageDocumentation
17
- */
18
- import { generateJsonSchema } from "./json-schema/generator.js";
19
- import { generateUiSchema } from "./ui-schema/generator.js";
20
- export { setSchemaExtension, getSchemaExtension } from "./json-schema/types.js";
21
- // Re-export individual generators
22
- export { generateJsonSchema } from "./json-schema/generator.js";
23
- export { generateUiSchema } from "./ui-schema/generator.js";
24
- /**
25
- * Builds both JSON Schema and UI Schema from a FormSpec.
26
- *
27
- * This is a browser-safe version that does not include file system operations.
28
- *
29
- * @example
30
- * ```typescript
31
- * const form = formspec(
32
- * field.text("name", { required: true }),
33
- * field.number("age", { min: 0 }),
34
- * );
35
- *
36
- * const { jsonSchema, uiSchema } = buildFormSchemas(form);
37
- * ```
38
- *
39
- * @param form - The FormSpec to build schemas from
40
- * @returns Object containing both jsonSchema and uiSchema
41
- */
42
- export function buildFormSchemas(form) {
43
- return {
44
- jsonSchema: generateJsonSchema(form),
45
- uiSchema: generateUiSchema(form),
1
+ // src/json-schema/schema.ts
2
+ import { z } from "zod";
3
+ var jsonSchemaTypeSchema = z.enum([
4
+ "string",
5
+ "number",
6
+ "integer",
7
+ "boolean",
8
+ "object",
9
+ "array",
10
+ "null"
11
+ ]);
12
+ var jsonSchema7Schema = z.lazy(
13
+ () => z.object({
14
+ $schema: z.string().optional(),
15
+ $id: z.string().optional(),
16
+ $ref: z.string().optional(),
17
+ // Metadata
18
+ title: z.string().optional(),
19
+ description: z.string().optional(),
20
+ deprecated: z.boolean().optional(),
21
+ // Type
22
+ type: z.union([jsonSchemaTypeSchema, z.array(jsonSchemaTypeSchema)]).optional(),
23
+ // String validation
24
+ minLength: z.number().optional(),
25
+ maxLength: z.number().optional(),
26
+ pattern: z.string().optional(),
27
+ // Number validation
28
+ minimum: z.number().optional(),
29
+ maximum: z.number().optional(),
30
+ exclusiveMinimum: z.number().optional(),
31
+ exclusiveMaximum: z.number().optional(),
32
+ // Enum
33
+ enum: z.array(z.union([z.string(), z.number(), z.boolean(), z.null()])).readonly().optional(),
34
+ const: z.union([z.string(), z.number(), z.boolean(), z.null()]).optional(),
35
+ // Object
36
+ properties: z.record(z.string(), jsonSchema7Schema).optional(),
37
+ required: z.array(z.string()).optional(),
38
+ additionalProperties: z.union([z.boolean(), jsonSchema7Schema]).optional(),
39
+ // Array
40
+ items: z.union([jsonSchema7Schema, z.array(jsonSchema7Schema)]).optional(),
41
+ minItems: z.number().optional(),
42
+ maxItems: z.number().optional(),
43
+ // Composition
44
+ allOf: z.array(jsonSchema7Schema).optional(),
45
+ anyOf: z.array(jsonSchema7Schema).optional(),
46
+ oneOf: z.array(jsonSchema7Schema).optional(),
47
+ not: jsonSchema7Schema.optional(),
48
+ // Conditional
49
+ if: jsonSchema7Schema.optional(),
50
+ then: jsonSchema7Schema.optional(),
51
+ else: jsonSchema7Schema.optional(),
52
+ // Format
53
+ format: z.string().optional(),
54
+ // Default
55
+ default: z.unknown().optional(),
56
+ // FormSpec extensions
57
+ "x-formspec-source": z.string().optional(),
58
+ "x-formspec-params": z.array(z.string()).readonly().optional(),
59
+ "x-formspec-schemaSource": z.string().optional()
60
+ }).passthrough()
61
+ );
62
+
63
+ // src/json-schema/generator.ts
64
+ import { z as z2 } from "zod";
65
+ function parseOrThrow(schema, value, label) {
66
+ try {
67
+ return schema.parse(value);
68
+ } catch (error) {
69
+ if (error instanceof z2.ZodError) {
70
+ throw new Error(
71
+ `Generated ${label} failed validation:
72
+ ${error.issues.map((i) => ` ${i.path.join(".")}: ${i.message}`).join("\n")}`
73
+ );
74
+ }
75
+ throw error;
76
+ }
77
+ }
78
+ function generateNestedSchema(elements) {
79
+ const properties = {};
80
+ const required = [];
81
+ collectFields(elements, properties, required);
82
+ const uniqueRequired = [...new Set(required)];
83
+ return {
84
+ type: "object",
85
+ properties,
86
+ ...uniqueRequired.length > 0 && { required: uniqueRequired }
87
+ };
88
+ }
89
+ function fieldToJsonSchema(field) {
90
+ const base = {};
91
+ if (field.label !== void 0) {
92
+ base.title = field.label;
93
+ }
94
+ switch (field._field) {
95
+ case "text":
96
+ return { ...base, type: "string" };
97
+ case "number":
98
+ return {
99
+ ...base,
100
+ type: "number",
101
+ ...field.min !== void 0 && { minimum: field.min },
102
+ ...field.max !== void 0 && { maximum: field.max }
103
+ };
104
+ case "boolean":
105
+ return { ...base, type: "boolean" };
106
+ case "enum": {
107
+ const opts = field.options;
108
+ const isObjectOptions = opts.length > 0 && opts.every(
109
+ (opt) => typeof opt === "object" && "id" in opt && "label" in opt
110
+ );
111
+ if (isObjectOptions) {
112
+ return {
113
+ ...base,
114
+ type: "string",
115
+ oneOf: opts.map((o) => ({
116
+ const: o.id,
117
+ title: o.label
118
+ }))
119
+ };
120
+ }
121
+ return { ...base, type: "string", enum: opts };
122
+ }
123
+ case "dynamic_enum":
124
+ return {
125
+ ...base,
126
+ type: "string",
127
+ "x-formspec-source": field.source,
128
+ ...field.params !== void 0 && field.params.length > 0 && { "x-formspec-params": field.params }
129
+ };
130
+ case "dynamic_schema":
131
+ return {
132
+ ...base,
133
+ type: "object",
134
+ additionalProperties: true,
135
+ "x-formspec-schemaSource": field.schemaSource
136
+ };
137
+ case "array": {
138
+ const arrayField = field;
139
+ return {
140
+ ...base,
141
+ type: "array",
142
+ items: generateNestedSchema(arrayField.items),
143
+ ...arrayField.minItems !== void 0 && { minItems: arrayField.minItems },
144
+ ...arrayField.maxItems !== void 0 && { maxItems: arrayField.maxItems }
145
+ };
146
+ }
147
+ case "object": {
148
+ const objectField = field;
149
+ const nestedSchema = generateNestedSchema(objectField.properties);
150
+ return {
151
+ ...base,
152
+ ...nestedSchema
153
+ };
154
+ }
155
+ default: {
156
+ const _exhaustive = field;
157
+ return _exhaustive;
158
+ }
159
+ }
160
+ }
161
+ function collectFields(elements, properties, required) {
162
+ for (const element of elements) {
163
+ switch (element._type) {
164
+ case "field":
165
+ properties[element.name] = fieldToJsonSchema(element);
166
+ if (element.required === true) {
167
+ required.push(element.name);
168
+ }
169
+ break;
170
+ case "group":
171
+ collectFields(element.elements, properties, required);
172
+ break;
173
+ case "conditional":
174
+ collectFields(
175
+ element.elements,
176
+ properties,
177
+ required
178
+ );
179
+ break;
180
+ }
181
+ }
182
+ }
183
+ function generateJsonSchema(form) {
184
+ const properties = {};
185
+ const required = [];
186
+ collectFields(form.elements, properties, required);
187
+ const uniqueRequired = [...new Set(required)];
188
+ const result = {
189
+ $schema: "https://json-schema.org/draft-07/schema#",
190
+ type: "object",
191
+ properties,
192
+ ...uniqueRequired.length > 0 && { required: uniqueRequired }
193
+ };
194
+ return parseOrThrow(jsonSchema7Schema, result, "JSON Schema");
195
+ }
196
+
197
+ // src/ui-schema/schema.ts
198
+ import { z as z3 } from "zod";
199
+ var jsonPointerSchema = z3.string();
200
+ var ruleEffectSchema = z3.enum(["SHOW", "HIDE", "ENABLE", "DISABLE"]);
201
+ var uiSchemaElementTypeSchema = z3.enum([
202
+ "Control",
203
+ "VerticalLayout",
204
+ "HorizontalLayout",
205
+ "Group",
206
+ "Categorization",
207
+ "Category",
208
+ "Label"
209
+ ]);
210
+ var ruleConditionSchema = z3.lazy(
211
+ () => z3.object({
212
+ const: z3.unknown().optional(),
213
+ enum: z3.array(z3.unknown()).readonly().optional(),
214
+ type: z3.string().optional(),
215
+ not: ruleConditionSchema.optional(),
216
+ minimum: z3.number().optional(),
217
+ maximum: z3.number().optional(),
218
+ exclusiveMinimum: z3.number().optional(),
219
+ exclusiveMaximum: z3.number().optional(),
220
+ minLength: z3.number().optional(),
221
+ properties: z3.record(z3.string(), ruleConditionSchema).optional(),
222
+ required: z3.array(z3.string()).optional(),
223
+ allOf: z3.array(ruleConditionSchema).optional()
224
+ }).strict()
225
+ );
226
+ var schemaBasedConditionSchema = z3.object({
227
+ scope: jsonPointerSchema,
228
+ schema: ruleConditionSchema
229
+ }).strict();
230
+ var ruleSchema = z3.object({
231
+ effect: ruleEffectSchema,
232
+ condition: schemaBasedConditionSchema
233
+ }).strict();
234
+ var uiSchemaElementSchema = z3.lazy(
235
+ () => z3.union([
236
+ controlSchema,
237
+ verticalLayoutSchema,
238
+ horizontalLayoutSchema,
239
+ groupLayoutSchema,
240
+ categorizationSchema,
241
+ categorySchema,
242
+ labelElementSchema
243
+ ])
244
+ );
245
+ var controlSchema = z3.object({
246
+ type: z3.literal("Control"),
247
+ scope: jsonPointerSchema,
248
+ label: z3.union([z3.string(), z3.literal(false)]).optional(),
249
+ rule: ruleSchema.optional(),
250
+ options: z3.record(z3.string(), z3.unknown()).optional()
251
+ }).passthrough();
252
+ var verticalLayoutSchema = z3.lazy(
253
+ () => z3.object({
254
+ type: z3.literal("VerticalLayout"),
255
+ elements: z3.array(uiSchemaElementSchema),
256
+ rule: ruleSchema.optional(),
257
+ options: z3.record(z3.string(), z3.unknown()).optional()
258
+ }).passthrough()
259
+ );
260
+ var horizontalLayoutSchema = z3.lazy(
261
+ () => z3.object({
262
+ type: z3.literal("HorizontalLayout"),
263
+ elements: z3.array(uiSchemaElementSchema),
264
+ rule: ruleSchema.optional(),
265
+ options: z3.record(z3.string(), z3.unknown()).optional()
266
+ }).passthrough()
267
+ );
268
+ var groupLayoutSchema = z3.lazy(
269
+ () => z3.object({
270
+ type: z3.literal("Group"),
271
+ label: z3.string(),
272
+ elements: z3.array(uiSchemaElementSchema),
273
+ rule: ruleSchema.optional(),
274
+ options: z3.record(z3.string(), z3.unknown()).optional()
275
+ }).passthrough()
276
+ );
277
+ var categorySchema = z3.lazy(
278
+ () => z3.object({
279
+ type: z3.literal("Category"),
280
+ label: z3.string(),
281
+ elements: z3.array(uiSchemaElementSchema),
282
+ rule: ruleSchema.optional(),
283
+ options: z3.record(z3.string(), z3.unknown()).optional()
284
+ }).passthrough()
285
+ );
286
+ var categorizationSchema = z3.lazy(
287
+ () => z3.object({
288
+ type: z3.literal("Categorization"),
289
+ elements: z3.array(categorySchema),
290
+ label: z3.string().optional(),
291
+ rule: ruleSchema.optional(),
292
+ options: z3.record(z3.string(), z3.unknown()).optional()
293
+ }).passthrough()
294
+ );
295
+ var labelElementSchema = z3.object({
296
+ type: z3.literal("Label"),
297
+ text: z3.string(),
298
+ rule: ruleSchema.optional(),
299
+ options: z3.record(z3.string(), z3.unknown()).optional()
300
+ }).passthrough();
301
+ var uiSchema = z3.lazy(
302
+ () => z3.union([verticalLayoutSchema, horizontalLayoutSchema, groupLayoutSchema, categorizationSchema])
303
+ );
304
+
305
+ // src/ui-schema/generator.ts
306
+ import { z as z4 } from "zod";
307
+ function parseOrThrow2(schema, value, label) {
308
+ try {
309
+ return schema.parse(value);
310
+ } catch (error) {
311
+ if (error instanceof z4.ZodError) {
312
+ throw new Error(
313
+ `Generated ${label} failed validation:
314
+ ${error.issues.map((i) => ` ${i.path.join(".")}: ${i.message}`).join("\n")}`
315
+ );
316
+ }
317
+ throw error;
318
+ }
319
+ }
320
+ function fieldToScope(fieldName) {
321
+ return `#/properties/${fieldName}`;
322
+ }
323
+ function createShowRule(fieldName, value) {
324
+ return {
325
+ effect: "SHOW",
326
+ condition: {
327
+ scope: fieldToScope(fieldName),
328
+ schema: { const: value }
329
+ }
330
+ };
331
+ }
332
+ function combineRules(parentRule, childRule) {
333
+ const parentCondition = parentRule.condition;
334
+ const childCondition = childRule.condition;
335
+ return {
336
+ effect: "SHOW",
337
+ condition: {
338
+ scope: "#",
339
+ schema: {
340
+ allOf: [
341
+ {
342
+ properties: {
343
+ [parentCondition.scope.replace("#/properties/", "")]: parentCondition.schema
344
+ }
345
+ },
346
+ {
347
+ properties: {
348
+ [childCondition.scope.replace("#/properties/", "")]: childCondition.schema
349
+ }
350
+ }
351
+ ]
352
+ }
353
+ }
354
+ };
355
+ }
356
+ function elementsToUiSchema(elements, parentRule) {
357
+ const result = [];
358
+ for (const element of elements) {
359
+ switch (element._type) {
360
+ case "field": {
361
+ const control = {
362
+ type: "Control",
363
+ scope: fieldToScope(element.name),
364
+ ...element.label !== void 0 && { label: element.label },
365
+ ...parentRule !== void 0 && { rule: parentRule }
366
+ };
367
+ result.push(control);
368
+ break;
369
+ }
370
+ case "group": {
371
+ const groupElement = element;
372
+ const group = {
373
+ type: "Group",
374
+ label: groupElement.label,
375
+ elements: elementsToUiSchema(groupElement.elements, parentRule),
376
+ ...parentRule !== void 0 && { rule: parentRule }
377
+ };
378
+ result.push(group);
379
+ break;
380
+ }
381
+ case "conditional": {
382
+ const conditionalElement = element;
383
+ const newRule = createShowRule(conditionalElement.field, conditionalElement.value);
384
+ const combinedRule = parentRule !== void 0 ? combineRules(parentRule, newRule) : newRule;
385
+ const childElements = elementsToUiSchema(conditionalElement.elements, combinedRule);
386
+ result.push(...childElements);
387
+ break;
388
+ }
389
+ }
390
+ }
391
+ return result;
392
+ }
393
+ function formSpecFieldToElement(field, scopePrefix = "#/properties") {
394
+ const control = {
395
+ type: "Control",
396
+ scope: `${scopePrefix}/${field.id}`
397
+ };
398
+ if (field.label !== void 0) {
399
+ control.label = field.label;
400
+ }
401
+ if (field.showWhen !== void 0 && typeof field.showWhen === "object" && "field" in field.showWhen && "value" in field.showWhen) {
402
+ const sw = field.showWhen;
403
+ control.rule = {
404
+ effect: "SHOW",
405
+ condition: {
406
+ scope: `#/properties/${sw.field}`,
407
+ schema: { const: sw.value }
408
+ }
46
409
  };
410
+ }
411
+ return control;
412
+ }
413
+ function generateUiSchemaFromFields(fields) {
414
+ const groupMap = /* @__PURE__ */ new Map();
415
+ const orderedKeys = [];
416
+ const ungrouped = [];
417
+ for (const field of fields) {
418
+ const element = formSpecFieldToElement(field);
419
+ if (field.group !== void 0) {
420
+ if (!groupMap.has(field.group)) {
421
+ groupMap.set(field.group, []);
422
+ orderedKeys.push(field.group);
423
+ }
424
+ groupMap.get(field.group).push(element);
425
+ } else {
426
+ orderedKeys.push(null);
427
+ ungrouped.push(element);
428
+ }
429
+ }
430
+ const elements = [];
431
+ let ungroupedIndex = 0;
432
+ for (const key of orderedKeys) {
433
+ if (key === null) {
434
+ const el = ungrouped[ungroupedIndex++];
435
+ if (el !== void 0) {
436
+ elements.push(el);
437
+ }
438
+ } else {
439
+ const groupElements = groupMap.get(key) ?? [];
440
+ const groupLayout = {
441
+ type: "Group",
442
+ label: key,
443
+ elements: groupElements
444
+ };
445
+ elements.push(groupLayout);
446
+ }
447
+ }
448
+ const result = {
449
+ type: "VerticalLayout",
450
+ elements
451
+ };
452
+ return parseOrThrow2(uiSchema, result, "UI Schema");
453
+ }
454
+ function generateUiSchema(form) {
455
+ const result = {
456
+ type: "VerticalLayout",
457
+ elements: elementsToUiSchema(form.elements)
458
+ };
459
+ return parseOrThrow2(uiSchema, result, "UI Schema");
460
+ }
461
+
462
+ // src/json-schema/types.ts
463
+ function setSchemaExtension(schema, key, value) {
464
+ schema[key] = value;
465
+ }
466
+ function getSchemaExtension(schema, key) {
467
+ return schema[key];
468
+ }
469
+
470
+ // src/browser.ts
471
+ function buildFormSchemas(form) {
472
+ return {
473
+ jsonSchema: generateJsonSchema(form),
474
+ uiSchema: generateUiSchema(form)
475
+ };
47
476
  }
477
+ export {
478
+ buildFormSchemas,
479
+ categorizationSchema,
480
+ categorySchema,
481
+ controlSchema,
482
+ generateJsonSchema,
483
+ generateUiSchema,
484
+ generateUiSchemaFromFields,
485
+ getSchemaExtension,
486
+ groupLayoutSchema,
487
+ horizontalLayoutSchema,
488
+ jsonSchema7Schema,
489
+ jsonSchemaTypeSchema,
490
+ labelElementSchema,
491
+ ruleConditionSchema,
492
+ ruleEffectSchema,
493
+ ruleSchema,
494
+ schemaBasedConditionSchema,
495
+ setSchemaExtension,
496
+ uiSchemaElementSchema,
497
+ uiSchemaElementTypeSchema,
498
+ uiSchema as uiSchemaSchema,
499
+ verticalLayoutSchema
500
+ };
48
501
  //# sourceMappingURL=browser.js.map