@squiz/dx-json-schema-lib 1.79.0 → 1.80.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +6 -0
- package/jest.config.ts +13 -2
- package/lib/JsonSchemaService.d.ts +34 -0
- package/lib/JsonSchemaService.js +140 -0
- package/lib/JsonSchemaService.js.map +1 -0
- package/lib/JsonSchemaService.spec.d.ts +1 -0
- package/lib/JsonSchemaService.spec.js +1807 -0
- package/lib/JsonSchemaService.spec.js.map +1 -0
- package/lib/JsonValidationService.d.ts +11 -33
- package/lib/JsonValidationService.js +43 -194
- package/lib/JsonValidationService.js.map +1 -1
- package/lib/JsonValidationService.spec.js +19 -1800
- package/lib/JsonValidationService.spec.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/jsonTypeResolution/TypeResolver.d.ts +0 -1
- package/lib/jsonTypeResolution/TypeResolver.js +0 -3
- package/lib/jsonTypeResolution/TypeResolver.js.map +1 -1
- package/lib/jsonTypeResolution/TypeResolver.spec.js +10 -0
- package/lib/jsonTypeResolution/TypeResolver.spec.js.map +1 -1
- package/lib/manifest/v1/DxComponentInputSchema.spec.js +18 -0
- package/lib/manifest/v1/DxComponentInputSchema.spec.js.map +1 -1
- package/lib/manifest/v1/DxContentMetaSchema.json +24 -32
- package/lib/manifest/v1/JobV1.d.ts +23 -18
- package/lib/manifest/v1/__test__/schemas/invalidUiMetadata.json +31 -0
- package/lib/manifest/v1/v1.d.ts +23 -18
- package/lib/manifest/v1/v1.spec.js +3 -66
- package/lib/manifest/v1/v1.spec.js.map +1 -1
- package/lib/processValidationResult.d.ts +2 -0
- package/lib/processValidationResult.js +24 -0
- package/lib/processValidationResult.js.map +1 -0
- package/lib/validators/customKeywordValidators.d.ts +2 -0
- package/lib/validators/customKeywordValidators.js +47 -0
- package/lib/validators/customKeywordValidators.js.map +1 -0
- package/lib/validators/customKeywordValidators.spec.d.ts +1 -0
- package/lib/validators/customKeywordValidators.spec.js +100 -0
- package/lib/validators/customKeywordValidators.spec.js.map +1 -0
- package/package.json +1 -1
- package/src/JsonSchemaService.spec.ts +2198 -0
- package/src/JsonSchemaService.ts +159 -0
- package/src/JsonValidationService.spec.ts +26 -2195
- package/src/JsonValidationService.ts +64 -226
- package/src/index.ts +1 -0
- package/src/jsonTypeResolution/TypeResolver.spec.ts +12 -0
- package/src/jsonTypeResolution/TypeResolver.ts +0 -4
- package/src/manifest/v1/DxComponentInputSchema.spec.ts +21 -0
- package/src/manifest/v1/DxContentMetaSchema.json +24 -32
- package/src/manifest/v1/JobV1.ts +23 -23
- package/src/manifest/v1/__test__/schemas/invalidUiMetadata.json +31 -0
- package/src/manifest/v1/v1.spec.ts +3 -73
- package/src/manifest/v1/v1.ts +23 -23
- package/src/processValidationResult.ts +20 -0
- package/src/validators/customKeywordValidators.spec.ts +171 -0
- package/src/validators/customKeywordValidators.ts +53 -0
- package/tsconfig.tsbuildinfo +1 -1
@@ -8,10 +8,7 @@ const SchemaValidationError_1 = require("./errors/SchemaValidationError");
|
|
8
8
|
const validComponent_json_1 = __importDefault(require("./manifest/v1/__test__/schemas/validComponent.json"));
|
9
9
|
const validComponentJson_json_1 = __importDefault(require("./manifest/v1/__test__/schemas/validComponentJson.json"));
|
10
10
|
const inputStringWithFormat_json_1 = __importDefault(require("./manifest/v1/__test__/schemas/inputStringWithFormat.json"));
|
11
|
-
const
|
12
|
-
const primitiveTypes_1 = require("./primitiveTypes");
|
13
|
-
const TypeResolverBuilder_1 = require("./jsonTypeResolution/TypeResolverBuilder");
|
14
|
-
const resolvableTypes_1 = require("./resolvableTypes");
|
11
|
+
const invalidUiMetadata_json_1 = __importDefault(require("./manifest/v1/__test__/schemas/invalidUiMetadata.json"));
|
15
12
|
function expectToThrowErrorMatchingTypeAndMessage(
|
16
13
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
17
14
|
received,
|
@@ -91,6 +88,23 @@ describe('JsonValidationService', () => {
|
|
91
88
|
],
|
92
89
|
});
|
93
90
|
});
|
91
|
+
it('should throw error for invalid ui:metadata options', () => {
|
92
|
+
expectToThrowErrorMatchingTypeAndMessage(() => {
|
93
|
+
jsonValidationService.validateManifest(invalidUiMetadata_json_1.default, 'v1');
|
94
|
+
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: ui:metadata property quickOption is only valid for enum based properties.', {
|
95
|
+
'#/functions/0/input/properties/prop': [
|
96
|
+
{
|
97
|
+
data: {
|
98
|
+
expected: 'enum',
|
99
|
+
pointer: '#/functions/0/input/properties/prop',
|
100
|
+
received: 'object',
|
101
|
+
value: { type: 'string', 'ui:metadata': { quickOption: true } },
|
102
|
+
},
|
103
|
+
message: 'ui:metadata property quickOption is only valid for enum based properties.',
|
104
|
+
},
|
105
|
+
],
|
106
|
+
});
|
107
|
+
});
|
94
108
|
});
|
95
109
|
describe('validateRenderInput', () => {
|
96
110
|
describe('matrix-asset-uri input', () => {
|
@@ -212,21 +226,7 @@ describe('JsonValidationService', () => {
|
|
212
226
|
const value = {
|
213
227
|
'my-input': [{ type: 'text', value: 'hello' }],
|
214
228
|
};
|
215
|
-
|
216
|
-
jsonValidationService.validateRenderInput(schema, value);
|
217
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Expected `Array [{"type":"text","value":"hello"}]` (array) in `#/my-input` to be of type `string`', {
|
218
|
-
'#/my-input': [
|
219
|
-
{
|
220
|
-
message: 'Expected `Array [{"type":"text","value":"hello"}]` (array) in `#/my-input` to be of type `string`',
|
221
|
-
data: {
|
222
|
-
received: 'array',
|
223
|
-
expected: 'string',
|
224
|
-
value: [{ type: 'text', value: 'hello' }],
|
225
|
-
pointer: '#/my-input',
|
226
|
-
},
|
227
|
-
},
|
228
|
-
],
|
229
|
-
});
|
229
|
+
expect(() => jsonValidationService.validateRenderInput(schema, value)).toThrowError(SchemaValidationError_1.SchemaValidationError);
|
230
230
|
});
|
231
231
|
it('should validate a property with type FormattedText within an if/then/else as string', () => {
|
232
232
|
const schema = {
|
@@ -272,1785 +272,4 @@ describe('JsonValidationService', () => {
|
|
272
272
|
});
|
273
273
|
});
|
274
274
|
});
|
275
|
-
const defaultSchema = {
|
276
|
-
type: 'object',
|
277
|
-
properties: {
|
278
|
-
myProperty: {
|
279
|
-
type: 'string',
|
280
|
-
},
|
281
|
-
},
|
282
|
-
required: ['myProperty'],
|
283
|
-
};
|
284
|
-
function primitiveTypeFixture(title, schema = defaultSchema) {
|
285
|
-
return (0, TypeResolver_1.PrimitiveType)({
|
286
|
-
...schema,
|
287
|
-
title,
|
288
|
-
});
|
289
|
-
}
|
290
|
-
function resolvableTypeFixture(title, schema = defaultSchema) {
|
291
|
-
return (0, TypeResolver_1.ResolvableType)({
|
292
|
-
...schema,
|
293
|
-
title,
|
294
|
-
});
|
295
|
-
}
|
296
|
-
describe('JsonSchemaService', () => {
|
297
|
-
let jsonSchemaService;
|
298
|
-
beforeAll(() => {
|
299
|
-
const typeResolver = new TypeResolver_1.TypeResolver([primitiveTypes_1.FormattedTextType]);
|
300
|
-
jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
301
|
-
});
|
302
|
-
describe('validateContentSchema', () => {
|
303
|
-
it('should return true for a valid content schema', () => {
|
304
|
-
const contentSchema = {
|
305
|
-
type: 'object',
|
306
|
-
properties: {
|
307
|
-
'my-input': { type: 'number' },
|
308
|
-
},
|
309
|
-
required: ['my-input'],
|
310
|
-
};
|
311
|
-
const result = jsonSchemaService.validateInput(contentSchema);
|
312
|
-
expect(result).toBe(true);
|
313
|
-
});
|
314
|
-
it('should return false for an invalid content schema, where the required property is missed from a child object', () => {
|
315
|
-
const contentSchema = {
|
316
|
-
type: 'object',
|
317
|
-
properties: {
|
318
|
-
'my-input': {
|
319
|
-
type: 'object',
|
320
|
-
properties: {
|
321
|
-
'my-deep-input': { type: 'object' },
|
322
|
-
},
|
323
|
-
},
|
324
|
-
},
|
325
|
-
required: ['my-input'],
|
326
|
-
};
|
327
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
328
|
-
jsonSchemaService.validateInput(contentSchema);
|
329
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: The required property `required` is missing at `#/properties/my-input`', {
|
330
|
-
'#/properties/my-input': [
|
331
|
-
{
|
332
|
-
data: { key: 'required', pointer: '#/properties/my-input' },
|
333
|
-
message: 'The required property `required` is missing at `#/properties/my-input`',
|
334
|
-
},
|
335
|
-
],
|
336
|
-
});
|
337
|
-
});
|
338
|
-
it('should throw a SchemaValidationError for an invalid content schema, top level type must be object', () => {
|
339
|
-
const contentSchema = {
|
340
|
-
type: 'array',
|
341
|
-
items: {
|
342
|
-
type: 'object',
|
343
|
-
properties: {
|
344
|
-
'my-input': { type: 'number' },
|
345
|
-
},
|
346
|
-
required: ['my-input'],
|
347
|
-
},
|
348
|
-
};
|
349
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
350
|
-
jsonSchemaService.validateInput(contentSchema);
|
351
|
-
}, SchemaValidationError_1.SchemaValidationError, `failed validation: Expected value at \`#/type\` to be \`object\`, but value given is \`array\``, {
|
352
|
-
'#/type': [
|
353
|
-
{
|
354
|
-
data: { expected: 'object', pointer: '#/type', value: 'array' },
|
355
|
-
message: 'Expected value at `#/type` to be `object`, but value given is `array`',
|
356
|
-
},
|
357
|
-
],
|
358
|
-
});
|
359
|
-
});
|
360
|
-
it('should throw a SchemaValidationError for an invalid content schema missing the required property', () => {
|
361
|
-
const contentSchema = {
|
362
|
-
type: 'object',
|
363
|
-
properties: {
|
364
|
-
'my-input': { type: 'number' },
|
365
|
-
},
|
366
|
-
};
|
367
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
368
|
-
jsonSchemaService.validateInput(contentSchema);
|
369
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: The required property `required` is missing at `#`', {
|
370
|
-
'#': [
|
371
|
-
{ data: { key: 'required', pointer: '#' }, message: 'The required property `required` is missing at `#`' },
|
372
|
-
],
|
373
|
-
});
|
374
|
-
});
|
375
|
-
});
|
376
|
-
describe('SquizImage input', () => {
|
377
|
-
it('should validate a squizImage input', () => {
|
378
|
-
const schema = {
|
379
|
-
type: 'object',
|
380
|
-
properties: {
|
381
|
-
image: {
|
382
|
-
type: 'SquizImage',
|
383
|
-
},
|
384
|
-
},
|
385
|
-
};
|
386
|
-
const value = {
|
387
|
-
name: 'My Image',
|
388
|
-
alt: 'My Image that did not load',
|
389
|
-
caption: 'This above is a loaded image',
|
390
|
-
imageVariations: {
|
391
|
-
original: {
|
392
|
-
url: 'https://picsum.photos/200/300',
|
393
|
-
width: 100,
|
394
|
-
height: 100,
|
395
|
-
byteSize: 1000,
|
396
|
-
mimeType: 'image/jpeg',
|
397
|
-
aspectRatio: '1:1',
|
398
|
-
sha1Hash: '1234567890abcdef1234567890abcdef12345678',
|
399
|
-
},
|
400
|
-
},
|
401
|
-
};
|
402
|
-
expect(jsonSchemaService.validateInput(value, schema)).toEqual(true);
|
403
|
-
});
|
404
|
-
it('should error if SquizImage type is missing required properties', async () => {
|
405
|
-
const schema = {
|
406
|
-
type: 'object',
|
407
|
-
properties: {
|
408
|
-
myProp: {
|
409
|
-
type: 'SquizImage',
|
410
|
-
},
|
411
|
-
},
|
412
|
-
};
|
413
|
-
const value = {
|
414
|
-
// @ts-expect-error - missing required properties
|
415
|
-
myProp: {
|
416
|
-
alt: 'alt',
|
417
|
-
caption: 'caption',
|
418
|
-
name: 'name',
|
419
|
-
},
|
420
|
-
};
|
421
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
422
|
-
jsonSchemaService.validateInput(value, schema);
|
423
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Expected `Object {"alt":"alt","caption":"caption","name":"name"}` (object) in `#/myProp` to be of type `SquizImage`', {
|
424
|
-
'#/myProp': [
|
425
|
-
{
|
426
|
-
data: {
|
427
|
-
expected: 'SquizImage',
|
428
|
-
pointer: '#/myProp',
|
429
|
-
received: 'object',
|
430
|
-
value: { alt: 'alt', caption: 'caption', name: 'name' },
|
431
|
-
},
|
432
|
-
message: 'Expected `Object {"alt":"alt","caption":"caption","name":"name"}` (object) in `#/myProp` to be of type `SquizImage`',
|
433
|
-
},
|
434
|
-
],
|
435
|
-
});
|
436
|
-
});
|
437
|
-
});
|
438
|
-
describe('FormattedText input', () => {
|
439
|
-
it('should handle type as an array', () => {
|
440
|
-
const functionInputSchema = {
|
441
|
-
type: 'object',
|
442
|
-
properties: {
|
443
|
-
'my-input': { type: ['number', 'string'] },
|
444
|
-
},
|
445
|
-
required: ['my-input'],
|
446
|
-
};
|
447
|
-
expect(jsonSchemaService.validateInput({
|
448
|
-
'my-input': 'formattedText',
|
449
|
-
}, functionInputSchema)).toEqual(true);
|
450
|
-
expect(jsonSchemaService.validateInput({
|
451
|
-
'my-input': 123,
|
452
|
-
}, functionInputSchema)).toEqual(true);
|
453
|
-
});
|
454
|
-
it.failing('should handle type is an array of both string and FormattedText', () => {
|
455
|
-
const formattedText = [
|
456
|
-
{
|
457
|
-
tag: 'p',
|
458
|
-
type: 'tag',
|
459
|
-
children: [
|
460
|
-
{ type: 'text', value: 'This is some ' },
|
461
|
-
{ type: 'text', value: 'Link to asset 12345' },
|
462
|
-
{ type: 'text', value: ' with an image ' },
|
463
|
-
{ type: 'text', value: '.' },
|
464
|
-
],
|
465
|
-
},
|
466
|
-
];
|
467
|
-
const functionInputSchema = {
|
468
|
-
type: 'object',
|
469
|
-
properties: {
|
470
|
-
'my-input': { type: ['FormattedText', 'string'] },
|
471
|
-
},
|
472
|
-
required: ['my-input'],
|
473
|
-
};
|
474
|
-
expect(jsonSchemaService.validateInput({
|
475
|
-
'my-input': formattedText,
|
476
|
-
}, functionInputSchema)).toEqual(true);
|
477
|
-
expect(jsonSchemaService.validateInput({
|
478
|
-
'my-input': 'hello',
|
479
|
-
}, functionInputSchema)).toEqual(true);
|
480
|
-
});
|
481
|
-
it('should return true if input is formatted text', () => {
|
482
|
-
const functionInputSchema = {
|
483
|
-
type: 'object',
|
484
|
-
properties: {
|
485
|
-
'my-input': { type: 'FormattedText' },
|
486
|
-
},
|
487
|
-
required: ['my-input'],
|
488
|
-
};
|
489
|
-
const formattedText = [
|
490
|
-
{
|
491
|
-
tag: 'p',
|
492
|
-
type: 'tag',
|
493
|
-
children: [
|
494
|
-
{ type: 'text', value: 'This is some ' },
|
495
|
-
{ type: 'text', value: 'Link to asset 12345' },
|
496
|
-
{ type: 'text', value: ' with an image ' },
|
497
|
-
{ type: 'text', value: '.' },
|
498
|
-
],
|
499
|
-
},
|
500
|
-
];
|
501
|
-
const inputValue = {
|
502
|
-
'my-input': formattedText,
|
503
|
-
};
|
504
|
-
expect(() => jsonSchemaService.validateInput(inputValue, functionInputSchema)).not.toThrow();
|
505
|
-
});
|
506
|
-
it('should throw an error if the FormattedText input is not formatted text', () => {
|
507
|
-
const functionInputSchema = {
|
508
|
-
type: 'object',
|
509
|
-
properties: {
|
510
|
-
'my-input': { type: 'FormattedText' },
|
511
|
-
},
|
512
|
-
required: ['my-input'],
|
513
|
-
};
|
514
|
-
const inputValue = {
|
515
|
-
'my-input': 123,
|
516
|
-
};
|
517
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
518
|
-
jsonSchemaService.validateInput(inputValue, functionInputSchema);
|
519
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `123` in `#/my-input` does not match any given oneof schema', {
|
520
|
-
'#/my-input': [
|
521
|
-
{
|
522
|
-
data: {
|
523
|
-
errors: [
|
524
|
-
{
|
525
|
-
code: 'type-error',
|
526
|
-
data: { expected: 'array', pointer: '#/my-input', received: 'number', value: 123 },
|
527
|
-
message: 'Expected `123` (number) in `#/my-input` to be of type `array`',
|
528
|
-
name: 'TypeError',
|
529
|
-
type: 'error',
|
530
|
-
},
|
531
|
-
],
|
532
|
-
oneOf: [{ $ref: 'FormattedText.json' }],
|
533
|
-
pointer: '#/my-input',
|
534
|
-
value: '123',
|
535
|
-
},
|
536
|
-
message: 'Value `123` in `#/my-input` does not match any given oneof schema',
|
537
|
-
},
|
538
|
-
],
|
539
|
-
});
|
540
|
-
});
|
541
|
-
it('should throw an error if the FormattedText input is invalid formatted text', () => {
|
542
|
-
const functionInputSchema = {
|
543
|
-
type: 'object',
|
544
|
-
properties: {
|
545
|
-
'my-input': { type: 'FormattedText' },
|
546
|
-
},
|
547
|
-
required: ['my-input'],
|
548
|
-
};
|
549
|
-
const inputValue = {
|
550
|
-
'my-input': {
|
551
|
-
something: 'aa',
|
552
|
-
},
|
553
|
-
};
|
554
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
555
|
-
jsonSchemaService.validateInput(inputValue, functionInputSchema);
|
556
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `{"something":"aa"}` in `#/my-input` does not match any given oneof schema', {
|
557
|
-
'#/my-input': [
|
558
|
-
{
|
559
|
-
data: {
|
560
|
-
errors: [
|
561
|
-
{
|
562
|
-
code: 'type-error',
|
563
|
-
data: { expected: 'array', pointer: '#/my-input', received: 'object', value: { something: 'aa' } },
|
564
|
-
message: 'Expected `Object {"something":"aa"}` (object) in `#/my-input` to be of type `array`',
|
565
|
-
name: 'TypeError',
|
566
|
-
type: 'error',
|
567
|
-
},
|
568
|
-
],
|
569
|
-
oneOf: [{ $ref: 'FormattedText.json' }],
|
570
|
-
pointer: '#/my-input',
|
571
|
-
value: '{"something":"aa"}',
|
572
|
-
},
|
573
|
-
message: 'Value `{"something":"aa"}` in `#/my-input` does not match any given oneof schema',
|
574
|
-
},
|
575
|
-
],
|
576
|
-
});
|
577
|
-
});
|
578
|
-
it('should throw an error if the FormattedText input is invalid formatted text (deeply nested)', () => {
|
579
|
-
const functionInputSchema = {
|
580
|
-
type: 'object',
|
581
|
-
properties: {
|
582
|
-
'my-input': { type: 'FormattedText' },
|
583
|
-
},
|
584
|
-
required: ['my-input'],
|
585
|
-
};
|
586
|
-
const formattedText = [
|
587
|
-
{
|
588
|
-
tag: 'p',
|
589
|
-
type: 'tag',
|
590
|
-
children: [
|
591
|
-
{ type: 'text', value: 'This is some ' },
|
592
|
-
{ type: 'text', value: 'Link to asset 12345' },
|
593
|
-
{ type: 'text', value: ' with an image ' },
|
594
|
-
{ type: 'text', value: 123 },
|
595
|
-
{ type: 'text', value: '.' },
|
596
|
-
],
|
597
|
-
},
|
598
|
-
];
|
599
|
-
const inputValue = {
|
600
|
-
'my-input': formattedText,
|
601
|
-
};
|
602
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
603
|
-
jsonSchemaService.validateInput(inputValue, functionInputSchema);
|
604
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `[{"tag":"p","type":"tag","children":[{"type":"text","value":"This is some "},{"type":"text","value":"Link to asset 12345"},{"type":"text","value":" with an image "},{"type":"text","value":123},{"type":"text","value":"."}]}]` in `#/my-input` does not match any given oneof schema', {
|
605
|
-
'#/my-input': [
|
606
|
-
{
|
607
|
-
data: {
|
608
|
-
errors: [
|
609
|
-
{
|
610
|
-
code: 'any-of-error',
|
611
|
-
data: {
|
612
|
-
anyOf: [
|
613
|
-
{ $ref: '#/definitions/HigherOrderFormattedNodes' },
|
614
|
-
{ $ref: '#/definitions/BaseFormattedNodes' },
|
615
|
-
],
|
616
|
-
pointer: '#/my-input/0',
|
617
|
-
value: {
|
618
|
-
children: [
|
619
|
-
{ type: 'text', value: 'This is some ' },
|
620
|
-
{ type: 'text', value: 'Link to asset 12345' },
|
621
|
-
{ type: 'text', value: ' with an image ' },
|
622
|
-
{ type: 'text', value: 123 },
|
623
|
-
{ type: 'text', value: '.' },
|
624
|
-
],
|
625
|
-
tag: 'p',
|
626
|
-
type: 'tag',
|
627
|
-
},
|
628
|
-
},
|
629
|
-
message: 'Object at `#/my-input/0` does not match any schema',
|
630
|
-
name: 'AnyOfError',
|
631
|
-
type: 'error',
|
632
|
-
},
|
633
|
-
],
|
634
|
-
oneOf: [{ $ref: 'FormattedText.json' }],
|
635
|
-
pointer: '#/my-input',
|
636
|
-
value: '[{"tag":"p","type":"tag","children":[{"type":"text","value":"This is some "},{"type":"text","value":"Link to asset 12345"},{"type":"text","value":" with an image "},{"type":"text","value":123},{"type":"text","value":"."}]}]',
|
637
|
-
},
|
638
|
-
message: 'Value `[{"tag":"p","type":"tag","children":[{"type":"text","value":"This is some "},{"type":"text","value":"Link to asset 12345"},{"type":"text","value":" with an image "},{"type":"text","value":123},{"type":"text","value":"."}]}]` in `#/my-input` does not match any given oneof schema',
|
639
|
-
},
|
640
|
-
],
|
641
|
-
});
|
642
|
-
});
|
643
|
-
it('should validate an array of formatted texts', () => {
|
644
|
-
const formattedText = [
|
645
|
-
{
|
646
|
-
tag: 'p',
|
647
|
-
type: 'tag',
|
648
|
-
children: [{ type: 'text', value: 'hello' }],
|
649
|
-
},
|
650
|
-
];
|
651
|
-
const schema = {
|
652
|
-
type: 'object',
|
653
|
-
properties: {
|
654
|
-
arr: {
|
655
|
-
type: 'array',
|
656
|
-
items: { type: 'FormattedText' },
|
657
|
-
},
|
658
|
-
},
|
659
|
-
};
|
660
|
-
const value = {
|
661
|
-
arr: [formattedText],
|
662
|
-
};
|
663
|
-
expect(jsonSchemaService.validateInput(value, schema)).toEqual(true);
|
664
|
-
});
|
665
|
-
it('should throw an error if the FormattedText input is invalid formatted text (deeply, deeply nested)', () => {
|
666
|
-
const formattedText = [
|
667
|
-
{
|
668
|
-
tag: 'p',
|
669
|
-
type: 'tag',
|
670
|
-
children: [{ type: 'text', value: 'hello' }],
|
671
|
-
},
|
672
|
-
];
|
673
|
-
const inputValue = {
|
674
|
-
'my-input': formattedText,
|
675
|
-
deep: {
|
676
|
-
arr: [
|
677
|
-
{
|
678
|
-
prop: formattedText,
|
679
|
-
formattedTextArray: [formattedText, formattedText, { bad: 'data' }],
|
680
|
-
},
|
681
|
-
],
|
682
|
-
},
|
683
|
-
};
|
684
|
-
const functionInputSchema = {
|
685
|
-
type: 'object',
|
686
|
-
properties: {
|
687
|
-
'my-input': { type: 'FormattedText' },
|
688
|
-
deep: {
|
689
|
-
type: 'object',
|
690
|
-
properties: {
|
691
|
-
arr: {
|
692
|
-
type: 'array',
|
693
|
-
items: {
|
694
|
-
type: 'object',
|
695
|
-
required: ['prop', 'formattedTextArray'],
|
696
|
-
properties: {
|
697
|
-
prop: 'FormattedText',
|
698
|
-
formattedTextArray: {
|
699
|
-
type: 'array',
|
700
|
-
items: {
|
701
|
-
type: 'FormattedText',
|
702
|
-
},
|
703
|
-
},
|
704
|
-
},
|
705
|
-
},
|
706
|
-
},
|
707
|
-
},
|
708
|
-
required: ['arr'],
|
709
|
-
},
|
710
|
-
},
|
711
|
-
required: ['my-input', 'deep'],
|
712
|
-
};
|
713
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
714
|
-
jsonSchemaService.validateInput(inputValue, functionInputSchema);
|
715
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `{"bad":"data"}` in `#/deep/arr/0/formattedTextArray/2` does not match any given oneof schema', {
|
716
|
-
'#/deep/arr/0/formattedTextArray/2': [
|
717
|
-
{
|
718
|
-
data: {
|
719
|
-
errors: [
|
720
|
-
{
|
721
|
-
code: 'type-error',
|
722
|
-
data: {
|
723
|
-
expected: 'array',
|
724
|
-
pointer: '#/deep/arr/0/formattedTextArray/2',
|
725
|
-
received: 'object',
|
726
|
-
value: { bad: 'data' },
|
727
|
-
},
|
728
|
-
message: 'Expected `Object {"bad":"data"}` (object) in `#/deep/arr/0/formattedTextArray/2` to be of type `array`',
|
729
|
-
name: 'TypeError',
|
730
|
-
type: 'error',
|
731
|
-
},
|
732
|
-
],
|
733
|
-
oneOf: [{ $ref: 'FormattedText.json' }],
|
734
|
-
pointer: '#/deep/arr/0/formattedTextArray/2',
|
735
|
-
value: '{"bad":"data"}',
|
736
|
-
},
|
737
|
-
message: 'Value `{"bad":"data"}` in `#/deep/arr/0/formattedTextArray/2` does not match any given oneof schema',
|
738
|
-
},
|
739
|
-
],
|
740
|
-
});
|
741
|
-
});
|
742
|
-
it('should validate a FormattedText value when the schema is a $ref', async () => {
|
743
|
-
const formattedText = [
|
744
|
-
{
|
745
|
-
tag: 'p',
|
746
|
-
type: 'tag',
|
747
|
-
children: [{ type: 'text', value: 'hello' }],
|
748
|
-
},
|
749
|
-
];
|
750
|
-
const schema = {
|
751
|
-
type: 'object',
|
752
|
-
properties: {
|
753
|
-
'my-input': { $ref: '#/definitions/FormattedText' },
|
754
|
-
},
|
755
|
-
definitions: {
|
756
|
-
FormattedText: { type: 'FormattedText' },
|
757
|
-
},
|
758
|
-
required: ['my-input'],
|
759
|
-
};
|
760
|
-
const value = {
|
761
|
-
'my-input': formattedText,
|
762
|
-
};
|
763
|
-
expect(jsonSchemaService.validateInput(value, schema)).toEqual(true);
|
764
|
-
});
|
765
|
-
it('should error when a FormattedText value is invalid when the schema is a $ref', async () => {
|
766
|
-
const schema = {
|
767
|
-
type: 'object',
|
768
|
-
properties: {
|
769
|
-
'my-input': { $ref: '#/definitions/FormattedText' },
|
770
|
-
},
|
771
|
-
definitions: {
|
772
|
-
FormattedText: { type: 'FormattedText' },
|
773
|
-
},
|
774
|
-
required: ['my-input'],
|
775
|
-
};
|
776
|
-
const value = {
|
777
|
-
'my-input': { bad: 'data' },
|
778
|
-
};
|
779
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
780
|
-
jsonSchemaService.validateInput(value, schema);
|
781
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `{"bad":"data"}` in `#/my-input` does not match any given oneof schema', {
|
782
|
-
'#/my-input': [
|
783
|
-
{
|
784
|
-
data: {
|
785
|
-
errors: [
|
786
|
-
{
|
787
|
-
code: 'type-error',
|
788
|
-
data: { expected: 'array', pointer: '#/my-input', received: 'object', value: { bad: 'data' } },
|
789
|
-
message: 'Expected `Object {"bad":"data"}` (object) in `#/my-input` to be of type `array`',
|
790
|
-
name: 'TypeError',
|
791
|
-
type: 'error',
|
792
|
-
},
|
793
|
-
],
|
794
|
-
oneOf: [{ $ref: 'FormattedText.json' }],
|
795
|
-
pointer: '#/my-input',
|
796
|
-
value: '{"bad":"data"}',
|
797
|
-
},
|
798
|
-
message: 'Value `{"bad":"data"}` in `#/my-input` does not match any given oneof schema',
|
799
|
-
},
|
800
|
-
],
|
801
|
-
});
|
802
|
-
});
|
803
|
-
it('should validate a FormattedText value when the schema is a $ref to a $ref', async () => {
|
804
|
-
const formattedText = [
|
805
|
-
{
|
806
|
-
tag: 'p',
|
807
|
-
type: 'tag',
|
808
|
-
children: [{ type: 'text', value: 'hello' }],
|
809
|
-
},
|
810
|
-
];
|
811
|
-
const schema = {
|
812
|
-
type: 'object',
|
813
|
-
properties: {
|
814
|
-
'my-input': { $ref: '#/definitions/FormattedText' },
|
815
|
-
},
|
816
|
-
definitions: {
|
817
|
-
FormattedText: { $ref: '#/definitions/FormattedText2' },
|
818
|
-
FormattedText2: { type: 'FormattedText' },
|
819
|
-
},
|
820
|
-
required: ['my-input'],
|
821
|
-
};
|
822
|
-
const value = {
|
823
|
-
'my-input': formattedText,
|
824
|
-
};
|
825
|
-
expect(jsonSchemaService.validateInput(value, schema)).toEqual(true);
|
826
|
-
});
|
827
|
-
it('should validate a FormattedText value when the schema is in an if/then/else', async () => {
|
828
|
-
const formattedText = [
|
829
|
-
{
|
830
|
-
tag: 'p',
|
831
|
-
type: 'tag',
|
832
|
-
children: [{ type: 'text', value: 'hello' }],
|
833
|
-
},
|
834
|
-
];
|
835
|
-
const schema = {
|
836
|
-
type: 'object',
|
837
|
-
properties: {
|
838
|
-
'my-input': {
|
839
|
-
if: { type: 'string' },
|
840
|
-
then: { type: 'string' },
|
841
|
-
else: { type: 'FormattedText' },
|
842
|
-
},
|
843
|
-
},
|
844
|
-
required: ['my-input'],
|
845
|
-
};
|
846
|
-
const value = {
|
847
|
-
'my-input': formattedText,
|
848
|
-
};
|
849
|
-
expect(jsonSchemaService.validateInput(value, schema)).toEqual(true);
|
850
|
-
});
|
851
|
-
it('should allow an empty array', async () => {
|
852
|
-
const schema = {
|
853
|
-
type: 'object',
|
854
|
-
properties: {
|
855
|
-
'my-input': { type: 'FormattedText' },
|
856
|
-
},
|
857
|
-
required: [],
|
858
|
-
};
|
859
|
-
expect(jsonSchemaService.validateInput({ 'my-input': [] }, schema)).toEqual(true);
|
860
|
-
});
|
861
|
-
it.each([
|
862
|
-
['with attributes', { title: 'Link title' }],
|
863
|
-
['without attributes', undefined],
|
864
|
-
])('should validate link-to-matrix-asset - %s', (_, attributes) => {
|
865
|
-
const formattedText = [
|
866
|
-
{
|
867
|
-
type: 'link-to-matrix-asset',
|
868
|
-
matrixIdentifier: 'matrix-identifier',
|
869
|
-
matrixDomain: 'https://matrix-api-url',
|
870
|
-
target: '_blank',
|
871
|
-
matrixAssetId: '12345',
|
872
|
-
children: [{ type: 'text', value: 'hello' }],
|
873
|
-
attributes,
|
874
|
-
},
|
875
|
-
];
|
876
|
-
const schema = {
|
877
|
-
type: 'object',
|
878
|
-
properties: {
|
879
|
-
myInput: {
|
880
|
-
type: 'FormattedText',
|
881
|
-
},
|
882
|
-
},
|
883
|
-
};
|
884
|
-
const value = {
|
885
|
-
myInput: formattedText,
|
886
|
-
};
|
887
|
-
expect(jsonSchemaService.validateInput(value, schema)).toBe(true);
|
888
|
-
});
|
889
|
-
it('should throw if link-to-matrix-asset contains a reserved attribute', () => {
|
890
|
-
const formattedText = [
|
891
|
-
{
|
892
|
-
type: 'link-to-matrix-asset',
|
893
|
-
matrixIdentifier: 'matrix-identifier',
|
894
|
-
matrixDomain: 'https://matrix-api-url',
|
895
|
-
target: '_blank',
|
896
|
-
matrixAssetId: '12345',
|
897
|
-
children: [{ type: 'text', value: 'hello' }],
|
898
|
-
attributes: {
|
899
|
-
// href is reserved (resolved from the selected Matrix asset)
|
900
|
-
href: 'https://www.my-matrix.squiz.net',
|
901
|
-
},
|
902
|
-
},
|
903
|
-
];
|
904
|
-
const schema = {
|
905
|
-
type: 'object',
|
906
|
-
properties: {
|
907
|
-
myInput: {
|
908
|
-
type: 'FormattedText',
|
909
|
-
},
|
910
|
-
},
|
911
|
-
};
|
912
|
-
const value = {
|
913
|
-
myInput: formattedText,
|
914
|
-
};
|
915
|
-
expect(() => jsonSchemaService.validateInput(value, schema)).toThrow(/does not match any given oneof schema/);
|
916
|
-
});
|
917
|
-
it.each([
|
918
|
-
['with attributes', { title: 'Link title' }],
|
919
|
-
['without attributes', undefined],
|
920
|
-
])('should validate matrix-image - %s', (_, attributes) => {
|
921
|
-
const formattedText = [
|
922
|
-
{
|
923
|
-
type: 'matrix-image',
|
924
|
-
matrixIdentifier: 'matrix-identifier',
|
925
|
-
matrixDomain: 'https://matrix-api-url',
|
926
|
-
matrixAssetId: '12345',
|
927
|
-
attributes,
|
928
|
-
},
|
929
|
-
];
|
930
|
-
const schema = {
|
931
|
-
type: 'object',
|
932
|
-
properties: {
|
933
|
-
myInput: {
|
934
|
-
type: 'FormattedText',
|
935
|
-
},
|
936
|
-
},
|
937
|
-
};
|
938
|
-
const value = {
|
939
|
-
myInput: formattedText,
|
940
|
-
};
|
941
|
-
expect(jsonSchemaService.validateInput(value, schema)).toBe(true);
|
942
|
-
});
|
943
|
-
it('should throw if matrix-image contains a reserved attribute', () => {
|
944
|
-
const formattedText = [
|
945
|
-
{
|
946
|
-
type: 'matrix-image',
|
947
|
-
matrixIdentifier: 'matrix-identifier',
|
948
|
-
matrixDomain: 'https://matrix-api-url',
|
949
|
-
matrixAssetId: '12345',
|
950
|
-
attributes: {
|
951
|
-
// src is reserved (resolved from the selected Matrix asset)
|
952
|
-
src: 'https://www.my-matrix.squiz.net/image.png',
|
953
|
-
},
|
954
|
-
},
|
955
|
-
];
|
956
|
-
const schema = {
|
957
|
-
type: 'object',
|
958
|
-
properties: {
|
959
|
-
myInput: {
|
960
|
-
type: 'FormattedText',
|
961
|
-
},
|
962
|
-
},
|
963
|
-
};
|
964
|
-
const value = {
|
965
|
-
myInput: formattedText,
|
966
|
-
};
|
967
|
-
expect(() => jsonSchemaService.validateInput(value, schema)).toThrow(/does not match any given oneof schema/);
|
968
|
-
});
|
969
|
-
it.each([
|
970
|
-
['with attributes', { title: 'Link title' }],
|
971
|
-
['without attributes', undefined],
|
972
|
-
])('should validate dam-image - %s', (_, attributes) => {
|
973
|
-
const formattedText = [
|
974
|
-
{
|
975
|
-
type: 'dam-image',
|
976
|
-
damSystemIdentifier: 'dam-identifier',
|
977
|
-
damObjectId: '12345',
|
978
|
-
damSystemType: 'bynder',
|
979
|
-
attributes,
|
980
|
-
},
|
981
|
-
];
|
982
|
-
const schema = {
|
983
|
-
type: 'object',
|
984
|
-
properties: {
|
985
|
-
myInput: {
|
986
|
-
type: 'FormattedText',
|
987
|
-
},
|
988
|
-
},
|
989
|
-
};
|
990
|
-
const value = {
|
991
|
-
myInput: formattedText,
|
992
|
-
};
|
993
|
-
expect(jsonSchemaService.validateInput(value, schema)).toBe(true);
|
994
|
-
});
|
995
|
-
it('should throw if dam-image contains a reserved attribute', () => {
|
996
|
-
const formattedText = [
|
997
|
-
{
|
998
|
-
type: 'dam-image',
|
999
|
-
damSystemIdentifier: 'dam-identifier',
|
1000
|
-
damObjectId: '12345',
|
1001
|
-
damSystemType: 'bynder',
|
1002
|
-
attributes: {
|
1003
|
-
// src is reserved (resolved from the selected DAM asset)
|
1004
|
-
src: 'https://www.my-matrix.squiz.net/image.png',
|
1005
|
-
},
|
1006
|
-
},
|
1007
|
-
];
|
1008
|
-
const schema = {
|
1009
|
-
type: 'object',
|
1010
|
-
properties: {
|
1011
|
-
myInput: {
|
1012
|
-
type: 'FormattedText',
|
1013
|
-
},
|
1014
|
-
},
|
1015
|
-
};
|
1016
|
-
const value = {
|
1017
|
-
myInput: formattedText,
|
1018
|
-
};
|
1019
|
-
expect(() => jsonSchemaService.validateInput(value, schema)).toThrow(/does not match any given oneof schema/);
|
1020
|
-
});
|
1021
|
-
});
|
1022
|
-
describe('standard inputs', () => {
|
1023
|
-
const functionInputSchema = {
|
1024
|
-
type: 'object',
|
1025
|
-
properties: {
|
1026
|
-
'my-input': { type: 'number' },
|
1027
|
-
},
|
1028
|
-
required: ['my-input'],
|
1029
|
-
};
|
1030
|
-
it('should return true for valid input values', () => {
|
1031
|
-
const inputValue = {
|
1032
|
-
'my-input': 123,
|
1033
|
-
};
|
1034
|
-
const result = jsonSchemaService.validateInput(inputValue, functionInputSchema);
|
1035
|
-
expect(result).toBe(true);
|
1036
|
-
});
|
1037
|
-
it('should throw a SchemaValidationError for invalid input type', () => {
|
1038
|
-
const inputValue = {
|
1039
|
-
'my-input': '123',
|
1040
|
-
};
|
1041
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
1042
|
-
jsonSchemaService.validateInput(inputValue, functionInputSchema);
|
1043
|
-
}, SchemaValidationError_1.SchemaValidationError, `failed validation: Expected \`123\` (string) in \`#/my-input\` to be of type \`number\``, {
|
1044
|
-
'#/my-input': [
|
1045
|
-
{
|
1046
|
-
data: { expected: 'number', pointer: '#/my-input', received: 'string', value: '123' },
|
1047
|
-
message: 'Expected `123` (string) in `#/my-input` to be of type `number`',
|
1048
|
-
},
|
1049
|
-
],
|
1050
|
-
});
|
1051
|
-
});
|
1052
|
-
it('should throw a SchemaValidationError for invalid input missing properties', () => {
|
1053
|
-
const inputValue = {};
|
1054
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
1055
|
-
jsonSchemaService.validateInput(inputValue, functionInputSchema);
|
1056
|
-
}, SchemaValidationError_1.SchemaValidationError, `failed validation: The required property \`my-input\` is missing at \`#\``, {
|
1057
|
-
'#': [
|
1058
|
-
{ data: { key: 'my-input', pointer: '#' }, message: 'The required property `my-input` is missing at `#`' },
|
1059
|
-
],
|
1060
|
-
});
|
1061
|
-
});
|
1062
|
-
it('should throw a SchemaValidationError with a truncated enum value list if there are more than 5 enum options', () => {
|
1063
|
-
expectToThrowErrorMatchingTypeAndMessage(() => {
|
1064
|
-
jsonSchemaService.validateInput({ enum: 'z' }, { type: 'object', properties: { enum: { type: 'string', enum: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] } } });
|
1065
|
-
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Expected given value `z` in #/enum` to be one of `[a, b, c, d, e, ... 2 more]`', {
|
1066
|
-
'#/enum': [
|
1067
|
-
{
|
1068
|
-
data: { pointer: '#/enum', value: 'z', values: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] },
|
1069
|
-
message: 'Expected given value `z` in #/enum` to be one of `[a, b, c, d, e, ... 2 more]`',
|
1070
|
-
},
|
1071
|
-
],
|
1072
|
-
});
|
1073
|
-
});
|
1074
|
-
// TODO DEVX-658
|
1075
|
-
it.skip('should throw a SchemaValidationError for invalid input additional properties', () => {
|
1076
|
-
const inputValue = {
|
1077
|
-
'my-input': 123,
|
1078
|
-
'my-input-2': 123,
|
1079
|
-
};
|
1080
|
-
expect(() => {
|
1081
|
-
jsonSchemaService.validateInput(inputValue, functionInputSchema);
|
1082
|
-
}).toThrowErrorMatchingInlineSnapshot();
|
1083
|
-
});
|
1084
|
-
});
|
1085
|
-
describe('validateInput', () => {
|
1086
|
-
it.each([String('123'), Number(123), [123]])('should validate any primitive type with its resolvable type %s', (propertyValue) => {
|
1087
|
-
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
1088
|
-
const MyResolvableNumber = resolvableTypeFixture('MyResolvableNumber', { type: 'number' });
|
1089
|
-
const MyResolvableArray = resolvableTypeFixture('MyResolvableArray', { type: 'array' });
|
1090
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new TypeResolver_1.TypeResolver([primitiveSchema], [MyResolvableNumber, MyResolvableArray], {
|
1091
|
-
MyPrimitive: {
|
1092
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1093
|
-
MyResolvableNumber: (value) => value.toString(),
|
1094
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1095
|
-
MyResolvableArray: (value) => value.join(''),
|
1096
|
-
},
|
1097
|
-
}), JsonValidationService_1.ComponentInputMetaSchema);
|
1098
|
-
expect(jsonSchemaService.validateInput({ myProperty: propertyValue }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).toEqual(true);
|
1099
|
-
});
|
1100
|
-
it('should error when a primitive type is provided a value that cannot be resolved by its resolvable types', () => {
|
1101
|
-
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
1102
|
-
const MyResolvableNumber = resolvableTypeFixture('MyResolvableNumber', { type: 'number' });
|
1103
|
-
const MyResolvableArray = resolvableTypeFixture('MyResolvableArray', { type: 'array' });
|
1104
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new TypeResolver_1.TypeResolver([primitiveSchema], [MyResolvableNumber, MyResolvableArray], {
|
1105
|
-
MyPrimitive: {
|
1106
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1107
|
-
MyResolvableNumber: (value) => value.toString(),
|
1108
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1109
|
-
MyResolvableArray: (value) => value.join(''),
|
1110
|
-
},
|
1111
|
-
}), JsonValidationService_1.ComponentInputMetaSchema);
|
1112
|
-
expect(() => {
|
1113
|
-
jsonSchemaService.validateInput({ myProperty: true }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } });
|
1114
|
-
}).toThrowError();
|
1115
|
-
});
|
1116
|
-
it.each([String('123'), Number(123), [123]])('should validate a primitive type when defined as a ref with resolvable value %s', (propertyValue) => {
|
1117
|
-
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
1118
|
-
const MyResolvableNumber = resolvableTypeFixture('MyResolvableNumber', { type: 'number' });
|
1119
|
-
const MyResolvableArray = resolvableTypeFixture('MyResolvableArray', { type: 'array' });
|
1120
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new TypeResolver_1.TypeResolver([primitiveSchema], [MyResolvableNumber, MyResolvableArray], {
|
1121
|
-
MyPrimitive: {
|
1122
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1123
|
-
MyResolvableNumber: (value) => value.toString(),
|
1124
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1125
|
-
MyResolvableArray: (value) => value.join(''),
|
1126
|
-
},
|
1127
|
-
}), JsonValidationService_1.ComponentInputMetaSchema);
|
1128
|
-
expect(jsonSchemaService.validateInput({ myProperty: propertyValue }, {
|
1129
|
-
type: 'object',
|
1130
|
-
properties: { myProperty: { $ref: '#/definitions/Ref' } },
|
1131
|
-
definitions: { Ref: { type: 'MyPrimitive' } },
|
1132
|
-
})).toEqual(true);
|
1133
|
-
});
|
1134
|
-
it('should not validate on a primitive type against a resolvable type when a resolver is not defined', () => {
|
1135
|
-
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
1136
|
-
const MyResolvableNumber = resolvableTypeFixture('MyResolvableNumber', { type: 'number' });
|
1137
|
-
const MyResolvableArray = resolvableTypeFixture('MyResolvableArray', { type: 'array' });
|
1138
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new TypeResolver_1.TypeResolver([primitiveSchema], [MyResolvableNumber, MyResolvableArray], {
|
1139
|
-
MyPrimitive: {
|
1140
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1141
|
-
MyResolvableNumber: (value) => value.toString(),
|
1142
|
-
},
|
1143
|
-
}), JsonValidationService_1.ComponentInputMetaSchema);
|
1144
|
-
expect(() => {
|
1145
|
-
jsonSchemaService.validateInput({ myProperty: [123] }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } });
|
1146
|
-
}).toThrowError();
|
1147
|
-
});
|
1148
|
-
it('should validate a primitive type against similar but different resolvable types', () => {
|
1149
|
-
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
1150
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new TypeResolver_1.TypeResolver([primitiveSchema], [
|
1151
|
-
resolvableTypeFixture('MyResolvableSrcNumber', {
|
1152
|
-
type: 'object',
|
1153
|
-
properties: {
|
1154
|
-
src: { type: 'number' },
|
1155
|
-
},
|
1156
|
-
}),
|
1157
|
-
resolvableTypeFixture('MyResolvableSrcString', {
|
1158
|
-
type: 'object',
|
1159
|
-
properties: {
|
1160
|
-
src: { type: 'string' },
|
1161
|
-
},
|
1162
|
-
}),
|
1163
|
-
], {
|
1164
|
-
MyPrimitive: {
|
1165
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1166
|
-
MyResolvableSrcNumber: (value) => value.src.toString(),
|
1167
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1168
|
-
MyResolvableSrcString: (value) => value.src,
|
1169
|
-
},
|
1170
|
-
}), JsonValidationService_1.ComponentInputMetaSchema);
|
1171
|
-
expect(jsonSchemaService.validateInput({
|
1172
|
-
myProperty: {
|
1173
|
-
src: 123,
|
1174
|
-
},
|
1175
|
-
}, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).toEqual(true);
|
1176
|
-
expect(jsonSchemaService.validateInput({
|
1177
|
-
myProperty: {
|
1178
|
-
src: '123',
|
1179
|
-
},
|
1180
|
-
}, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).toEqual(true);
|
1181
|
-
});
|
1182
|
-
});
|
1183
|
-
describe('resolveInput', () => {
|
1184
|
-
it.each([String('123'), Number(123), [123]])('should resolve a primitive type from its resolvable type %s', async (resolvableValue) => {
|
1185
|
-
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
1186
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new TypeResolver_1.TypeResolver([primitiveSchema], [
|
1187
|
-
resolvableTypeFixture('MyResolvableNumber', { type: 'number' }),
|
1188
|
-
resolvableTypeFixture('MyResolvableArray', { type: 'array' }),
|
1189
|
-
], {
|
1190
|
-
MyPrimitive: {
|
1191
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1192
|
-
MyResolvableNumber: (value) => value.toString(),
|
1193
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1194
|
-
MyResolvableArray: (value) => value.join(''),
|
1195
|
-
},
|
1196
|
-
}), JsonValidationService_1.ComponentInputMetaSchema);
|
1197
|
-
await expect(jsonSchemaService.resolveInput({ myProperty: resolvableValue }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).resolves.toEqual({ myProperty: '123' });
|
1198
|
-
});
|
1199
|
-
it.each([
|
1200
|
-
[{ src: 'MyString' }, 'MyString'],
|
1201
|
-
[{ src: 1132 }, '1132'],
|
1202
|
-
])('should resolve a resolvable type %s against the correct resolver to %s', async (resolvableValue, output) => {
|
1203
|
-
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
1204
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new TypeResolver_1.TypeResolver([primitiveSchema], [
|
1205
|
-
resolvableTypeFixture('MyResolvableSrcString', {
|
1206
|
-
type: 'object',
|
1207
|
-
properties: { src: { type: 'string' } },
|
1208
|
-
}),
|
1209
|
-
resolvableTypeFixture('MyResolvableSrcNumber', {
|
1210
|
-
type: 'object',
|
1211
|
-
properties: { src: { type: 'number' } },
|
1212
|
-
}),
|
1213
|
-
], {
|
1214
|
-
MyPrimitive: {
|
1215
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1216
|
-
MyResolvableSrcNumber: (value) => value.src.toString(),
|
1217
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1218
|
-
MyResolvableSrcString: (value) => value.src,
|
1219
|
-
},
|
1220
|
-
}), JsonValidationService_1.ComponentInputMetaSchema);
|
1221
|
-
await expect(jsonSchemaService.resolveInput({ myProperty: resolvableValue }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).resolves.toEqual({ myProperty: output });
|
1222
|
-
});
|
1223
|
-
it('should resolve a primitive type from its resolvable type %s', async () => {
|
1224
|
-
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
1225
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new TypeResolver_1.TypeResolver([primitiveSchema], [resolvableTypeFixture('MyResolvableWithError', { type: 'number' })], {
|
1226
|
-
MyPrimitive: {
|
1227
|
-
// @ts-expect-error - fixture is unknown but we know the actual shape
|
1228
|
-
MyResolvableWithError: (_value) => {
|
1229
|
-
throw new Error('Failed resolving!!');
|
1230
|
-
},
|
1231
|
-
},
|
1232
|
-
}), JsonValidationService_1.ComponentInputMetaSchema);
|
1233
|
-
await expect(jsonSchemaService.resolveInput({ myProperty: 123, myProperty2: 234 }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' }, myProperty2: { type: 'MyPrimitive' } } })).rejects.toThrowErrorMatchingInlineSnapshot(`
|
1234
|
-
"Error(s) occurred when resolving JSON:
|
1235
|
-
Error: Error resolving JSON at #/myProperty: Failed resolving!!
|
1236
|
-
Error: Error resolving JSON at #/myProperty2: Failed resolving!!"
|
1237
|
-
`);
|
1238
|
-
});
|
1239
|
-
it('should resolve a FormattedText empty list', async () => {
|
1240
|
-
const types = TypeResolverBuilder_1.TypeResolverBuilder.new().addPrimitive(primitiveTypes_1.FormattedTextType).build();
|
1241
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(types, JsonValidationService_1.ComponentInputMetaSchema);
|
1242
|
-
await expect(jsonSchemaService.resolveInput({ myProp: [] }, { type: 'object', properties: { myProp: { type: 'FormattedText' } }, required: [] })).resolves.toEqual({ myProp: [] });
|
1243
|
-
});
|
1244
|
-
});
|
1245
|
-
});
|
1246
|
-
describe('JSONSchemaService - validation', () => {
|
1247
|
-
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1248
|
-
.addPrimitive(primitiveTypes_1.SquizImageType)
|
1249
|
-
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1250
|
-
.addPrimitive(primitiveTypes_1.FormattedTextType)
|
1251
|
-
.build();
|
1252
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1253
|
-
it('should validate a schema with all the squiz primitive types', () => {
|
1254
|
-
const schema = {
|
1255
|
-
type: 'object',
|
1256
|
-
properties: {
|
1257
|
-
image: { type: 'SquizImage' },
|
1258
|
-
link: { type: 'SquizLink' },
|
1259
|
-
text: { type: 'FormattedText' },
|
1260
|
-
},
|
1261
|
-
required: ['image', 'link', 'text'],
|
1262
|
-
};
|
1263
|
-
const formattedText = [
|
1264
|
-
{
|
1265
|
-
tag: 'p',
|
1266
|
-
type: 'tag',
|
1267
|
-
children: [{ type: 'text', value: 'hello' }],
|
1268
|
-
},
|
1269
|
-
];
|
1270
|
-
const input = {
|
1271
|
-
image: {
|
1272
|
-
name: 'test-image.jpeg',
|
1273
|
-
imageVariations: {
|
1274
|
-
original: {
|
1275
|
-
aspectRatio: '1:1',
|
1276
|
-
height: 100,
|
1277
|
-
width: 100,
|
1278
|
-
url: 'https://www.squiz.net',
|
1279
|
-
byteSize: 100,
|
1280
|
-
mimeType: 'image/jpeg',
|
1281
|
-
sha1Hash: '123',
|
1282
|
-
},
|
1283
|
-
},
|
1284
|
-
},
|
1285
|
-
link: {
|
1286
|
-
text: 'test-link',
|
1287
|
-
url: 'https://www.squiz.net',
|
1288
|
-
target: '_blank',
|
1289
|
-
},
|
1290
|
-
text: formattedText,
|
1291
|
-
};
|
1292
|
-
const result = jsonSchemaService.validateInput(input, schema);
|
1293
|
-
expect(result).toEqual(true);
|
1294
|
-
});
|
1295
|
-
it('should validate a schema with all the squiz primitive types and matrix-asset-uri format', () => {
|
1296
|
-
const schema = {
|
1297
|
-
type: 'object',
|
1298
|
-
properties: {
|
1299
|
-
image: { type: 'SquizImage' },
|
1300
|
-
link: { type: 'SquizLink' },
|
1301
|
-
text: { type: 'FormattedText' },
|
1302
|
-
asset: {
|
1303
|
-
type: 'string',
|
1304
|
-
format: 'matrix-asset-uri',
|
1305
|
-
},
|
1306
|
-
},
|
1307
|
-
required: ['image', 'link', 'text', 'asset'],
|
1308
|
-
};
|
1309
|
-
const formattedText = [
|
1310
|
-
{
|
1311
|
-
tag: 'p',
|
1312
|
-
type: 'tag',
|
1313
|
-
children: [{ type: 'text', value: 'hello' }],
|
1314
|
-
},
|
1315
|
-
];
|
1316
|
-
const input = {
|
1317
|
-
image: {
|
1318
|
-
name: 'test-image.jpeg',
|
1319
|
-
imageVariations: {
|
1320
|
-
original: {
|
1321
|
-
aspectRatio: '1:1',
|
1322
|
-
height: 100,
|
1323
|
-
width: 100,
|
1324
|
-
url: 'https://www.squiz.net',
|
1325
|
-
byteSize: 100,
|
1326
|
-
mimeType: 'image/jpeg',
|
1327
|
-
sha1Hash: '123',
|
1328
|
-
},
|
1329
|
-
},
|
1330
|
-
},
|
1331
|
-
link: {
|
1332
|
-
text: 'test-link',
|
1333
|
-
url: 'https://www.squiz.net',
|
1334
|
-
target: '_blank',
|
1335
|
-
},
|
1336
|
-
text: formattedText,
|
1337
|
-
asset: 'matrix-asset://identifier/123',
|
1338
|
-
};
|
1339
|
-
const result = jsonSchemaService.validateInput(input, schema);
|
1340
|
-
expect(result).toEqual(true);
|
1341
|
-
});
|
1342
|
-
it('should catch validation errors when there is a schema with all the squiz primitive types', () => {
|
1343
|
-
const schema = {
|
1344
|
-
type: 'object',
|
1345
|
-
properties: {
|
1346
|
-
image: { type: 'SquizImage' },
|
1347
|
-
link: { type: 'SquizLink' },
|
1348
|
-
text: { type: 'FormattedText' },
|
1349
|
-
},
|
1350
|
-
required: ['image', 'link', 'text'],
|
1351
|
-
};
|
1352
|
-
const formattedText = [
|
1353
|
-
//@ts-expect-error - wrong type
|
1354
|
-
{
|
1355
|
-
children: [{ type: 'text', value: 'hello' }],
|
1356
|
-
},
|
1357
|
-
];
|
1358
|
-
const input = {
|
1359
|
-
image: {
|
1360
|
-
name: 'test-image.jpeg',
|
1361
|
-
imageVariations: {
|
1362
|
-
//@ts-expect-error - wrong type
|
1363
|
-
original: {
|
1364
|
-
width: 100,
|
1365
|
-
url: 'https://www.squiz.net',
|
1366
|
-
byteSize: 100,
|
1367
|
-
mimeType: 'image/jpeg',
|
1368
|
-
sha1Hash: '123',
|
1369
|
-
},
|
1370
|
-
},
|
1371
|
-
},
|
1372
|
-
//@ts-expect-error - wrong type
|
1373
|
-
link: {
|
1374
|
-
text: 'test-link',
|
1375
|
-
target: '_blank',
|
1376
|
-
},
|
1377
|
-
text: formattedText,
|
1378
|
-
};
|
1379
|
-
expectToThrowErrorMatchingTypeAndMessage(() => jsonSchemaService.validateInput(input, schema), SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}` in `#/image` does not match any given oneof schema,\nValue `{"text":"test-link","target":"_blank"}` in `#/link` does not match any given oneof schema,\nValue `[{"children":[{"type":"text","value":"hello"}]}]` in `#/text` does not match any given oneof schema', {
|
1380
|
-
'#/image': [
|
1381
|
-
{
|
1382
|
-
data: {
|
1383
|
-
errors: [
|
1384
|
-
{
|
1385
|
-
code: 'required-property-error',
|
1386
|
-
data: { key: 'height', pointer: '#/image/imageVariations/original' },
|
1387
|
-
message: 'The required property `height` is missing at `#/image/imageVariations/original`',
|
1388
|
-
name: 'RequiredPropertyError',
|
1389
|
-
type: 'error',
|
1390
|
-
},
|
1391
|
-
{
|
1392
|
-
code: 'required-property-error',
|
1393
|
-
data: { key: 'aspectRatio', pointer: '#/image/imageVariations/original' },
|
1394
|
-
message: 'The required property `aspectRatio` is missing at `#/image/imageVariations/original`',
|
1395
|
-
name: 'RequiredPropertyError',
|
1396
|
-
type: 'error',
|
1397
|
-
},
|
1398
|
-
],
|
1399
|
-
oneOf: [{ $ref: 'SquizImage.json' }],
|
1400
|
-
pointer: '#/image',
|
1401
|
-
value: '{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}',
|
1402
|
-
},
|
1403
|
-
message: 'Value `{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}` in `#/image` does not match any given oneof schema',
|
1404
|
-
},
|
1405
|
-
],
|
1406
|
-
'#/link': [
|
1407
|
-
{
|
1408
|
-
data: {
|
1409
|
-
errors: [
|
1410
|
-
{
|
1411
|
-
code: 'required-property-error',
|
1412
|
-
data: { key: 'url', pointer: '#/link' },
|
1413
|
-
message: 'The required property `url` is missing at `#/link`',
|
1414
|
-
name: 'RequiredPropertyError',
|
1415
|
-
type: 'error',
|
1416
|
-
},
|
1417
|
-
],
|
1418
|
-
oneOf: [{ $ref: 'SquizLink.json' }],
|
1419
|
-
pointer: '#/link',
|
1420
|
-
value: '{"text":"test-link","target":"_blank"}',
|
1421
|
-
},
|
1422
|
-
message: 'Value `{"text":"test-link","target":"_blank"}` in `#/link` does not match any given oneof schema',
|
1423
|
-
},
|
1424
|
-
],
|
1425
|
-
'#/text': [
|
1426
|
-
{
|
1427
|
-
data: {
|
1428
|
-
errors: [
|
1429
|
-
{
|
1430
|
-
code: 'any-of-error',
|
1431
|
-
data: {
|
1432
|
-
anyOf: [
|
1433
|
-
{ $ref: '#/definitions/HigherOrderFormattedNodes' },
|
1434
|
-
{ $ref: '#/definitions/BaseFormattedNodes' },
|
1435
|
-
],
|
1436
|
-
pointer: '#/text/0',
|
1437
|
-
value: { children: [{ type: 'text', value: 'hello' }] },
|
1438
|
-
},
|
1439
|
-
message: 'Object at `#/text/0` does not match any schema',
|
1440
|
-
name: 'AnyOfError',
|
1441
|
-
type: 'error',
|
1442
|
-
},
|
1443
|
-
],
|
1444
|
-
oneOf: [{ $ref: 'FormattedText.json' }],
|
1445
|
-
pointer: '#/text',
|
1446
|
-
value: '[{"children":[{"type":"text","value":"hello"}]}]',
|
1447
|
-
},
|
1448
|
-
message: 'Value `[{"children":[{"type":"text","value":"hello"}]}]` in `#/text` does not match any given oneof schema',
|
1449
|
-
},
|
1450
|
-
],
|
1451
|
-
});
|
1452
|
-
});
|
1453
|
-
it('should catch validation errors when invalid matrix-asset-uri is provided with invalid other squiz primitive types ', () => {
|
1454
|
-
const schema = {
|
1455
|
-
type: 'object',
|
1456
|
-
properties: {
|
1457
|
-
image: { type: 'SquizImage' },
|
1458
|
-
link: { type: 'SquizLink' },
|
1459
|
-
text: { type: 'FormattedText' },
|
1460
|
-
asset: {
|
1461
|
-
type: 'string',
|
1462
|
-
format: 'matrix-asset-uri',
|
1463
|
-
},
|
1464
|
-
},
|
1465
|
-
required: ['image', 'link', 'text', 'asset'],
|
1466
|
-
};
|
1467
|
-
const formattedText = [
|
1468
|
-
//@ts-expect-error - wrong type
|
1469
|
-
{
|
1470
|
-
children: [{ type: 'text', value: 'hello' }],
|
1471
|
-
},
|
1472
|
-
];
|
1473
|
-
const input = {
|
1474
|
-
image: {
|
1475
|
-
name: 'test-image.jpeg',
|
1476
|
-
imageVariations: {
|
1477
|
-
//@ts-expect-error - wrong type
|
1478
|
-
original: {
|
1479
|
-
width: 100,
|
1480
|
-
url: 'https://www.squiz.net',
|
1481
|
-
byteSize: 100,
|
1482
|
-
mimeType: 'image/jpeg',
|
1483
|
-
sha1Hash: '123',
|
1484
|
-
},
|
1485
|
-
},
|
1486
|
-
},
|
1487
|
-
//@ts-expect-error - wrong type
|
1488
|
-
link: {
|
1489
|
-
text: 'test-link',
|
1490
|
-
target: '_blank',
|
1491
|
-
},
|
1492
|
-
text: formattedText,
|
1493
|
-
// @ts-expect-error - wrong type
|
1494
|
-
asset: 'matrix://123',
|
1495
|
-
};
|
1496
|
-
expectToThrowErrorMatchingTypeAndMessage(() => jsonSchemaService.validateInput(input, schema), SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}` in `#/image` does not match any given oneof schema,\nValue `{"text":"test-link","target":"_blank"}` in `#/link` does not match any given oneof schema,\nValue `[{"children":[{"type":"text","value":"hello"}]}]` in `#/text` does not match any given oneof schema,\nValue matrix-asset-uri (matrix://123) in `#/asset` is not a valid matrix asset uri', {
|
1497
|
-
'#/asset': [
|
1498
|
-
{
|
1499
|
-
data: {
|
1500
|
-
errors: {
|
1501
|
-
assetId: {
|
1502
|
-
data: { expected: /^\d+(?::.+)?$/, received: '' },
|
1503
|
-
message: 'Matrix Asset Id has invalid format, must match /^d+(?::.+)?$/',
|
1504
|
-
},
|
1505
|
-
scheme: {
|
1506
|
-
data: { expected: 'matrix-asset', received: 'matrix' },
|
1507
|
-
message: 'Uri scheme is invalid, must match "matrix-asset"',
|
1508
|
-
},
|
1509
|
-
},
|
1510
|
-
pointer: '#/asset',
|
1511
|
-
value: 'matrix://123',
|
1512
|
-
},
|
1513
|
-
message: 'Value matrix-asset-uri (matrix://123) in `#/asset` is not a valid matrix asset uri',
|
1514
|
-
},
|
1515
|
-
],
|
1516
|
-
'#/image': [
|
1517
|
-
{
|
1518
|
-
data: {
|
1519
|
-
errors: [
|
1520
|
-
{
|
1521
|
-
code: 'required-property-error',
|
1522
|
-
data: { key: 'height', pointer: '#/image/imageVariations/original' },
|
1523
|
-
message: 'The required property `height` is missing at `#/image/imageVariations/original`',
|
1524
|
-
name: 'RequiredPropertyError',
|
1525
|
-
type: 'error',
|
1526
|
-
},
|
1527
|
-
{
|
1528
|
-
code: 'required-property-error',
|
1529
|
-
data: { key: 'aspectRatio', pointer: '#/image/imageVariations/original' },
|
1530
|
-
message: 'The required property `aspectRatio` is missing at `#/image/imageVariations/original`',
|
1531
|
-
name: 'RequiredPropertyError',
|
1532
|
-
type: 'error',
|
1533
|
-
},
|
1534
|
-
],
|
1535
|
-
oneOf: [{ $ref: 'SquizImage.json' }],
|
1536
|
-
pointer: '#/image',
|
1537
|
-
value: '{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}',
|
1538
|
-
},
|
1539
|
-
message: 'Value `{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}` in `#/image` does not match any given oneof schema',
|
1540
|
-
},
|
1541
|
-
],
|
1542
|
-
'#/link': [
|
1543
|
-
{
|
1544
|
-
data: {
|
1545
|
-
errors: [
|
1546
|
-
{
|
1547
|
-
code: 'required-property-error',
|
1548
|
-
data: { key: 'url', pointer: '#/link' },
|
1549
|
-
message: 'The required property `url` is missing at `#/link`',
|
1550
|
-
name: 'RequiredPropertyError',
|
1551
|
-
type: 'error',
|
1552
|
-
},
|
1553
|
-
],
|
1554
|
-
oneOf: [{ $ref: 'SquizLink.json' }],
|
1555
|
-
pointer: '#/link',
|
1556
|
-
value: '{"text":"test-link","target":"_blank"}',
|
1557
|
-
},
|
1558
|
-
message: 'Value `{"text":"test-link","target":"_blank"}` in `#/link` does not match any given oneof schema',
|
1559
|
-
},
|
1560
|
-
],
|
1561
|
-
'#/text': [
|
1562
|
-
{
|
1563
|
-
data: {
|
1564
|
-
errors: [
|
1565
|
-
{
|
1566
|
-
code: 'any-of-error',
|
1567
|
-
data: {
|
1568
|
-
anyOf: [
|
1569
|
-
{ $ref: '#/definitions/HigherOrderFormattedNodes' },
|
1570
|
-
{ $ref: '#/definitions/BaseFormattedNodes' },
|
1571
|
-
],
|
1572
|
-
pointer: '#/text/0',
|
1573
|
-
value: { children: [{ type: 'text', value: 'hello' }] },
|
1574
|
-
},
|
1575
|
-
message: 'Object at `#/text/0` does not match any schema',
|
1576
|
-
name: 'AnyOfError',
|
1577
|
-
type: 'error',
|
1578
|
-
},
|
1579
|
-
],
|
1580
|
-
oneOf: [{ $ref: 'FormattedText.json' }],
|
1581
|
-
pointer: '#/text',
|
1582
|
-
value: '[{"children":[{"type":"text","value":"hello"}]}]',
|
1583
|
-
},
|
1584
|
-
message: 'Value `[{"children":[{"type":"text","value":"hello"}]}]` in `#/text` does not match any given oneof schema',
|
1585
|
-
},
|
1586
|
-
],
|
1587
|
-
});
|
1588
|
-
});
|
1589
|
-
it('should validate when MatrixAssetType is being used for a squiz primitive type with resolver', () => {
|
1590
|
-
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1591
|
-
.addPrimitive(primitiveTypes_1.SquizImageType)
|
1592
|
-
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1593
|
-
.addPrimitive(primitiveTypes_1.FormattedTextType)
|
1594
|
-
.addResolver(primitiveTypes_1.SquizImageType, resolvableTypes_1.MatrixAssetType, () => {
|
1595
|
-
return {
|
1596
|
-
name: '',
|
1597
|
-
imageVariations: {
|
1598
|
-
original: {
|
1599
|
-
width: 0,
|
1600
|
-
height: 0,
|
1601
|
-
url: '',
|
1602
|
-
mimeType: '',
|
1603
|
-
byteSize: 0,
|
1604
|
-
sha1Hash: '',
|
1605
|
-
aspectRatio: '',
|
1606
|
-
},
|
1607
|
-
},
|
1608
|
-
};
|
1609
|
-
})
|
1610
|
-
.build();
|
1611
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1612
|
-
const schema = {
|
1613
|
-
type: 'object',
|
1614
|
-
properties: {
|
1615
|
-
image: { type: 'SquizImage' },
|
1616
|
-
link: { type: 'SquizLink' },
|
1617
|
-
text: { type: 'FormattedText' },
|
1618
|
-
asset: {
|
1619
|
-
type: 'string',
|
1620
|
-
format: 'matrix-asset-uri',
|
1621
|
-
},
|
1622
|
-
},
|
1623
|
-
required: ['image', 'link', 'text', 'asset'],
|
1624
|
-
};
|
1625
|
-
const formattedText = [
|
1626
|
-
{
|
1627
|
-
tag: 'p',
|
1628
|
-
type: 'tag',
|
1629
|
-
children: [{ type: 'text', value: 'hello' }],
|
1630
|
-
},
|
1631
|
-
];
|
1632
|
-
const input = {
|
1633
|
-
image: {
|
1634
|
-
matrixAssetId: '123',
|
1635
|
-
matrixIdentifier: 'identifier',
|
1636
|
-
matrixDomain: 'domain',
|
1637
|
-
},
|
1638
|
-
link: {
|
1639
|
-
text: 'test-link',
|
1640
|
-
url: 'https://www.squiz.net',
|
1641
|
-
target: '_blank',
|
1642
|
-
},
|
1643
|
-
text: formattedText,
|
1644
|
-
asset: 'matrix-asset://identifier/123',
|
1645
|
-
};
|
1646
|
-
expect(jsonSchemaService.validateInput(input, schema)).toEqual(true);
|
1647
|
-
});
|
1648
|
-
it('it should catch MatrixAssetType validation errors when being use for squiz primitive type with resolver', () => {
|
1649
|
-
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1650
|
-
.addPrimitive(primitiveTypes_1.SquizImageType)
|
1651
|
-
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1652
|
-
.addPrimitive(primitiveTypes_1.FormattedTextType)
|
1653
|
-
.addResolver(primitiveTypes_1.SquizImageType, resolvableTypes_1.MatrixAssetType, () => {
|
1654
|
-
return {
|
1655
|
-
name: '',
|
1656
|
-
imageVariations: {
|
1657
|
-
original: {
|
1658
|
-
width: 0,
|
1659
|
-
height: 0,
|
1660
|
-
url: '',
|
1661
|
-
mimeType: '',
|
1662
|
-
byteSize: 0,
|
1663
|
-
sha1Hash: '',
|
1664
|
-
aspectRatio: '',
|
1665
|
-
},
|
1666
|
-
},
|
1667
|
-
};
|
1668
|
-
})
|
1669
|
-
.build();
|
1670
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1671
|
-
const schema = {
|
1672
|
-
type: 'object',
|
1673
|
-
properties: {
|
1674
|
-
image: { type: 'SquizImage' },
|
1675
|
-
},
|
1676
|
-
required: ['image'],
|
1677
|
-
};
|
1678
|
-
const input = {
|
1679
|
-
//@ts-expect-error - intentionally invalid input
|
1680
|
-
image: {
|
1681
|
-
matrixIdentifier: 'identifier',
|
1682
|
-
},
|
1683
|
-
};
|
1684
|
-
expectToThrowErrorMatchingTypeAndMessage(() => jsonSchemaService.validateInput(input, schema), SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `{"matrixIdentifier":"identifier"}` in `#/image` does not match any given oneof schema', {
|
1685
|
-
'#/image': [
|
1686
|
-
{
|
1687
|
-
data: {
|
1688
|
-
errors: [
|
1689
|
-
{
|
1690
|
-
code: 'required-property-error',
|
1691
|
-
data: { key: 'name', pointer: '#/image' },
|
1692
|
-
message: 'The required property `name` is missing at `#/image`',
|
1693
|
-
name: 'RequiredPropertyError',
|
1694
|
-
type: 'error',
|
1695
|
-
},
|
1696
|
-
{
|
1697
|
-
code: 'required-property-error',
|
1698
|
-
data: { key: 'imageVariations', pointer: '#/image' },
|
1699
|
-
message: 'The required property `imageVariations` is missing at `#/image`',
|
1700
|
-
name: 'RequiredPropertyError',
|
1701
|
-
type: 'error',
|
1702
|
-
},
|
1703
|
-
{
|
1704
|
-
code: 'required-property-error',
|
1705
|
-
data: { key: 'matrixAssetId', pointer: '#/image' },
|
1706
|
-
message: 'The required property `matrixAssetId` is missing at `#/image`',
|
1707
|
-
name: 'RequiredPropertyError',
|
1708
|
-
type: 'error',
|
1709
|
-
},
|
1710
|
-
],
|
1711
|
-
oneOf: [{ $ref: 'SquizImage.json' }, { $ref: 'MatrixAsset.json' }],
|
1712
|
-
pointer: '#/image',
|
1713
|
-
value: '{"matrixIdentifier":"identifier"}',
|
1714
|
-
},
|
1715
|
-
message: 'Value `{"matrixIdentifier":"identifier"}` in `#/image` does not match any given oneof schema',
|
1716
|
-
},
|
1717
|
-
],
|
1718
|
-
});
|
1719
|
-
});
|
1720
|
-
it('should only resolve an array of items containing resolvable types, other arrays are unaffected', async () => {
|
1721
|
-
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1722
|
-
.addPrimitive(primitiveTypes_1.SquizImageType)
|
1723
|
-
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1724
|
-
.addPrimitive(primitiveTypes_1.FormattedTextType)
|
1725
|
-
.addResolver(primitiveTypes_1.SquizImageType, resolvableTypes_1.MatrixAssetType, () => {
|
1726
|
-
return {
|
1727
|
-
name: '',
|
1728
|
-
imageVariations: {
|
1729
|
-
original: {
|
1730
|
-
width: 0,
|
1731
|
-
height: 0,
|
1732
|
-
url: '',
|
1733
|
-
mimeType: '',
|
1734
|
-
byteSize: 0,
|
1735
|
-
sha1Hash: '',
|
1736
|
-
aspectRatio: '',
|
1737
|
-
},
|
1738
|
-
},
|
1739
|
-
};
|
1740
|
-
})
|
1741
|
-
.addResolver(primitiveTypes_1.SquizLinkType, resolvableTypes_1.MatrixAssetLinkType, () => {
|
1742
|
-
return {
|
1743
|
-
text: 'link text',
|
1744
|
-
url: 'www.test.com',
|
1745
|
-
target: '_self',
|
1746
|
-
};
|
1747
|
-
})
|
1748
|
-
.build();
|
1749
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1750
|
-
const schema = {
|
1751
|
-
type: 'object',
|
1752
|
-
properties: {
|
1753
|
-
images: {
|
1754
|
-
description: 'Gallery images',
|
1755
|
-
type: 'array',
|
1756
|
-
items: {
|
1757
|
-
type: 'SquizImage',
|
1758
|
-
},
|
1759
|
-
},
|
1760
|
-
squizLink: {
|
1761
|
-
title: 'Squiz link',
|
1762
|
-
type: 'array',
|
1763
|
-
items: {
|
1764
|
-
type: 'SquizLink',
|
1765
|
-
},
|
1766
|
-
},
|
1767
|
-
//custom format
|
1768
|
-
multiLine: {
|
1769
|
-
title: 'Multi-line (textarea)',
|
1770
|
-
type: 'array',
|
1771
|
-
items: {
|
1772
|
-
type: 'string',
|
1773
|
-
format: 'multi-line',
|
1774
|
-
},
|
1775
|
-
},
|
1776
|
-
//string array
|
1777
|
-
listOfStrings: {
|
1778
|
-
type: 'array',
|
1779
|
-
title: 'A list of strings',
|
1780
|
-
items: {
|
1781
|
-
type: 'string',
|
1782
|
-
default: 'Holy smokes',
|
1783
|
-
},
|
1784
|
-
},
|
1785
|
-
},
|
1786
|
-
required: ['images', 'links'],
|
1787
|
-
};
|
1788
|
-
const input = {
|
1789
|
-
images: [
|
1790
|
-
{
|
1791
|
-
matrixDomain: 'https://feaas-page-render-us.dev.matrix.squiz.cloud/59',
|
1792
|
-
matrixAssetId: '160',
|
1793
|
-
matrixIdentifier: 'feaas-matrix-us',
|
1794
|
-
},
|
1795
|
-
],
|
1796
|
-
squizLink: [
|
1797
|
-
{
|
1798
|
-
matrixDomain: 'https://feaas-page-render-us.dev.matrix.squiz.cloud/59',
|
1799
|
-
matrixAssetId: '160',
|
1800
|
-
matrixIdentifier: 'feaas-matrix-us',
|
1801
|
-
target: '_self',
|
1802
|
-
},
|
1803
|
-
],
|
1804
|
-
multiline: ['wow', 'much', 'multiline'],
|
1805
|
-
listOfStrings: ['very', 'string'],
|
1806
|
-
};
|
1807
|
-
const result = await jsonSchemaService.resolveInput(input, schema);
|
1808
|
-
expect(result).toEqual({
|
1809
|
-
images: [
|
1810
|
-
{
|
1811
|
-
imageVariations: {
|
1812
|
-
original: { aspectRatio: '', byteSize: 0, height: 0, mimeType: '', sha1Hash: '', url: '', width: 0 },
|
1813
|
-
},
|
1814
|
-
name: '',
|
1815
|
-
},
|
1816
|
-
],
|
1817
|
-
squizLink: [
|
1818
|
-
{
|
1819
|
-
text: 'link text',
|
1820
|
-
url: 'www.test.com',
|
1821
|
-
target: '_self',
|
1822
|
-
},
|
1823
|
-
],
|
1824
|
-
multiline: ['wow', 'much', 'multiline'],
|
1825
|
-
listOfStrings: ['very', 'string'],
|
1826
|
-
});
|
1827
|
-
});
|
1828
|
-
it('should resolve multiple primitive type array items in multi-level nested array structures', async () => {
|
1829
|
-
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1830
|
-
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1831
|
-
.addResolver(primitiveTypes_1.SquizLinkType, resolvableTypes_1.MatrixAssetLinkType, (input) => {
|
1832
|
-
if ('matrixIdentifier' in input) {
|
1833
|
-
return {
|
1834
|
-
text: 'link text',
|
1835
|
-
url: `www.test.com/asset/${input.matrixAssetId}`,
|
1836
|
-
target: input.target,
|
1837
|
-
};
|
1838
|
-
}
|
1839
|
-
return input;
|
1840
|
-
})
|
1841
|
-
.build();
|
1842
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1843
|
-
const schema = {
|
1844
|
-
type: 'object',
|
1845
|
-
properties: {
|
1846
|
-
object1: {
|
1847
|
-
type: 'object',
|
1848
|
-
required: [],
|
1849
|
-
properties: {
|
1850
|
-
array1: {
|
1851
|
-
type: 'array',
|
1852
|
-
items: {
|
1853
|
-
type: 'object',
|
1854
|
-
properties: {
|
1855
|
-
linksArray: {
|
1856
|
-
type: 'array',
|
1857
|
-
items: {
|
1858
|
-
type: 'SquizLink',
|
1859
|
-
},
|
1860
|
-
},
|
1861
|
-
},
|
1862
|
-
},
|
1863
|
-
},
|
1864
|
-
},
|
1865
|
-
},
|
1866
|
-
},
|
1867
|
-
};
|
1868
|
-
const input = {
|
1869
|
-
object1: {
|
1870
|
-
array1: [
|
1871
|
-
{
|
1872
|
-
linksArray: [
|
1873
|
-
{
|
1874
|
-
url: '#LineOne',
|
1875
|
-
text: 'Link One',
|
1876
|
-
target: '_blank',
|
1877
|
-
},
|
1878
|
-
{
|
1879
|
-
url: '#LineTwo',
|
1880
|
-
text: 'Link Two',
|
1881
|
-
target: '_blank',
|
1882
|
-
},
|
1883
|
-
{
|
1884
|
-
matrixAssetId: '100',
|
1885
|
-
matrixDomain: 'my-matrix.squiz.net',
|
1886
|
-
matrixIdentifier: 'my-matrix-identifier',
|
1887
|
-
target: '_blank',
|
1888
|
-
},
|
1889
|
-
],
|
1890
|
-
},
|
1891
|
-
],
|
1892
|
-
},
|
1893
|
-
};
|
1894
|
-
const result = await jsonSchemaService.resolveInput(input, schema);
|
1895
|
-
expect(result).toEqual({
|
1896
|
-
object1: {
|
1897
|
-
array1: [
|
1898
|
-
{
|
1899
|
-
linksArray: [
|
1900
|
-
{
|
1901
|
-
url: '#LineOne',
|
1902
|
-
text: 'Link One',
|
1903
|
-
target: '_blank',
|
1904
|
-
},
|
1905
|
-
{
|
1906
|
-
url: '#LineTwo',
|
1907
|
-
text: 'Link Two',
|
1908
|
-
target: '_blank',
|
1909
|
-
},
|
1910
|
-
{
|
1911
|
-
url: 'www.test.com/asset/100',
|
1912
|
-
text: 'link text',
|
1913
|
-
target: '_blank',
|
1914
|
-
},
|
1915
|
-
],
|
1916
|
-
},
|
1917
|
-
],
|
1918
|
-
},
|
1919
|
-
});
|
1920
|
-
});
|
1921
|
-
it('should not use the resolver for primitive items in a resolvable array', async () => {
|
1922
|
-
const mockMatrixAssetResolver = jest.fn((input) => {
|
1923
|
-
return {
|
1924
|
-
name: input.matrixAssetId,
|
1925
|
-
imageVariations: {
|
1926
|
-
original: {
|
1927
|
-
width: 0,
|
1928
|
-
height: 0,
|
1929
|
-
url: '',
|
1930
|
-
mimeType: '',
|
1931
|
-
byteSize: 0,
|
1932
|
-
sha1Hash: '',
|
1933
|
-
aspectRatio: '',
|
1934
|
-
},
|
1935
|
-
},
|
1936
|
-
};
|
1937
|
-
});
|
1938
|
-
const mockMatrixLinkResolver = jest.fn((input) => {
|
1939
|
-
return {
|
1940
|
-
text: input.matrixAssetId,
|
1941
|
-
url: 'www.test.com',
|
1942
|
-
target: '_self',
|
1943
|
-
};
|
1944
|
-
});
|
1945
|
-
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1946
|
-
.addPrimitive(primitiveTypes_1.SquizImageType)
|
1947
|
-
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1948
|
-
.addPrimitive(primitiveTypes_1.FormattedTextType)
|
1949
|
-
.addResolver(primitiveTypes_1.SquizImageType, resolvableTypes_1.MatrixAssetType, mockMatrixAssetResolver)
|
1950
|
-
.addResolver(primitiveTypes_1.SquizLinkType, resolvableTypes_1.MatrixAssetLinkType, mockMatrixLinkResolver)
|
1951
|
-
.build();
|
1952
|
-
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1953
|
-
const schema = {
|
1954
|
-
type: 'object',
|
1955
|
-
properties: {
|
1956
|
-
images: {
|
1957
|
-
description: 'Gallery images',
|
1958
|
-
type: 'array',
|
1959
|
-
items: {
|
1960
|
-
type: 'SquizImage',
|
1961
|
-
},
|
1962
|
-
},
|
1963
|
-
squizLink: {
|
1964
|
-
title: 'Squiz link',
|
1965
|
-
type: 'array',
|
1966
|
-
items: {
|
1967
|
-
type: 'SquizLink',
|
1968
|
-
},
|
1969
|
-
},
|
1970
|
-
//custom format
|
1971
|
-
multiLine: {
|
1972
|
-
title: 'Multi-line (textarea)',
|
1973
|
-
type: 'array',
|
1974
|
-
items: {
|
1975
|
-
type: 'string',
|
1976
|
-
format: 'multi-line',
|
1977
|
-
},
|
1978
|
-
},
|
1979
|
-
//string array
|
1980
|
-
listOfStrings: {
|
1981
|
-
type: 'array',
|
1982
|
-
title: 'A list of strings',
|
1983
|
-
items: {
|
1984
|
-
type: 'string',
|
1985
|
-
default: 'Holy smokes',
|
1986
|
-
},
|
1987
|
-
},
|
1988
|
-
},
|
1989
|
-
required: ['images', 'links'],
|
1990
|
-
};
|
1991
|
-
const input = {
|
1992
|
-
images: [
|
1993
|
-
{
|
1994
|
-
matrixDomain: 'https://feaas-page-render-us.dev.matrix.squiz.cloud/59',
|
1995
|
-
matrixAssetId: '160',
|
1996
|
-
matrixIdentifier: 'feaas-matrix-us',
|
1997
|
-
},
|
1998
|
-
{
|
1999
|
-
imageVariations: {
|
2000
|
-
original: { aspectRatio: '', byteSize: 0, height: 0, mimeType: '', sha1Hash: '', url: '', width: 0 },
|
2001
|
-
},
|
2002
|
-
name: '',
|
2003
|
-
},
|
2004
|
-
],
|
2005
|
-
squizLink: [
|
2006
|
-
{
|
2007
|
-
matrixDomain: 'https://feaas-page-render-us.dev.matrix.squiz.cloud/59',
|
2008
|
-
matrixAssetId: '160',
|
2009
|
-
matrixIdentifier: 'feaas-matrix-us',
|
2010
|
-
target: '_self',
|
2011
|
-
},
|
2012
|
-
{
|
2013
|
-
text: 'link text',
|
2014
|
-
url: 'www.test.com',
|
2015
|
-
target: '_self',
|
2016
|
-
},
|
2017
|
-
],
|
2018
|
-
multiline: ['wow', 'much', 'multiline'],
|
2019
|
-
listOfStrings: ['very', 'string'],
|
2020
|
-
};
|
2021
|
-
const result = await jsonSchemaService.resolveInput(input, schema);
|
2022
|
-
expect(result).toEqual({
|
2023
|
-
images: [
|
2024
|
-
{
|
2025
|
-
imageVariations: {
|
2026
|
-
original: { aspectRatio: '', byteSize: 0, height: 0, mimeType: '', sha1Hash: '', url: '', width: 0 },
|
2027
|
-
},
|
2028
|
-
name: input.images[0].matrixAssetId,
|
2029
|
-
},
|
2030
|
-
{
|
2031
|
-
imageVariations: {
|
2032
|
-
original: { aspectRatio: '', byteSize: 0, height: 0, mimeType: '', sha1Hash: '', url: '', width: 0 },
|
2033
|
-
},
|
2034
|
-
name: '',
|
2035
|
-
},
|
2036
|
-
],
|
2037
|
-
squizLink: [
|
2038
|
-
{
|
2039
|
-
text: input.squizLink[0].matrixAssetId,
|
2040
|
-
url: 'www.test.com',
|
2041
|
-
target: '_self',
|
2042
|
-
},
|
2043
|
-
{
|
2044
|
-
text: 'link text',
|
2045
|
-
url: 'www.test.com',
|
2046
|
-
target: '_self',
|
2047
|
-
},
|
2048
|
-
],
|
2049
|
-
multiline: ['wow', 'much', 'multiline'],
|
2050
|
-
listOfStrings: ['very', 'string'],
|
2051
|
-
});
|
2052
|
-
expect(mockMatrixAssetResolver).toHaveBeenCalledTimes(1);
|
2053
|
-
expect(mockMatrixLinkResolver).toHaveBeenCalledTimes(1);
|
2054
|
-
});
|
2055
|
-
});
|
2056
275
|
//# sourceMappingURL=JsonValidationService.spec.js.map
|