-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathudttype.spec.ts
More file actions
329 lines (285 loc) · 11.9 KB
/
udttype.spec.ts
File metadata and controls
329 lines (285 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { describe, expect, it } from '@jest/globals';
import { type FormanSchemaField, toFormanSchema, toJSONSchema, validateForman } from '../../src';
import type { JSONSchema7 } from 'json-schema';
describe('udttype composite', () => {
describe('Forman -> JSON Schema', () => {
it('should convert udttype to select with x-composite marker', () => {
const field: FormanSchemaField = {
name: 'wrapper',
type: 'collection',
spec: [
{
name: 'type',
type: 'udttype',
label: 'Type',
default: 'text',
},
],
};
const result = toJSONSchema(field);
// The type property should be an allOf wrapper with $ref (draft-07 compliant)
expect(result.properties!['type']).toBeDefined();
const typeField = result.properties!['type'] as JSONSchema7;
expect(Object.getOwnPropertyDescriptor(typeField, 'x-composite')?.value).toBe('udttype');
expect(typeField.title).toBe('Type');
expect(typeField.default).toBe('text');
// Should use allOf wrapper with $ref
expect(typeField.allOf).toBeDefined();
expect((typeField.allOf![0] as JSONSchema7).$ref).toBe('#/definitions/udttype');
// The inner fragment in $defs should have type + oneOf but no title
const udttypeDef = result['definitions']!['udttype'] as JSONSchema7;
expect(udttypeDef.type).toBe('string');
expect(udttypeDef.title).toBeUndefined();
expect(udttypeDef.oneOf).toBeDefined();
expect(udttypeDef.oneOf!.length).toBe(8);
expect(udttypeDef.default).toBe('');
const optionValues = udttypeDef.oneOf!.map(o => (o as JSONSchema7).const);
expect(optionValues).toEqual(['', 'array', 'collection', 'date', 'text', 'number', 'boolean', 'buffer']);
});
it('should produce conditional nested fields via allOf on parent', () => {
const field: FormanSchemaField = {
name: 'wrapper',
type: 'collection',
spec: [
{
name: 'type',
type: 'udttype',
label: 'Type',
},
],
};
const result = toJSONSchema(field);
// Parent collection should have allOf with conditional fields
expect(result.allOf).toBeDefined();
expect(result.allOf!.length).toBeGreaterThan(0);
// Check that conditional fields have if/then structure
const conditionals = result.allOf!.filter(item => (item as JSONSchema7).if);
expect(conditionals.length).toBeGreaterThan(0);
// Check text option has conditional nested fields
const textConditional = conditionals.find(item => {
const schema = item as JSONSchema7;
return (
(schema.if as JSONSchema7)?.properties?.type &&
((schema.if as JSONSchema7).properties!['type'] as JSONSchema7).const === 'text'
);
}) as JSONSchema7 | undefined;
expect(textConditional).toBeDefined();
expect(textConditional!.then).toBeDefined();
});
it('should produce $defs on the root when udttype is used', () => {
const field: FormanSchemaField = {
name: 'wrapper',
type: 'collection',
spec: [
{
name: 'type',
type: 'udttype',
label: 'Type',
},
],
};
const result = toJSONSchema(field);
// $defs should contain the udttype definition
expect(result['definitions']).toBeDefined();
expect(result['definitions']!['udttype']).toBeDefined();
expect(Object.getOwnPropertyDescriptor(result['definitions']!['udttype'], 'x-composite')?.value).toBe('udttype');
});
it('should use $ref for recursive udttype references', () => {
const field: FormanSchemaField = {
name: 'wrapper',
type: 'collection',
spec: [
{
name: 'type',
type: 'udttype',
label: 'Type',
},
],
};
const result = toJSONSchema(field);
// The array option's nested spec should reference $ref for the recursive udttype
const arrayConditional = result.allOf?.find(item => {
const schema = item as JSONSchema7;
return (
(schema.if as JSONSchema7)?.properties?.type &&
((schema.if as JSONSchema7).properties!['type'] as JSONSchema7).const === 'array'
);
}) as JSONSchema7 | undefined;
expect(arrayConditional).toBeDefined();
// Somewhere in the nested structure there should be a $ref to udttype
const json = JSON.stringify(arrayConditional);
expect(json).toContain('$ref');
expect(json).toContain('#/definitions/udttype');
});
it('should convert a single udttype field in a minimal collection', () => {
const result = toJSONSchema({
name: 'root',
type: 'collection',
spec: [{ name: 'x', type: 'udttype', label: 'Type' }],
});
const xField = result.properties!['x'] as JSONSchema7;
expect(xField.allOf).toBeDefined();
expect(xField.title).toBe('Type');
expect(result['definitions']).toBeDefined();
expect(result['definitions']!['udttype']).toBeDefined();
});
it('should not emit default when field has no default', () => {
const field: FormanSchemaField = {
name: 'wrapper',
type: 'collection',
spec: [
{
name: 'type',
type: 'udttype',
label: 'Type',
},
],
};
const result = toJSONSchema(field);
const typeField = result.properties!['type'] as JSONSchema7;
expect(typeField).not.toHaveProperty('default');
});
it('should convert schema with udttype field (snapshot)', () => {
const field: FormanSchemaField = {
name: 'wrapper',
type: 'collection',
spec: [
{
name: 'type',
type: 'udttype',
label: 'Type',
default: 'text',
},
],
};
const result = toJSONSchema(field);
expect(result).toMatchSnapshot();
});
});
describe('JSON Schema -> Forman', () => {
it('should collapse x-composite udttype back to udttype field', () => {
const jsonSchema: JSONSchema7 = {
type: 'string',
title: 'Type',
description: 'Select the type',
'x-composite': 'udttype',
} as any;
const result = toFormanSchema(jsonSchema);
expect(result.type).toBe('udttype');
expect(result.label).toBe('Type');
expect(result.help).toBe('Select the type');
});
it('should preserve default value on collapse', () => {
const jsonSchema: JSONSchema7 = {
type: 'string',
title: 'Type',
default: 'text',
'x-composite': 'udttype',
} as any;
const result = toFormanSchema(jsonSchema);
expect(result.type).toBe('udttype');
expect(result.default).toBe('text');
});
});
describe('Validation', () => {
it('should pass for valid udttype value', async () => {
const result = await validateForman({ type: 'text', required: false, multiline: false }, [
{
name: 'type',
type: 'udttype',
label: 'Type',
},
]);
expect(result.errors).toHaveLength(0);
expect(result.valid).toBe(true);
});
it('should fail for invalid udttype value', async () => {
const result = await validateForman({ type: 'invalid' }, [
{
name: 'type',
type: 'udttype',
label: 'Type',
},
]);
expect(result.valid).toBe(false);
expect(result.errors.length).toBeGreaterThan(0);
});
it('should validate required udttype field', async () => {
const result = await validateForman({ type: null }, [
{
name: 'type',
type: 'udttype',
label: 'Type',
required: true,
},
]);
expect(result.valid).toBe(false);
});
});
describe('Round-trip', () => {
it('should round-trip udttype through toJSONSchema and toFormanSchema', () => {
const field: FormanSchemaField = {
name: 'wrapper',
type: 'collection',
spec: [
{
name: 'type',
type: 'udttype',
label: 'Type',
help: 'Pick a type',
default: 'number',
},
],
};
const jsonSchema = toJSONSchema(field);
const typeField = jsonSchema.properties!['type'] as JSONSchema7;
const formanField = toFormanSchema({ type: 'object', properties: { type: typeField } });
const firstField = (formanField.spec as FormanSchemaField[])[0]!;
expect(firstField.name).toBe('type');
expect(firstField.type).toBe('udttype');
expect(firstField.label).toBe('Type');
expect(firstField.help).toBe('Pick a type');
expect(firstField.default).toBe('number');
});
});
describe('Complex validation', () => {
it('should validate udttype collection with nested spec, sequence, required', async () => {
const result = await validateForman(
{
type: 'collection',
spec: [{ name: 'field1', type: 'text', required: false, multiline: false }],
sequence: true,
required: true,
},
[
{
name: 'type',
type: 'udttype',
label: 'Type',
},
],
);
expect(result.errors).toHaveLength(0);
expect(result.valid).toBe(true);
});
});
describe('Recursion', () => {
it('should not cause infinite expansion during conversion', () => {
const field: FormanSchemaField = {
name: 'wrapper',
type: 'collection',
spec: [
{
name: 'type',
type: 'udttype',
label: 'Type',
},
],
};
// Should complete without hanging
const result = toJSONSchema(field);
expect(result).toBeDefined();
expect(result['definitions']).toBeDefined();
});
});
});