-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconst-to-enum.test.ts
More file actions
327 lines (261 loc) · 9.05 KB
/
const-to-enum.test.ts
File metadata and controls
327 lines (261 loc) · 9.05 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
import { JSONSchema4 } from 'json-schema';
import { constToEnum } from '../src/transformers/const-to-enum';
describe('constToEnum', () => {
describe('basic const conversion', () => {
test('should convert string const to single-element enum array', () => {
const schema: JSONSchema4 = {
type: 'string',
const: 'tab',
};
const result = constToEnum(schema);
expect(result.enum).toEqual(['tab']);
expect(result.const).toBeUndefined();
expect(result.type).toBe('string');
});
test('should convert number const to single-element enum array', () => {
const schema: JSONSchema4 = {
type: 'number',
const: 42,
};
const result = constToEnum(schema);
expect(result.enum).toEqual([42]);
expect(result.const).toBeUndefined();
expect(result.type).toBe('number');
});
test('should convert boolean const to single-element enum array', () => {
const schema: JSONSchema4 = {
type: 'boolean',
const: true,
};
const result = constToEnum(schema);
expect(result.enum).toEqual([true]);
expect(result.const).toBeUndefined();
});
test('should convert null const to single-element enum array', () => {
const schema: JSONSchema4 = {
const: null,
};
const result = constToEnum(schema);
expect(result.enum).toEqual([null]);
expect(result.const).toBeUndefined();
});
});
describe('enum precedence over const', () => {
test('should preserve existing enum and remove const when both present', () => {
const schema: JSONSchema4 = {
type: 'string',
const: 'ignored',
enum: ['a', 'b', 'c'],
};
const result = constToEnum(schema);
expect(result.enum).toEqual(['a', 'b', 'c']);
expect(result.const).toBeUndefined();
});
});
describe('metadata preservation', () => {
test('should preserve description when converting const', () => {
const schema: JSONSchema4 = {
type: 'string',
const: 'tab',
description: 'Tab indent style',
};
const result = constToEnum(schema);
expect(result.enum).toEqual(['tab']);
expect(result.description).toBe('Tab indent style');
expect(result.type).toBe('string');
});
test('should preserve all other properties when converting const', () => {
const schema: JSONSchema4 = {
type: 'string',
const: 'value',
title: 'My Title',
description: 'My Description',
default: 'value',
};
const result = constToEnum(schema);
expect(result.enum).toEqual(['value']);
expect(result.title).toBe('My Title');
expect(result.description).toBe('My Description');
expect(result.default).toBe('value');
expect(result.const).toBeUndefined();
});
});
describe('nested schema transformation', () => {
test('should transform const in nested properties', () => {
const schema: JSONSchema4 = {
type: 'object',
properties: {
indent: {
type: 'string',
const: 'tab',
},
size: {
type: 'number',
const: 4,
},
},
};
const result = constToEnum(schema);
expect(result.properties?.indent.enum).toEqual(['tab']);
expect(result.properties?.indent.const).toBeUndefined();
expect(result.properties?.size.enum).toEqual([4]);
expect(result.properties?.size.const).toBeUndefined();
});
test('should transform const in deeply nested properties', () => {
const schema: JSONSchema4 = {
type: 'object',
properties: {
config: {
type: 'object',
properties: {
style: {
type: 'string',
const: 'compact',
},
},
},
},
};
const result = constToEnum(schema);
expect(result.properties?.config.properties?.style.enum).toEqual(['compact']);
expect(result.properties?.config.properties?.style.const).toBeUndefined();
});
});
describe('oneOf/anyOf with const values', () => {
test('should transform const values in oneOf members', () => {
const schema: JSONSchema4 = {
oneOf: [
{ type: 'string', const: 'tab' },
{ type: 'string', const: 'space' },
],
};
const result = constToEnum(schema);
expect(result.oneOf?.[0].enum).toEqual(['tab']);
expect(result.oneOf?.[0].const).toBeUndefined();
expect(result.oneOf?.[1].enum).toEqual(['space']);
expect(result.oneOf?.[1].const).toBeUndefined();
});
test('should transform const values in anyOf members', () => {
const schema: JSONSchema4 = {
anyOf: [
{ type: 'number', const: 2 },
{ type: 'number', const: 4 },
],
};
const result = constToEnum(schema);
expect(result.anyOf?.[0].enum).toEqual([2]);
expect(result.anyOf?.[0].const).toBeUndefined();
expect(result.anyOf?.[1].enum).toEqual([4]);
expect(result.anyOf?.[1].const).toBeUndefined();
});
test('should transform const values in allOf members', () => {
const schema: JSONSchema4 = {
allOf: [
{ type: 'string', const: 'fixed' },
],
};
const result = constToEnum(schema);
expect(result.allOf?.[0].enum).toEqual(['fixed']);
expect(result.allOf?.[0].const).toBeUndefined();
});
test('should transform const in nested oneOf within properties', () => {
const schema: JSONSchema4 = {
type: 'object',
properties: {
indentStyle: {
oneOf: [
{ type: 'string', const: 'tab', description: 'Use tabs' },
{ type: 'string', const: 'space', description: 'Use spaces' },
],
},
},
};
const result = constToEnum(schema);
expect(result.properties?.indentStyle.oneOf?.[0].enum).toEqual(['tab']);
expect(result.properties?.indentStyle.oneOf?.[0].description).toBe('Use tabs');
expect(result.properties?.indentStyle.oneOf?.[1].enum).toEqual(['space']);
expect(result.properties?.indentStyle.oneOf?.[1].description).toBe('Use spaces');
});
});
describe('edge cases', () => {
test('should not transform object const (not a valid enum value)', () => {
const schema: JSONSchema4 = {
const: { key: 'value' },
};
const result = constToEnum(schema);
expect(result.const).toEqual({ key: 'value' });
expect(result.enum).toBeUndefined();
});
test('should not transform array const (not a valid enum value)', () => {
const schema: JSONSchema4 = {
const: ['a', 'b'],
};
const result = constToEnum(schema);
expect(result.const).toEqual(['a', 'b']);
expect(result.enum).toBeUndefined();
});
test('should handle schema without const property', () => {
const schema: JSONSchema4 = {
type: 'string',
description: 'A simple string',
};
const result = constToEnum(schema);
expect(result.type).toBe('string');
expect(result.description).toBe('A simple string');
expect(result.enum).toBeUndefined();
expect(result.const).toBeUndefined();
});
test('should handle empty schema', () => {
const schema: JSONSchema4 = {};
const result = constToEnum(schema);
expect(result).toEqual({});
});
test('should transform const in array items', () => {
const schema: JSONSchema4 = {
type: 'array',
items: {
type: 'string',
const: 'fixed',
},
};
const result = constToEnum(schema);
expect((result.items as JSONSchema4).enum).toEqual(['fixed']);
expect((result.items as JSONSchema4).const).toBeUndefined();
});
test('should transform const in additionalProperties', () => {
const schema: JSONSchema4 = {
type: 'object',
additionalProperties: {
type: 'string',
const: 'value',
},
};
const result = constToEnum(schema);
expect((result.additionalProperties as JSONSchema4).enum).toEqual(['value']);
expect((result.additionalProperties as JSONSchema4).const).toBeUndefined();
});
test('should handle mixed const and enum in oneOf', () => {
const schema: JSONSchema4 = {
oneOf: [
{ type: 'string', const: 'a' },
{ type: 'string', enum: ['b', 'c'] },
],
};
const result = constToEnum(schema);
expect(result.oneOf?.[0].enum).toEqual(['a']);
expect(result.oneOf?.[0].const).toBeUndefined();
expect(result.oneOf?.[1].enum).toEqual(['b', 'c']);
});
test('should handle required property with boolean true schema', () => {
const schema: JSONSchema4 = {
required: ['field'],
properties: {
field: true as any,
},
};
const result = constToEnum(schema);
expect(result.required).toEqual(['field']);
expect(result.properties?.field).toBe(true);
});
});
});