@squiz/dx-json-schema-lib 1.34.1-alpha.0 → 1.34.1-alpha.10
Sign up to get free protection for your applications and to get access to all the features.
- package/.npm/_logs/{2023-05-17T04_23_12_814Z-debug-0.log → 2023-05-31T03_00_42_068Z-debug-0.log} +16 -16
- package/jsonCompiler.ts +5 -1
- package/lib/JsonValidationService.d.ts +3 -1
- package/lib/JsonValidationService.js +6 -1
- package/lib/JsonValidationService.js.map +1 -1
- package/lib/JsonValidationService.spec.js +604 -28
- package/lib/JsonValidationService.spec.js.map +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/pageContents/pageContentsModels.d.ts +1 -0
- package/lib/pageContents/pageContentsModels.js +28 -0
- package/lib/pageContents/pageContentsModels.js.map +1 -0
- package/lib/pageContents/pageContentsSchemas.d.ts +2 -0
- package/lib/pageContents/pageContentsSchemas.js +9 -0
- package/lib/pageContents/pageContentsSchemas.js.map +1 -0
- package/lib/pageContents/v1/pageContents.d.ts +45 -0
- package/lib/pageContents/v1/pageContents.js +9 -0
- package/lib/pageContents/v1/pageContents.js.map +1 -0
- package/lib/pageContents/v1/pageContents.json +127 -0
- package/lib/validators/customFormatValidators.js +21 -6
- package/lib/validators/customFormatValidators.js.map +1 -1
- package/package.json +2 -2
- package/src/JsonValidationService.spec.ts +686 -29
- package/src/JsonValidationService.ts +7 -0
- package/src/index.ts +3 -0
- package/src/pageContents/pageContentsModels.ts +1 -0
- package/src/pageContents/pageContentsSchemas.ts +3 -0
- package/src/pageContents/v1/pageContents.json +127 -0
- package/src/pageContents/v1/pageContents.ts +52 -0
- package/src/validators/customFormatValidators.ts +22 -10
- package/tsconfig.tsbuildinfo +1 -1
@@ -13,7 +13,11 @@ import {
|
|
13
13
|
ResolvableType,
|
14
14
|
TypeResolver,
|
15
15
|
} from './jsonTypeResolution/TypeResolver';
|
16
|
-
import { FormattedTextType, SquizImageType } from './primitiveTypes';
|
16
|
+
import { FormattedTextType, SquizImageType, SquizLinkType } from './primitiveTypes';
|
17
|
+
import { TypeResolverBuilder } from './jsonTypeResolution/TypeResolverBuilder';
|
18
|
+
import { MatrixAssetUri } from './validators/utils/matrixAssetValidator';
|
19
|
+
import { MatrixAssetType } from './resolvableTypes';
|
20
|
+
import { PageContents } from './pageContents/v1/pageContents';
|
17
21
|
|
18
22
|
function expectToThrowErrorMatchingTypeAndMessage(
|
19
23
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
@@ -153,22 +157,32 @@ describe('JsonValidationService', () => {
|
|
153
157
|
});
|
154
158
|
},
|
155
159
|
SchemaValidationError,
|
156
|
-
|
160
|
+
'failed validation: Value matrix-asset-uri (not-valid://canary uat matrix.squiz.cloud//abc123) in `#/matrix-asset` is not a valid matrix asset uri',
|
157
161
|
{
|
158
|
-
'#/matrix-asset':
|
159
|
-
|
160
|
-
data: {
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
162
|
+
'#/matrix-asset': [
|
163
|
+
{
|
164
|
+
data: {
|
165
|
+
errors: {
|
166
|
+
assetId: {
|
167
|
+
data: { expected: /^\d+(?::.+)?$/, received: '/abc123' },
|
168
|
+
message: 'Matrix Asset Id has invalid format, must match /^d+(?::.+)?$/',
|
169
|
+
},
|
170
|
+
identifier: {
|
171
|
+
data: { expected: /^[a-zA-Z0-9.-]+$/, received: 'canary uat matrix.squiz.cloud' },
|
172
|
+
message: 'Matrix Identifier has invalid format, must match /^[a-zA-Z0-9.-]+$/',
|
173
|
+
},
|
174
|
+
scheme: {
|
175
|
+
data: { expected: 'matrix-asset', received: 'not-valid' },
|
176
|
+
message: 'Uri scheme is invalid, must match "matrix-asset"',
|
177
|
+
},
|
178
|
+
},
|
179
|
+
pointer: '#/matrix-asset',
|
180
|
+
value: 'not-valid://canary uat matrix.squiz.cloud//abc123',
|
181
|
+
},
|
182
|
+
message:
|
183
|
+
'Value matrix-asset-uri (not-valid://canary uat matrix.squiz.cloud//abc123) in `#/matrix-asset` is not a valid matrix asset uri',
|
170
184
|
},
|
171
|
-
|
185
|
+
],
|
172
186
|
},
|
173
187
|
);
|
174
188
|
});
|
@@ -201,22 +215,31 @@ describe('JsonValidationService', () => {
|
|
201
215
|
});
|
202
216
|
},
|
203
217
|
SchemaValidationError,
|
204
|
-
|
218
|
+
'failed validation: Value matrix-asset-uri (matrix://) in `#/matrix-asset` is not a valid matrix asset uri',
|
205
219
|
{
|
206
|
-
'#/matrix-asset':
|
207
|
-
|
208
|
-
data: {
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
220
|
+
'#/matrix-asset': [
|
221
|
+
{
|
222
|
+
data: {
|
223
|
+
errors: {
|
224
|
+
assetId: {
|
225
|
+
data: { expected: /^\d+(?::.+)?$/, received: '' },
|
226
|
+
message: 'Matrix Asset Id has invalid format, must match /^d+(?::.+)?$/',
|
227
|
+
},
|
228
|
+
identifier: {
|
229
|
+
data: { expected: /^[a-zA-Z0-9.-]+$/, received: '' },
|
230
|
+
message: 'Matrix Identifier has invalid format, must match /^[a-zA-Z0-9.-]+$/',
|
231
|
+
},
|
232
|
+
scheme: {
|
233
|
+
data: { expected: 'matrix-asset', received: 'matrix' },
|
234
|
+
message: 'Uri scheme is invalid, must match "matrix-asset"',
|
235
|
+
},
|
236
|
+
},
|
237
|
+
pointer: '#/matrix-asset',
|
238
|
+
value: 'matrix://',
|
239
|
+
},
|
240
|
+
message: 'Value matrix-asset-uri (matrix://) in `#/matrix-asset` is not a valid matrix asset uri',
|
218
241
|
},
|
219
|
-
|
242
|
+
],
|
220
243
|
},
|
221
244
|
);
|
222
245
|
});
|
@@ -317,6 +340,101 @@ describe('JsonValidationService', () => {
|
|
317
340
|
expect(jsonValidationService.validateRenderInput(schema, otherValue)).toEqual(true);
|
318
341
|
});
|
319
342
|
});
|
343
|
+
|
344
|
+
describe('validatePageContents', () => {
|
345
|
+
const validPageContents: PageContents[] = [
|
346
|
+
{
|
347
|
+
containers: [],
|
348
|
+
},
|
349
|
+
{
|
350
|
+
containers: [
|
351
|
+
{
|
352
|
+
layout: {
|
353
|
+
name: 'chris-custom-2-col-layout',
|
354
|
+
type: 'inline',
|
355
|
+
value: '<div>[area://output/1]</div>',
|
356
|
+
},
|
357
|
+
items: [
|
358
|
+
{
|
359
|
+
type: 'component',
|
360
|
+
content: 'content-store://asdfkjashdfkjhwek234j234',
|
361
|
+
component: 'namespace/componentname/version',
|
362
|
+
layout_area: 1,
|
363
|
+
},
|
364
|
+
],
|
365
|
+
},
|
366
|
+
],
|
367
|
+
},
|
368
|
+
];
|
369
|
+
it.each(validPageContents)('should validate valid page contents', (input) => {
|
370
|
+
expect(jsonValidationService.validatePageContents(input)).toEqual(true);
|
371
|
+
});
|
372
|
+
|
373
|
+
it('should throw error for empty page contents', () => {
|
374
|
+
const input = {};
|
375
|
+
expectToThrowErrorMatchingTypeAndMessage(
|
376
|
+
() => {
|
377
|
+
jsonValidationService.validatePageContents(input);
|
378
|
+
},
|
379
|
+
SchemaValidationError,
|
380
|
+
'failed validation: The required property `containers` is missing at `#`',
|
381
|
+
{
|
382
|
+
'#': [
|
383
|
+
{
|
384
|
+
data: { key: 'containers', pointer: '#' },
|
385
|
+
message: 'The required property `containers` is missing at `#`',
|
386
|
+
},
|
387
|
+
],
|
388
|
+
},
|
389
|
+
);
|
390
|
+
});
|
391
|
+
|
392
|
+
it('should throw error for invalid page contents with multiple errors', () => {
|
393
|
+
const input = {
|
394
|
+
containers: [
|
395
|
+
{
|
396
|
+
layout: {
|
397
|
+
name: 'chris-custom-2-col-layout',
|
398
|
+
type: 'inline',
|
399
|
+
value: 123,
|
400
|
+
},
|
401
|
+
items: [
|
402
|
+
{
|
403
|
+
type: 'bad-type',
|
404
|
+
content: 'content-store://asdfkjashdfkjhwek234j234',
|
405
|
+
component: 'namespace/componentname/version',
|
406
|
+
layout_area: 1,
|
407
|
+
},
|
408
|
+
],
|
409
|
+
},
|
410
|
+
],
|
411
|
+
};
|
412
|
+
expectToThrowErrorMatchingTypeAndMessage(
|
413
|
+
() => {
|
414
|
+
jsonValidationService.validatePageContents(input);
|
415
|
+
},
|
416
|
+
SchemaValidationError,
|
417
|
+
'failed validation: Expected `123` (number) in `#/containers/0/layout/value` to be of type `string`,' +
|
418
|
+
'\n' +
|
419
|
+
'Expected given value `bad-type` in #/containers/0/items/0/type` to be one of `[component, rich-text]`',
|
420
|
+
{
|
421
|
+
'#/containers/0/items/0/type': [
|
422
|
+
{
|
423
|
+
data: { pointer: '#/containers/0/items/0/type', value: 'bad-type', values: ['component', 'rich-text'] },
|
424
|
+
message:
|
425
|
+
'Expected given value `bad-type` in #/containers/0/items/0/type` to be one of `[component, rich-text]`',
|
426
|
+
},
|
427
|
+
],
|
428
|
+
'#/containers/0/layout/value': [
|
429
|
+
{
|
430
|
+
data: { pointer: '#/containers/0/layout/value', expected: 'string', received: 'number', value: 123 },
|
431
|
+
message: 'Expected `123` (number) in `#/containers/0/layout/value` to be of type `string`',
|
432
|
+
},
|
433
|
+
],
|
434
|
+
},
|
435
|
+
);
|
436
|
+
});
|
437
|
+
});
|
320
438
|
});
|
321
439
|
|
322
440
|
const defaultSchema: JSONSchema = {
|
@@ -1374,3 +1492,542 @@ describe('JsonSchemaService', () => {
|
|
1374
1492
|
});
|
1375
1493
|
});
|
1376
1494
|
});
|
1495
|
+
|
1496
|
+
describe('JSONSchemaService - validation', () => {
|
1497
|
+
const typeResolver = TypeResolverBuilder.new()
|
1498
|
+
.addPrimitive(SquizImageType)
|
1499
|
+
.addPrimitive(SquizLinkType)
|
1500
|
+
.addPrimitive(FormattedTextType)
|
1501
|
+
.build();
|
1502
|
+
|
1503
|
+
const jsonSchemaService = new JSONSchemaService(typeResolver, ComponentInputMetaSchema);
|
1504
|
+
|
1505
|
+
it('should validate a schema with all the squiz primitive types', () => {
|
1506
|
+
const schema = {
|
1507
|
+
type: 'object',
|
1508
|
+
properties: {
|
1509
|
+
image: { type: 'SquizImage' },
|
1510
|
+
link: { type: 'SquizLink' },
|
1511
|
+
text: { type: 'FormattedText' },
|
1512
|
+
},
|
1513
|
+
required: ['image', 'link', 'text'],
|
1514
|
+
};
|
1515
|
+
const formattedText: FormattedText = [
|
1516
|
+
{
|
1517
|
+
tag: 'p',
|
1518
|
+
type: 'tag',
|
1519
|
+
children: [{ type: 'text', value: 'hello' }],
|
1520
|
+
},
|
1521
|
+
];
|
1522
|
+
const input: {
|
1523
|
+
image: SquizImageType['__shape__'];
|
1524
|
+
link: SquizLinkType['__shape__'];
|
1525
|
+
text: FormattedText;
|
1526
|
+
} = {
|
1527
|
+
image: {
|
1528
|
+
name: 'test-image.jpeg',
|
1529
|
+
imageVariations: {
|
1530
|
+
original: {
|
1531
|
+
aspectRatio: '1:1',
|
1532
|
+
height: 100,
|
1533
|
+
width: 100,
|
1534
|
+
url: 'https://www.squiz.net',
|
1535
|
+
byteSize: 100,
|
1536
|
+
mimeType: 'image/jpeg',
|
1537
|
+
sha1Hash: '123',
|
1538
|
+
},
|
1539
|
+
},
|
1540
|
+
},
|
1541
|
+
link: {
|
1542
|
+
name: 'test-link',
|
1543
|
+
url: 'https://www.squiz.net',
|
1544
|
+
target: '_blank',
|
1545
|
+
},
|
1546
|
+
text: formattedText,
|
1547
|
+
};
|
1548
|
+
|
1549
|
+
const result = jsonSchemaService.validateInput(input, schema);
|
1550
|
+
|
1551
|
+
expect(result).toEqual(true);
|
1552
|
+
});
|
1553
|
+
|
1554
|
+
it('should validate a schema with all the squiz primitive types and matrix-asset-uri format', () => {
|
1555
|
+
const schema = {
|
1556
|
+
type: 'object',
|
1557
|
+
properties: {
|
1558
|
+
image: { type: 'SquizImage' },
|
1559
|
+
link: { type: 'SquizLink' },
|
1560
|
+
text: { type: 'FormattedText' },
|
1561
|
+
asset: {
|
1562
|
+
type: 'string',
|
1563
|
+
format: 'matrix-asset-uri',
|
1564
|
+
},
|
1565
|
+
},
|
1566
|
+
required: ['image', 'link', 'text', 'asset'],
|
1567
|
+
};
|
1568
|
+
const formattedText: FormattedText = [
|
1569
|
+
{
|
1570
|
+
tag: 'p',
|
1571
|
+
type: 'tag',
|
1572
|
+
children: [{ type: 'text', value: 'hello' }],
|
1573
|
+
},
|
1574
|
+
];
|
1575
|
+
const input: {
|
1576
|
+
image: SquizImageType['__shape__'];
|
1577
|
+
link: SquizLinkType['__shape__'];
|
1578
|
+
text: FormattedText;
|
1579
|
+
asset: MatrixAssetUri;
|
1580
|
+
} = {
|
1581
|
+
image: {
|
1582
|
+
name: 'test-image.jpeg',
|
1583
|
+
imageVariations: {
|
1584
|
+
original: {
|
1585
|
+
aspectRatio: '1:1',
|
1586
|
+
height: 100,
|
1587
|
+
width: 100,
|
1588
|
+
url: 'https://www.squiz.net',
|
1589
|
+
byteSize: 100,
|
1590
|
+
mimeType: 'image/jpeg',
|
1591
|
+
sha1Hash: '123',
|
1592
|
+
},
|
1593
|
+
},
|
1594
|
+
},
|
1595
|
+
link: {
|
1596
|
+
name: 'test-link',
|
1597
|
+
url: 'https://www.squiz.net',
|
1598
|
+
target: '_blank',
|
1599
|
+
},
|
1600
|
+
text: formattedText,
|
1601
|
+
asset: 'matrix-asset://identifier/123',
|
1602
|
+
};
|
1603
|
+
|
1604
|
+
const result = jsonSchemaService.validateInput(input, schema);
|
1605
|
+
|
1606
|
+
expect(result).toEqual(true);
|
1607
|
+
});
|
1608
|
+
|
1609
|
+
it('should catch validation errors when there is a schema with all the squiz primitive types', () => {
|
1610
|
+
const schema = {
|
1611
|
+
type: 'object',
|
1612
|
+
properties: {
|
1613
|
+
image: { type: 'SquizImage' },
|
1614
|
+
link: { type: 'SquizLink' },
|
1615
|
+
text: { type: 'FormattedText' },
|
1616
|
+
},
|
1617
|
+
required: ['image', 'link', 'text'],
|
1618
|
+
};
|
1619
|
+
const formattedText: FormattedText = [
|
1620
|
+
//@ts-expect-error - wrong type
|
1621
|
+
{
|
1622
|
+
children: [{ type: 'text', value: 'hello' }],
|
1623
|
+
},
|
1624
|
+
];
|
1625
|
+
const input: {
|
1626
|
+
image: SquizImageType['__shape__'];
|
1627
|
+
link: SquizLinkType['__shape__'];
|
1628
|
+
text: FormattedText;
|
1629
|
+
} = {
|
1630
|
+
image: {
|
1631
|
+
name: 'test-image.jpeg',
|
1632
|
+
imageVariations: {
|
1633
|
+
//@ts-expect-error - wrong type
|
1634
|
+
original: {
|
1635
|
+
width: 100,
|
1636
|
+
url: 'https://www.squiz.net',
|
1637
|
+
byteSize: 100,
|
1638
|
+
mimeType: 'image/jpeg',
|
1639
|
+
sha1Hash: '123',
|
1640
|
+
},
|
1641
|
+
},
|
1642
|
+
},
|
1643
|
+
//@ts-expect-error - wrong type
|
1644
|
+
link: {
|
1645
|
+
name: 'test-link',
|
1646
|
+
target: '_blank',
|
1647
|
+
},
|
1648
|
+
text: formattedText,
|
1649
|
+
};
|
1650
|
+
|
1651
|
+
expectToThrowErrorMatchingTypeAndMessage(
|
1652
|
+
() => jsonSchemaService.validateInput(input, schema),
|
1653
|
+
SchemaValidationError,
|
1654
|
+
'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 `{"name":"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',
|
1655
|
+
{
|
1656
|
+
'#/image': [
|
1657
|
+
{
|
1658
|
+
data: {
|
1659
|
+
errors: [
|
1660
|
+
{
|
1661
|
+
code: 'required-property-error',
|
1662
|
+
data: { key: 'height', pointer: '#/image/imageVariations/original' },
|
1663
|
+
message: 'The required property `height` is missing at `#/image/imageVariations/original`',
|
1664
|
+
name: 'RequiredPropertyError',
|
1665
|
+
type: 'error',
|
1666
|
+
},
|
1667
|
+
{
|
1668
|
+
code: 'required-property-error',
|
1669
|
+
data: { key: 'aspectRatio', pointer: '#/image/imageVariations/original' },
|
1670
|
+
message: 'The required property `aspectRatio` is missing at `#/image/imageVariations/original`',
|
1671
|
+
name: 'RequiredPropertyError',
|
1672
|
+
type: 'error',
|
1673
|
+
},
|
1674
|
+
],
|
1675
|
+
oneOf: [{ $ref: 'SquizImage.json' }],
|
1676
|
+
pointer: '#/image',
|
1677
|
+
value:
|
1678
|
+
'{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}',
|
1679
|
+
},
|
1680
|
+
message:
|
1681
|
+
'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',
|
1682
|
+
},
|
1683
|
+
],
|
1684
|
+
'#/link': [
|
1685
|
+
{
|
1686
|
+
data: {
|
1687
|
+
errors: [
|
1688
|
+
{
|
1689
|
+
code: 'required-property-error',
|
1690
|
+
data: { key: 'url', pointer: '#/link' },
|
1691
|
+
message: 'The required property `url` is missing at `#/link`',
|
1692
|
+
name: 'RequiredPropertyError',
|
1693
|
+
type: 'error',
|
1694
|
+
},
|
1695
|
+
],
|
1696
|
+
oneOf: [{ $ref: 'SquizLink.json' }],
|
1697
|
+
pointer: '#/link',
|
1698
|
+
value: '{"name":"test-link","target":"_blank"}',
|
1699
|
+
},
|
1700
|
+
message: 'Value `{"name":"test-link","target":"_blank"}` in `#/link` does not match any given oneof schema',
|
1701
|
+
},
|
1702
|
+
],
|
1703
|
+
'#/text': [
|
1704
|
+
{
|
1705
|
+
data: {
|
1706
|
+
errors: [
|
1707
|
+
{
|
1708
|
+
code: 'any-of-error',
|
1709
|
+
data: {
|
1710
|
+
anyOf: [
|
1711
|
+
{ $ref: '#/definitions/HigherOrderFormattedNodes' },
|
1712
|
+
{ $ref: '#/definitions/BaseFormattedNodes' },
|
1713
|
+
],
|
1714
|
+
pointer: '#/text/0',
|
1715
|
+
value: { children: [{ type: 'text', value: 'hello' }] },
|
1716
|
+
},
|
1717
|
+
message: 'Object at `#/text/0` does not match any schema',
|
1718
|
+
name: 'AnyOfError',
|
1719
|
+
type: 'error',
|
1720
|
+
},
|
1721
|
+
],
|
1722
|
+
oneOf: [{ $ref: 'FormattedText.json' }],
|
1723
|
+
pointer: '#/text',
|
1724
|
+
value: '[{"children":[{"type":"text","value":"hello"}]}]',
|
1725
|
+
},
|
1726
|
+
message:
|
1727
|
+
'Value `[{"children":[{"type":"text","value":"hello"}]}]` in `#/text` does not match any given oneof schema',
|
1728
|
+
},
|
1729
|
+
],
|
1730
|
+
},
|
1731
|
+
);
|
1732
|
+
});
|
1733
|
+
|
1734
|
+
it('should catch validation errors when invalid matrix-asset-uri is provided with invalid other squiz primitive types ', () => {
|
1735
|
+
const schema = {
|
1736
|
+
type: 'object',
|
1737
|
+
properties: {
|
1738
|
+
image: { type: 'SquizImage' },
|
1739
|
+
link: { type: 'SquizLink' },
|
1740
|
+
text: { type: 'FormattedText' },
|
1741
|
+
asset: {
|
1742
|
+
type: 'string',
|
1743
|
+
format: 'matrix-asset-uri',
|
1744
|
+
},
|
1745
|
+
},
|
1746
|
+
|
1747
|
+
required: ['image', 'link', 'text', 'asset'],
|
1748
|
+
};
|
1749
|
+
const formattedText: FormattedText = [
|
1750
|
+
//@ts-expect-error - wrong type
|
1751
|
+
{
|
1752
|
+
children: [{ type: 'text', value: 'hello' }],
|
1753
|
+
},
|
1754
|
+
];
|
1755
|
+
const input: {
|
1756
|
+
image: SquizImageType['__shape__'];
|
1757
|
+
link: SquizLinkType['__shape__'];
|
1758
|
+
text: FormattedText;
|
1759
|
+
asset: MatrixAssetUri;
|
1760
|
+
} = {
|
1761
|
+
image: {
|
1762
|
+
name: 'test-image.jpeg',
|
1763
|
+
imageVariations: {
|
1764
|
+
//@ts-expect-error - wrong type
|
1765
|
+
original: {
|
1766
|
+
width: 100,
|
1767
|
+
url: 'https://www.squiz.net',
|
1768
|
+
byteSize: 100,
|
1769
|
+
mimeType: 'image/jpeg',
|
1770
|
+
sha1Hash: '123',
|
1771
|
+
},
|
1772
|
+
},
|
1773
|
+
},
|
1774
|
+
//@ts-expect-error - wrong type
|
1775
|
+
link: {
|
1776
|
+
name: 'test-link',
|
1777
|
+
target: '_blank',
|
1778
|
+
},
|
1779
|
+
text: formattedText,
|
1780
|
+
// @ts-expect-error - wrong type
|
1781
|
+
asset: 'matrix://123',
|
1782
|
+
};
|
1783
|
+
|
1784
|
+
expectToThrowErrorMatchingTypeAndMessage(
|
1785
|
+
() => jsonSchemaService.validateInput(input, schema),
|
1786
|
+
SchemaValidationError,
|
1787
|
+
'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 `{"name":"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',
|
1788
|
+
{
|
1789
|
+
'#/asset': [
|
1790
|
+
{
|
1791
|
+
data: {
|
1792
|
+
errors: {
|
1793
|
+
assetId: {
|
1794
|
+
data: { expected: /^\d+(?::.+)?$/, received: '' },
|
1795
|
+
message: 'Matrix Asset Id has invalid format, must match /^d+(?::.+)?$/',
|
1796
|
+
},
|
1797
|
+
scheme: {
|
1798
|
+
data: { expected: 'matrix-asset', received: 'matrix' },
|
1799
|
+
message: 'Uri scheme is invalid, must match "matrix-asset"',
|
1800
|
+
},
|
1801
|
+
},
|
1802
|
+
pointer: '#/asset',
|
1803
|
+
value: 'matrix://123',
|
1804
|
+
},
|
1805
|
+
message: 'Value matrix-asset-uri (matrix://123) in `#/asset` is not a valid matrix asset uri',
|
1806
|
+
},
|
1807
|
+
],
|
1808
|
+
'#/image': [
|
1809
|
+
{
|
1810
|
+
data: {
|
1811
|
+
errors: [
|
1812
|
+
{
|
1813
|
+
code: 'required-property-error',
|
1814
|
+
data: { key: 'height', pointer: '#/image/imageVariations/original' },
|
1815
|
+
message: 'The required property `height` is missing at `#/image/imageVariations/original`',
|
1816
|
+
name: 'RequiredPropertyError',
|
1817
|
+
type: 'error',
|
1818
|
+
},
|
1819
|
+
{
|
1820
|
+
code: 'required-property-error',
|
1821
|
+
data: { key: 'aspectRatio', pointer: '#/image/imageVariations/original' },
|
1822
|
+
message: 'The required property `aspectRatio` is missing at `#/image/imageVariations/original`',
|
1823
|
+
name: 'RequiredPropertyError',
|
1824
|
+
type: 'error',
|
1825
|
+
},
|
1826
|
+
],
|
1827
|
+
oneOf: [{ $ref: 'SquizImage.json' }],
|
1828
|
+
pointer: '#/image',
|
1829
|
+
value:
|
1830
|
+
'{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}',
|
1831
|
+
},
|
1832
|
+
message:
|
1833
|
+
'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',
|
1834
|
+
},
|
1835
|
+
],
|
1836
|
+
'#/link': [
|
1837
|
+
{
|
1838
|
+
data: {
|
1839
|
+
errors: [
|
1840
|
+
{
|
1841
|
+
code: 'required-property-error',
|
1842
|
+
data: { key: 'url', pointer: '#/link' },
|
1843
|
+
message: 'The required property `url` is missing at `#/link`',
|
1844
|
+
name: 'RequiredPropertyError',
|
1845
|
+
type: 'error',
|
1846
|
+
},
|
1847
|
+
],
|
1848
|
+
oneOf: [{ $ref: 'SquizLink.json' }],
|
1849
|
+
pointer: '#/link',
|
1850
|
+
value: '{"name":"test-link","target":"_blank"}',
|
1851
|
+
},
|
1852
|
+
message: 'Value `{"name":"test-link","target":"_blank"}` in `#/link` does not match any given oneof schema',
|
1853
|
+
},
|
1854
|
+
],
|
1855
|
+
'#/text': [
|
1856
|
+
{
|
1857
|
+
data: {
|
1858
|
+
errors: [
|
1859
|
+
{
|
1860
|
+
code: 'any-of-error',
|
1861
|
+
data: {
|
1862
|
+
anyOf: [
|
1863
|
+
{ $ref: '#/definitions/HigherOrderFormattedNodes' },
|
1864
|
+
{ $ref: '#/definitions/BaseFormattedNodes' },
|
1865
|
+
],
|
1866
|
+
pointer: '#/text/0',
|
1867
|
+
value: { children: [{ type: 'text', value: 'hello' }] },
|
1868
|
+
},
|
1869
|
+
message: 'Object at `#/text/0` does not match any schema',
|
1870
|
+
name: 'AnyOfError',
|
1871
|
+
type: 'error',
|
1872
|
+
},
|
1873
|
+
],
|
1874
|
+
oneOf: [{ $ref: 'FormattedText.json' }],
|
1875
|
+
pointer: '#/text',
|
1876
|
+
value: '[{"children":[{"type":"text","value":"hello"}]}]',
|
1877
|
+
},
|
1878
|
+
message:
|
1879
|
+
'Value `[{"children":[{"type":"text","value":"hello"}]}]` in `#/text` does not match any given oneof schema',
|
1880
|
+
},
|
1881
|
+
],
|
1882
|
+
},
|
1883
|
+
);
|
1884
|
+
});
|
1885
|
+
|
1886
|
+
it('should validate when MatrixAssetType is being used for a squiz primitive type with resolver', () => {
|
1887
|
+
const typeResolver = TypeResolverBuilder.new()
|
1888
|
+
.addPrimitive(SquizImageType)
|
1889
|
+
.addPrimitive(SquizLinkType)
|
1890
|
+
.addPrimitive(FormattedTextType)
|
1891
|
+
.addResolver(SquizImageType, MatrixAssetType, (): SquizImageType['__shape__'] => {
|
1892
|
+
return {
|
1893
|
+
name: '',
|
1894
|
+
imageVariations: {
|
1895
|
+
original: {
|
1896
|
+
width: 0,
|
1897
|
+
height: 0,
|
1898
|
+
url: '',
|
1899
|
+
mimeType: '',
|
1900
|
+
byteSize: 0,
|
1901
|
+
sha1Hash: '',
|
1902
|
+
aspectRatio: '',
|
1903
|
+
},
|
1904
|
+
},
|
1905
|
+
};
|
1906
|
+
})
|
1907
|
+
.build();
|
1908
|
+
|
1909
|
+
const jsonSchemaService = new JSONSchemaService(typeResolver, ComponentInputMetaSchema);
|
1910
|
+
const schema = {
|
1911
|
+
type: 'object',
|
1912
|
+
properties: {
|
1913
|
+
image: { type: 'SquizImage' },
|
1914
|
+
link: { type: 'SquizLink' },
|
1915
|
+
text: { type: 'FormattedText' },
|
1916
|
+
asset: {
|
1917
|
+
type: 'string',
|
1918
|
+
format: 'matrix-asset-uri',
|
1919
|
+
},
|
1920
|
+
},
|
1921
|
+
required: ['image', 'link', 'text', 'asset'],
|
1922
|
+
};
|
1923
|
+
const formattedText: FormattedText = [
|
1924
|
+
{
|
1925
|
+
tag: 'p',
|
1926
|
+
type: 'tag',
|
1927
|
+
children: [{ type: 'text', value: 'hello' }],
|
1928
|
+
},
|
1929
|
+
];
|
1930
|
+
const input: {
|
1931
|
+
image: MatrixAssetType['__shape__'];
|
1932
|
+
link: SquizLinkType['__shape__'];
|
1933
|
+
text: FormattedText;
|
1934
|
+
asset: MatrixAssetUri;
|
1935
|
+
} = {
|
1936
|
+
image: {
|
1937
|
+
matrixAssetId: '123',
|
1938
|
+
matrixIdentifier: 'identifier',
|
1939
|
+
matrixDomain: 'domain',
|
1940
|
+
},
|
1941
|
+
link: {
|
1942
|
+
name: 'test-link',
|
1943
|
+
url: 'https://www.squiz.net',
|
1944
|
+
target: '_blank',
|
1945
|
+
},
|
1946
|
+
text: formattedText,
|
1947
|
+
asset: 'matrix-asset://identifier/123',
|
1948
|
+
};
|
1949
|
+
|
1950
|
+
expect(jsonSchemaService.validateInput(input, schema)).toEqual(true);
|
1951
|
+
});
|
1952
|
+
|
1953
|
+
it('it should catch MatrixAssetType validation errors when being use for squiz primitive type with resolver', () => {
|
1954
|
+
const typeResolver = TypeResolverBuilder.new()
|
1955
|
+
.addPrimitive(SquizImageType)
|
1956
|
+
.addPrimitive(SquizLinkType)
|
1957
|
+
.addPrimitive(FormattedTextType)
|
1958
|
+
.addResolver(SquizImageType, MatrixAssetType, (): SquizImageType['__shape__'] => {
|
1959
|
+
return {
|
1960
|
+
name: '',
|
1961
|
+
imageVariations: {
|
1962
|
+
original: {
|
1963
|
+
width: 0,
|
1964
|
+
height: 0,
|
1965
|
+
url: '',
|
1966
|
+
mimeType: '',
|
1967
|
+
byteSize: 0,
|
1968
|
+
sha1Hash: '',
|
1969
|
+
aspectRatio: '',
|
1970
|
+
},
|
1971
|
+
},
|
1972
|
+
};
|
1973
|
+
})
|
1974
|
+
.build();
|
1975
|
+
|
1976
|
+
const jsonSchemaService = new JSONSchemaService(typeResolver, ComponentInputMetaSchema);
|
1977
|
+
const schema = {
|
1978
|
+
type: 'object',
|
1979
|
+
properties: {
|
1980
|
+
image: { type: 'SquizImage' },
|
1981
|
+
},
|
1982
|
+
required: ['image'],
|
1983
|
+
};
|
1984
|
+
const input: {
|
1985
|
+
image: MatrixAssetType['__shape__'];
|
1986
|
+
} = {
|
1987
|
+
//@ts-expect-error - intentionally invalid input
|
1988
|
+
image: {
|
1989
|
+
matrixIdentifier: 'identifier',
|
1990
|
+
},
|
1991
|
+
};
|
1992
|
+
expectToThrowErrorMatchingTypeAndMessage(
|
1993
|
+
() => jsonSchemaService.validateInput(input, schema),
|
1994
|
+
SchemaValidationError,
|
1995
|
+
'failed validation: Value `{"matrixIdentifier":"identifier"}` in `#/image` does not match any given oneof schema',
|
1996
|
+
{
|
1997
|
+
'#/image': [
|
1998
|
+
{
|
1999
|
+
data: {
|
2000
|
+
errors: [
|
2001
|
+
{
|
2002
|
+
code: 'required-property-error',
|
2003
|
+
data: { key: 'name', pointer: '#/image' },
|
2004
|
+
message: 'The required property `name` is missing at `#/image`',
|
2005
|
+
name: 'RequiredPropertyError',
|
2006
|
+
type: 'error',
|
2007
|
+
},
|
2008
|
+
{
|
2009
|
+
code: 'required-property-error',
|
2010
|
+
data: { key: 'imageVariations', pointer: '#/image' },
|
2011
|
+
message: 'The required property `imageVariations` is missing at `#/image`',
|
2012
|
+
name: 'RequiredPropertyError',
|
2013
|
+
type: 'error',
|
2014
|
+
},
|
2015
|
+
{
|
2016
|
+
code: 'required-property-error',
|
2017
|
+
data: { key: 'matrixAssetId', pointer: '#/image' },
|
2018
|
+
message: 'The required property `matrixAssetId` is missing at `#/image`',
|
2019
|
+
name: 'RequiredPropertyError',
|
2020
|
+
type: 'error',
|
2021
|
+
},
|
2022
|
+
],
|
2023
|
+
oneOf: [{ $ref: 'SquizImage.json' }, { $ref: 'MatrixAsset.json' }],
|
2024
|
+
pointer: '#/image',
|
2025
|
+
value: '{"matrixIdentifier":"identifier"}',
|
2026
|
+
},
|
2027
|
+
message: 'Value `{"matrixIdentifier":"identifier"}` in `#/image` does not match any given oneof schema',
|
2028
|
+
},
|
2029
|
+
],
|
2030
|
+
},
|
2031
|
+
);
|
2032
|
+
});
|
2033
|
+
});
|