@takeshape/json-schema 11.52.0 → 11.55.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.
- package/dist/__tests__/schema-validator.test.d.ts +1 -0
- package/dist/__tests__/schema-validator.test.js +295 -0
- package/dist/converters/__tests__/schema-converter.test.d.ts +1 -0
- package/dist/converters/__tests__/schema-converter.test.js +1134 -0
- package/dist/converters/__tests__/search-shape-schema.json +495 -0
- package/dist/converters/index.d.ts +1 -2
- package/dist/converters/index.js +1 -16
- package/dist/converters/schema-converter.d.ts +0 -1
- package/dist/converters/schema-converter.js +540 -643
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -16
- package/dist/schema-validator.d.ts +1 -2
- package/dist/schema-validator.js +163 -189
- package/dist/utils/__tests__/references.test.d.ts +1 -0
- package/dist/utils/__tests__/references.test.js +121 -0
- package/dist/utils/__tests__/type-utils.test.d.ts +1 -0
- package/dist/utils/__tests__/type-utils.test.js +143 -0
- package/dist/utils/constants.d.ts +0 -1
- package/dist/utils/constants.js +49 -7
- package/dist/utils/index.d.ts +4 -5
- package/dist/utils/index.js +4 -49
- package/dist/utils/keys.d.ts +0 -1
- package/dist/utils/keys.js +5 -12
- package/dist/utils/references.d.ts +0 -1
- package/dist/utils/references.js +56 -57
- package/dist/utils/type-utils.d.ts +1 -2
- package/dist/utils/type-utils.js +36 -53
- package/dist/utils/types.d.ts +0 -1
- package/dist/utils/types.js +1 -5
- package/package.json +18 -25
- package/dist/converters/index.d.ts.map +0 -1
- package/dist/converters/schema-converter.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/schema-validator.d.ts.map +0 -1
- package/dist/utils/constants.d.ts.map +0 -1
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/keys.d.ts.map +0 -1
- package/dist/utils/references.d.ts.map +0 -1
- package/dist/utils/type-utils.d.ts.map +0 -1
- package/dist/utils/types.d.ts.map +0 -1
- package/es/converters/index.js +0 -1
- package/es/converters/schema-converter.js +0 -654
- package/es/index.js +0 -1
- package/es/schema-validator.js +0 -207
- package/es/utils/constants.js +0 -1
- package/es/utils/index.js +0 -4
- package/es/utils/keys.js +0 -9
- package/es/utils/references.js +0 -66
- package/es/utils/type-utils.js +0 -36
- package/es/utils/types.js +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
|
+
import { createAjv, createSchemaValidator, fixSchema, isInvalidPropertyRequired, validate } from "../schema-validator.js";
|
|
3
|
+
test('createSchemaValidator - missing $id', () => {
|
|
4
|
+
const testSchema = {
|
|
5
|
+
type: 'object',
|
|
6
|
+
properties: {
|
|
7
|
+
title: { type: 'string' }
|
|
8
|
+
},
|
|
9
|
+
required: ['title']
|
|
10
|
+
};
|
|
11
|
+
expect(() => createSchemaValidator(testSchema)).toThrowErrorMatchingInlineSnapshot(`[Error: Failed to create schema validator: schema is missing $id]`);
|
|
12
|
+
});
|
|
13
|
+
test('createSchemaValidator - valid', () => {
|
|
14
|
+
const testSchema = {
|
|
15
|
+
$id: 'test-schema',
|
|
16
|
+
type: 'object',
|
|
17
|
+
properties: {
|
|
18
|
+
title: { type: 'string' }
|
|
19
|
+
},
|
|
20
|
+
required: ['title']
|
|
21
|
+
};
|
|
22
|
+
const validate = createSchemaValidator(testSchema);
|
|
23
|
+
const data = { title: 'this is a title!' };
|
|
24
|
+
const { valid, errors } = validate(data);
|
|
25
|
+
expect(valid).toBe(true);
|
|
26
|
+
expect(errors.length).toBe(0);
|
|
27
|
+
});
|
|
28
|
+
test('createSchemaValidator - ignoreMissing', () => {
|
|
29
|
+
const testSchema = {
|
|
30
|
+
$id: 'test-schema',
|
|
31
|
+
type: 'object',
|
|
32
|
+
properties: {
|
|
33
|
+
obj: { type: 'object' }
|
|
34
|
+
},
|
|
35
|
+
required: ['obj']
|
|
36
|
+
};
|
|
37
|
+
const validate = createSchemaValidator(testSchema);
|
|
38
|
+
const data = {};
|
|
39
|
+
const { valid, errors } = validate(data, { ignoreMissing: true });
|
|
40
|
+
expect(valid).toBe(true);
|
|
41
|
+
expect(errors.length).toBe(0);
|
|
42
|
+
});
|
|
43
|
+
test('createSchemaValidator - ignoreNulls', () => {
|
|
44
|
+
const testSchema = {
|
|
45
|
+
$id: 'test-schema',
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
title: { type: 'string' },
|
|
49
|
+
obj: { type: 'object' }
|
|
50
|
+
},
|
|
51
|
+
required: ['title']
|
|
52
|
+
};
|
|
53
|
+
const validate = createSchemaValidator(testSchema);
|
|
54
|
+
const data = { obj: null, title: 'title', desc: null };
|
|
55
|
+
const { valid, errors } = validate(data, { ignoreNulls: true });
|
|
56
|
+
expect(valid).toBe(true);
|
|
57
|
+
expect(errors.length).toBe(0);
|
|
58
|
+
});
|
|
59
|
+
test('createSchemaValidator - nulls in unions', () => {
|
|
60
|
+
const testSchema = {
|
|
61
|
+
$id: 'test-schema',
|
|
62
|
+
type: 'object',
|
|
63
|
+
properties: {
|
|
64
|
+
title: { type: 'string' },
|
|
65
|
+
shapeArr: {
|
|
66
|
+
type: 'array',
|
|
67
|
+
items: {
|
|
68
|
+
discriminator: {
|
|
69
|
+
propertyName: '_shapeId'
|
|
70
|
+
},
|
|
71
|
+
required: ['_shapeId'],
|
|
72
|
+
oneOf: [
|
|
73
|
+
{
|
|
74
|
+
type: 'object',
|
|
75
|
+
properties: {
|
|
76
|
+
_shapeId: {
|
|
77
|
+
type: 'string',
|
|
78
|
+
enum: ['foo']
|
|
79
|
+
},
|
|
80
|
+
a: {
|
|
81
|
+
type: 'string'
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
type: 'object',
|
|
87
|
+
properties: {
|
|
88
|
+
_shapeId: {
|
|
89
|
+
type: 'string',
|
|
90
|
+
enum: ['bar']
|
|
91
|
+
},
|
|
92
|
+
a: {
|
|
93
|
+
type: 'string'
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
required: ['title']
|
|
102
|
+
};
|
|
103
|
+
const validate = createSchemaValidator(testSchema);
|
|
104
|
+
const data = { title: 'title', shapeArr: [{ _shapeId: 'foo', a: null }] };
|
|
105
|
+
const { valid, errors } = validate(data, { ignoreNulls: true });
|
|
106
|
+
expect(valid).toBe(true);
|
|
107
|
+
expect(errors.length).toBe(0);
|
|
108
|
+
});
|
|
109
|
+
const postSchema = {
|
|
110
|
+
$id: 'post-schema-id',
|
|
111
|
+
type: 'object',
|
|
112
|
+
properties: {
|
|
113
|
+
body: {
|
|
114
|
+
type: 'string',
|
|
115
|
+
minLength: 0,
|
|
116
|
+
description: '',
|
|
117
|
+
title: 'Body'
|
|
118
|
+
},
|
|
119
|
+
title: {
|
|
120
|
+
type: 'string',
|
|
121
|
+
minLength: 1,
|
|
122
|
+
description: '',
|
|
123
|
+
title: 'Title'
|
|
124
|
+
},
|
|
125
|
+
hero: {
|
|
126
|
+
title: 'Hero',
|
|
127
|
+
allOf: [
|
|
128
|
+
{
|
|
129
|
+
type: 'object',
|
|
130
|
+
properties: {
|
|
131
|
+
id: { type: 'string' }
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
{ $ref: '#/definitions/PostHero' }
|
|
135
|
+
]
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
required: ['title', 'hero'],
|
|
139
|
+
definitions: {
|
|
140
|
+
TSRelationship: {
|
|
141
|
+
type: 'object',
|
|
142
|
+
properties: {
|
|
143
|
+
shapeId: {
|
|
144
|
+
type: 'string'
|
|
145
|
+
},
|
|
146
|
+
contentTypeId: {
|
|
147
|
+
type: 'string'
|
|
148
|
+
},
|
|
149
|
+
id: {
|
|
150
|
+
type: 'string'
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
required: ['id']
|
|
154
|
+
},
|
|
155
|
+
PostHero: {
|
|
156
|
+
type: 'object',
|
|
157
|
+
properties: {
|
|
158
|
+
gallery: {
|
|
159
|
+
type: 'array',
|
|
160
|
+
items: {
|
|
161
|
+
properties: {
|
|
162
|
+
image: {
|
|
163
|
+
description: '',
|
|
164
|
+
$ref: '#/definitions/TSRelationship',
|
|
165
|
+
title: 'Image'
|
|
166
|
+
},
|
|
167
|
+
caption: {
|
|
168
|
+
type: 'string',
|
|
169
|
+
minLength: 0,
|
|
170
|
+
description: '',
|
|
171
|
+
title: 'Caption'
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
test('createSchemaValidator - ignoreNulls nested', () => {
|
|
181
|
+
const validate = createSchemaValidator(postSchema);
|
|
182
|
+
const data = {
|
|
183
|
+
title: 'title',
|
|
184
|
+
hero: {
|
|
185
|
+
gallery: [
|
|
186
|
+
{
|
|
187
|
+
caption: 'foo',
|
|
188
|
+
image: {
|
|
189
|
+
shapeId: 'ASSET',
|
|
190
|
+
id: 'asset-id'
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
caption: 'bar',
|
|
195
|
+
image: null
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const invalidResult = validate(data, { ignoreNulls: false });
|
|
201
|
+
expect(invalidResult.valid).toBe(false);
|
|
202
|
+
const validResult = validate(data, { ignoreNulls: true });
|
|
203
|
+
expect(validResult.valid).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
test('isInvalidPropertyRequired - cannot find parent', () => {
|
|
206
|
+
const schema = {
|
|
207
|
+
type: 'string'
|
|
208
|
+
};
|
|
209
|
+
const error = {
|
|
210
|
+
keyword: 'type',
|
|
211
|
+
instancePath: '/hero/gallery/1/image',
|
|
212
|
+
schemaPath: '#/definitions/TSRelationship/type',
|
|
213
|
+
params: {
|
|
214
|
+
type: 'object'
|
|
215
|
+
},
|
|
216
|
+
message: 'should be object'
|
|
217
|
+
};
|
|
218
|
+
expect(() => isInvalidPropertyRequired(schema, error)).toThrowErrorMatchingInlineSnapshot(`[Error: Unexpected error cannot find parent schema]`);
|
|
219
|
+
});
|
|
220
|
+
test('fixSchema - remove invalid regex', () => {
|
|
221
|
+
const schema = {
|
|
222
|
+
type: 'object',
|
|
223
|
+
properties: {
|
|
224
|
+
pattern: { type: 'string' },
|
|
225
|
+
title: { type: 'string', pattern: '[A-Z].+' }, // Valid
|
|
226
|
+
gallery: {
|
|
227
|
+
type: 'array',
|
|
228
|
+
items: {
|
|
229
|
+
type: 'object',
|
|
230
|
+
properties: {
|
|
231
|
+
name: { type: 'string' },
|
|
232
|
+
slug: { type: 'string', pattern: '[\\w-_]+' } // Invalid
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
expect(fixSchema(schema)).toEqual({
|
|
239
|
+
type: 'object',
|
|
240
|
+
properties: {
|
|
241
|
+
pattern: { type: 'string' },
|
|
242
|
+
title: { type: 'string', pattern: '[A-Z].+' }, // Valid pattern kept
|
|
243
|
+
gallery: {
|
|
244
|
+
type: 'array',
|
|
245
|
+
items: {
|
|
246
|
+
type: 'object',
|
|
247
|
+
properties: {
|
|
248
|
+
name: { type: 'string' },
|
|
249
|
+
slug: { type: 'string' } // Invalid pattern removed
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
test('validate - ignoreNulls', () => {
|
|
257
|
+
const ajv = createAjv();
|
|
258
|
+
const testSchema = {
|
|
259
|
+
definitions: {
|
|
260
|
+
foo: {
|
|
261
|
+
type: 'object',
|
|
262
|
+
properties: {
|
|
263
|
+
title: { type: 'string' },
|
|
264
|
+
obj: {
|
|
265
|
+
$ref: '#/definitions/bar'
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
required: ['title']
|
|
269
|
+
},
|
|
270
|
+
bar: {
|
|
271
|
+
type: 'object',
|
|
272
|
+
properties: {
|
|
273
|
+
a: { type: 'string' },
|
|
274
|
+
b: { type: 'string' },
|
|
275
|
+
c: { type: 'string' }
|
|
276
|
+
},
|
|
277
|
+
required: ['a', 'b']
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
ajv.addSchema(testSchema);
|
|
282
|
+
const data = {
|
|
283
|
+
obj: {
|
|
284
|
+
a: 'A',
|
|
285
|
+
b: 'B',
|
|
286
|
+
c: null
|
|
287
|
+
},
|
|
288
|
+
title: 'title'
|
|
289
|
+
};
|
|
290
|
+
const { valid, errors } = validate(ajv, '#/definitions/foo', data, {
|
|
291
|
+
ignoreNulls: true
|
|
292
|
+
});
|
|
293
|
+
expect(valid).toBe(true);
|
|
294
|
+
expect(errors.length).toBe(0);
|
|
295
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|