@takeshape/json-schema 11.72.0 → 11.74.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takeshape/json-schema",
3
- "version": "11.72.0",
3
+ "version": "11.74.1",
4
4
  "description": "JSON Schema validator",
5
5
  "homepage": "https://www.takeshape.io",
6
6
  "repository": {
@@ -38,7 +38,7 @@
38
38
  "ajv-formats": "3.0.1",
39
39
  "lodash": "^4.17.21",
40
40
  "minimatch": "^3.0.4",
41
- "@takeshape/util": "11.72.0"
41
+ "@takeshape/util": "11.74.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/json-schema": "^7.0.7",
@@ -49,7 +49,7 @@
49
49
  "node": ">=20"
50
50
  },
51
51
  "scripts": {
52
- "build": "tsc --build tsconfig.build.json",
52
+ "build": "tsc --project tsconfig.build.json",
53
53
  "prebuild:ci": "pnpm clean",
54
54
  "build:ci": "pnpm build --noCheck",
55
55
  "clean": "del-cli dist coverage *.tsbuildinfo",
@@ -1 +0,0 @@
1
- export {};
@@ -1,323 +0,0 @@
1
- import { expect, test } from 'vitest';
2
- import { createAjv, createSchemaValidator, createTypedValidator, 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
- });
296
- test('createTypedValidator - valid data', () => {
297
- const schema = {
298
- type: 'object',
299
- properties: {
300
- id: { type: 'string' },
301
- name: { type: 'string' }
302
- },
303
- required: ['id', 'name']
304
- };
305
- const validate = createTypedValidator(schema);
306
- const data = { id: '123', name: 'Test' };
307
- expect(validate(data)).toBe(true);
308
- });
309
- test('createTypedValidator - invalid data', () => {
310
- const schema = {
311
- type: 'object',
312
- properties: {
313
- id: { type: 'string' },
314
- name: { type: 'string' }
315
- },
316
- required: ['id', 'name']
317
- };
318
- const validate = createTypedValidator(schema);
319
- const data = { id: '123' }; // Missing 'name'
320
- expect(validate(data)).toBe(false);
321
- expect(validate.errors).toBeDefined();
322
- expect(validate.errors?.[0].message).toBe("must have required property 'name'");
323
- });
@@ -1 +0,0 @@
1
- export {};