@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
@@ -10,6 +10,8 @@ const validComponentJson_json_1 = __importDefault(require("./manifest/v1/__test_
|
|
10
10
|
const inputStringWithFormat_json_1 = __importDefault(require("./manifest/v1/__test__/schemas/inputStringWithFormat.json"));
|
11
11
|
const TypeResolver_1 = require("./jsonTypeResolution/TypeResolver");
|
12
12
|
const primitiveTypes_1 = require("./primitiveTypes");
|
13
|
+
const TypeResolverBuilder_1 = require("./jsonTypeResolution/TypeResolverBuilder");
|
14
|
+
const resolvableTypes_1 = require("./resolvableTypes");
|
13
15
|
function expectToThrowErrorMatchingTypeAndMessage(
|
14
16
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
15
17
|
received,
|
@@ -111,21 +113,30 @@ describe('JsonValidationService', () => {
|
|
111
113
|
jsonValidationService.validateRenderInput(functionInputSchema, {
|
112
114
|
'matrix-asset': invalidMatrixAssetUri,
|
113
115
|
});
|
114
|
-
}, SchemaValidationError_1.SchemaValidationError,
|
115
|
-
'#/matrix-asset':
|
116
|
-
|
117
|
-
data: {
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
116
|
+
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Value matrix-asset-uri (not-valid://canary uat matrix.squiz.cloud//abc123) in `#/matrix-asset` is not a valid matrix asset uri', {
|
117
|
+
'#/matrix-asset': [
|
118
|
+
{
|
119
|
+
data: {
|
120
|
+
errors: {
|
121
|
+
assetId: {
|
122
|
+
data: { expected: /^\d+(?::.+)?$/, received: '/abc123' },
|
123
|
+
message: 'Matrix Asset Id has invalid format, must match /^d+(?::.+)?$/',
|
124
|
+
},
|
125
|
+
identifier: {
|
126
|
+
data: { expected: /^[a-zA-Z0-9.-]+$/, received: 'canary uat matrix.squiz.cloud' },
|
127
|
+
message: 'Matrix Identifier has invalid format, must match /^[a-zA-Z0-9.-]+$/',
|
128
|
+
},
|
129
|
+
scheme: {
|
130
|
+
data: { expected: 'matrix-asset', received: 'not-valid' },
|
131
|
+
message: 'Uri scheme is invalid, must match "matrix-asset"',
|
132
|
+
},
|
133
|
+
},
|
134
|
+
pointer: '#/matrix-asset',
|
135
|
+
value: 'not-valid://canary uat matrix.squiz.cloud//abc123',
|
136
|
+
},
|
137
|
+
message: 'Value matrix-asset-uri (not-valid://canary uat matrix.squiz.cloud//abc123) in `#/matrix-asset` is not a valid matrix asset uri',
|
127
138
|
},
|
128
|
-
|
139
|
+
],
|
129
140
|
});
|
130
141
|
});
|
131
142
|
it('should throw error for invalid matrix-asset-uri type number', () => {
|
@@ -149,21 +160,30 @@ describe('JsonValidationService', () => {
|
|
149
160
|
jsonValidationService.validateRenderInput(functionInputSchema, {
|
150
161
|
'matrix-asset': invalidMatrixAssetUri,
|
151
162
|
});
|
152
|
-
}, SchemaValidationError_1.SchemaValidationError,
|
153
|
-
'#/matrix-asset':
|
154
|
-
|
155
|
-
data: {
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
163
|
+
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Value matrix-asset-uri (matrix://) in `#/matrix-asset` is not a valid matrix asset uri', {
|
164
|
+
'#/matrix-asset': [
|
165
|
+
{
|
166
|
+
data: {
|
167
|
+
errors: {
|
168
|
+
assetId: {
|
169
|
+
data: { expected: /^\d+(?::.+)?$/, received: '' },
|
170
|
+
message: 'Matrix Asset Id has invalid format, must match /^d+(?::.+)?$/',
|
171
|
+
},
|
172
|
+
identifier: {
|
173
|
+
data: { expected: /^[a-zA-Z0-9.-]+$/, received: '' },
|
174
|
+
message: 'Matrix Identifier has invalid format, must match /^[a-zA-Z0-9.-]+$/',
|
175
|
+
},
|
176
|
+
scheme: {
|
177
|
+
data: { expected: 'matrix-asset', received: 'matrix' },
|
178
|
+
message: 'Uri scheme is invalid, must match "matrix-asset"',
|
179
|
+
},
|
180
|
+
},
|
181
|
+
pointer: '#/matrix-asset',
|
182
|
+
value: 'matrix://',
|
183
|
+
},
|
184
|
+
message: 'Value matrix-asset-uri (matrix://) in `#/matrix-asset` is not a valid matrix asset uri',
|
165
185
|
},
|
166
|
-
|
186
|
+
],
|
167
187
|
});
|
168
188
|
});
|
169
189
|
});
|
@@ -251,6 +271,87 @@ describe('JsonValidationService', () => {
|
|
251
271
|
expect(jsonValidationService.validateRenderInput(schema, otherValue)).toEqual(true);
|
252
272
|
});
|
253
273
|
});
|
274
|
+
describe('validatePageContents', () => {
|
275
|
+
const validPageContents = [
|
276
|
+
{
|
277
|
+
containers: [],
|
278
|
+
},
|
279
|
+
{
|
280
|
+
containers: [
|
281
|
+
{
|
282
|
+
layout: {
|
283
|
+
name: 'chris-custom-2-col-layout',
|
284
|
+
type: 'inline',
|
285
|
+
value: '<div>[area://output/1]</div>',
|
286
|
+
},
|
287
|
+
items: [
|
288
|
+
{
|
289
|
+
type: 'component',
|
290
|
+
content: 'content-store://asdfkjashdfkjhwek234j234',
|
291
|
+
component: 'namespace/componentname/version',
|
292
|
+
layout_area: 1,
|
293
|
+
},
|
294
|
+
],
|
295
|
+
},
|
296
|
+
],
|
297
|
+
},
|
298
|
+
];
|
299
|
+
it.each(validPageContents)('should validate valid page contents', (input) => {
|
300
|
+
expect(jsonValidationService.validatePageContents(input)).toEqual(true);
|
301
|
+
});
|
302
|
+
it('should throw error for empty page contents', () => {
|
303
|
+
const input = {};
|
304
|
+
expectToThrowErrorMatchingTypeAndMessage(() => {
|
305
|
+
jsonValidationService.validatePageContents(input);
|
306
|
+
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: The required property `containers` is missing at `#`', {
|
307
|
+
'#': [
|
308
|
+
{
|
309
|
+
data: { key: 'containers', pointer: '#' },
|
310
|
+
message: 'The required property `containers` is missing at `#`',
|
311
|
+
},
|
312
|
+
],
|
313
|
+
});
|
314
|
+
});
|
315
|
+
it('should throw error for invalid page contents with multiple errors', () => {
|
316
|
+
const input = {
|
317
|
+
containers: [
|
318
|
+
{
|
319
|
+
layout: {
|
320
|
+
name: 'chris-custom-2-col-layout',
|
321
|
+
type: 'inline',
|
322
|
+
value: 123,
|
323
|
+
},
|
324
|
+
items: [
|
325
|
+
{
|
326
|
+
type: 'bad-type',
|
327
|
+
content: 'content-store://asdfkjashdfkjhwek234j234',
|
328
|
+
component: 'namespace/componentname/version',
|
329
|
+
layout_area: 1,
|
330
|
+
},
|
331
|
+
],
|
332
|
+
},
|
333
|
+
],
|
334
|
+
};
|
335
|
+
expectToThrowErrorMatchingTypeAndMessage(() => {
|
336
|
+
jsonValidationService.validatePageContents(input);
|
337
|
+
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Expected `123` (number) in `#/containers/0/layout/value` to be of type `string`,' +
|
338
|
+
'\n' +
|
339
|
+
'Expected given value `bad-type` in #/containers/0/items/0/type` to be one of `[component, rich-text]`', {
|
340
|
+
'#/containers/0/items/0/type': [
|
341
|
+
{
|
342
|
+
data: { pointer: '#/containers/0/items/0/type', value: 'bad-type', values: ['component', 'rich-text'] },
|
343
|
+
message: 'Expected given value `bad-type` in #/containers/0/items/0/type` to be one of `[component, rich-text]`',
|
344
|
+
},
|
345
|
+
],
|
346
|
+
'#/containers/0/layout/value': [
|
347
|
+
{
|
348
|
+
data: { pointer: '#/containers/0/layout/value', expected: 'string', received: 'number', value: 123 },
|
349
|
+
message: 'Expected `123` (number) in `#/containers/0/layout/value` to be of type `string`',
|
350
|
+
},
|
351
|
+
],
|
352
|
+
});
|
353
|
+
});
|
354
|
+
});
|
254
355
|
});
|
255
356
|
const defaultSchema = {
|
256
357
|
type: 'object',
|
@@ -1048,4 +1149,479 @@ describe('JsonSchemaService', () => {
|
|
1048
1149
|
});
|
1049
1150
|
});
|
1050
1151
|
});
|
1152
|
+
describe('JSONSchemaService - validation', () => {
|
1153
|
+
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1154
|
+
.addPrimitive(primitiveTypes_1.SquizImageType)
|
1155
|
+
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1156
|
+
.addPrimitive(primitiveTypes_1.FormattedTextType)
|
1157
|
+
.build();
|
1158
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1159
|
+
it('should validate a schema with all the squiz primitive types', () => {
|
1160
|
+
const schema = {
|
1161
|
+
type: 'object',
|
1162
|
+
properties: {
|
1163
|
+
image: { type: 'SquizImage' },
|
1164
|
+
link: { type: 'SquizLink' },
|
1165
|
+
text: { type: 'FormattedText' },
|
1166
|
+
},
|
1167
|
+
required: ['image', 'link', 'text'],
|
1168
|
+
};
|
1169
|
+
const formattedText = [
|
1170
|
+
{
|
1171
|
+
tag: 'p',
|
1172
|
+
type: 'tag',
|
1173
|
+
children: [{ type: 'text', value: 'hello' }],
|
1174
|
+
},
|
1175
|
+
];
|
1176
|
+
const input = {
|
1177
|
+
image: {
|
1178
|
+
name: 'test-image.jpeg',
|
1179
|
+
imageVariations: {
|
1180
|
+
original: {
|
1181
|
+
aspectRatio: '1:1',
|
1182
|
+
height: 100,
|
1183
|
+
width: 100,
|
1184
|
+
url: 'https://www.squiz.net',
|
1185
|
+
byteSize: 100,
|
1186
|
+
mimeType: 'image/jpeg',
|
1187
|
+
sha1Hash: '123',
|
1188
|
+
},
|
1189
|
+
},
|
1190
|
+
},
|
1191
|
+
link: {
|
1192
|
+
name: 'test-link',
|
1193
|
+
url: 'https://www.squiz.net',
|
1194
|
+
target: '_blank',
|
1195
|
+
},
|
1196
|
+
text: formattedText,
|
1197
|
+
};
|
1198
|
+
const result = jsonSchemaService.validateInput(input, schema);
|
1199
|
+
expect(result).toEqual(true);
|
1200
|
+
});
|
1201
|
+
it('should validate a schema with all the squiz primitive types and matrix-asset-uri format', () => {
|
1202
|
+
const schema = {
|
1203
|
+
type: 'object',
|
1204
|
+
properties: {
|
1205
|
+
image: { type: 'SquizImage' },
|
1206
|
+
link: { type: 'SquizLink' },
|
1207
|
+
text: { type: 'FormattedText' },
|
1208
|
+
asset: {
|
1209
|
+
type: 'string',
|
1210
|
+
format: 'matrix-asset-uri',
|
1211
|
+
},
|
1212
|
+
},
|
1213
|
+
required: ['image', 'link', 'text', 'asset'],
|
1214
|
+
};
|
1215
|
+
const formattedText = [
|
1216
|
+
{
|
1217
|
+
tag: 'p',
|
1218
|
+
type: 'tag',
|
1219
|
+
children: [{ type: 'text', value: 'hello' }],
|
1220
|
+
},
|
1221
|
+
];
|
1222
|
+
const input = {
|
1223
|
+
image: {
|
1224
|
+
name: 'test-image.jpeg',
|
1225
|
+
imageVariations: {
|
1226
|
+
original: {
|
1227
|
+
aspectRatio: '1:1',
|
1228
|
+
height: 100,
|
1229
|
+
width: 100,
|
1230
|
+
url: 'https://www.squiz.net',
|
1231
|
+
byteSize: 100,
|
1232
|
+
mimeType: 'image/jpeg',
|
1233
|
+
sha1Hash: '123',
|
1234
|
+
},
|
1235
|
+
},
|
1236
|
+
},
|
1237
|
+
link: {
|
1238
|
+
name: 'test-link',
|
1239
|
+
url: 'https://www.squiz.net',
|
1240
|
+
target: '_blank',
|
1241
|
+
},
|
1242
|
+
text: formattedText,
|
1243
|
+
asset: 'matrix-asset://identifier/123',
|
1244
|
+
};
|
1245
|
+
const result = jsonSchemaService.validateInput(input, schema);
|
1246
|
+
expect(result).toEqual(true);
|
1247
|
+
});
|
1248
|
+
it('should catch validation errors when there is a schema with all the squiz primitive types', () => {
|
1249
|
+
const schema = {
|
1250
|
+
type: 'object',
|
1251
|
+
properties: {
|
1252
|
+
image: { type: 'SquizImage' },
|
1253
|
+
link: { type: 'SquizLink' },
|
1254
|
+
text: { type: 'FormattedText' },
|
1255
|
+
},
|
1256
|
+
required: ['image', 'link', 'text'],
|
1257
|
+
};
|
1258
|
+
const formattedText = [
|
1259
|
+
//@ts-expect-error - wrong type
|
1260
|
+
{
|
1261
|
+
children: [{ type: 'text', value: 'hello' }],
|
1262
|
+
},
|
1263
|
+
];
|
1264
|
+
const input = {
|
1265
|
+
image: {
|
1266
|
+
name: 'test-image.jpeg',
|
1267
|
+
imageVariations: {
|
1268
|
+
//@ts-expect-error - wrong type
|
1269
|
+
original: {
|
1270
|
+
width: 100,
|
1271
|
+
url: 'https://www.squiz.net',
|
1272
|
+
byteSize: 100,
|
1273
|
+
mimeType: 'image/jpeg',
|
1274
|
+
sha1Hash: '123',
|
1275
|
+
},
|
1276
|
+
},
|
1277
|
+
},
|
1278
|
+
//@ts-expect-error - wrong type
|
1279
|
+
link: {
|
1280
|
+
name: 'test-link',
|
1281
|
+
target: '_blank',
|
1282
|
+
},
|
1283
|
+
text: formattedText,
|
1284
|
+
};
|
1285
|
+
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 `{"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', {
|
1286
|
+
'#/image': [
|
1287
|
+
{
|
1288
|
+
data: {
|
1289
|
+
errors: [
|
1290
|
+
{
|
1291
|
+
code: 'required-property-error',
|
1292
|
+
data: { key: 'height', pointer: '#/image/imageVariations/original' },
|
1293
|
+
message: 'The required property `height` is missing at `#/image/imageVariations/original`',
|
1294
|
+
name: 'RequiredPropertyError',
|
1295
|
+
type: 'error',
|
1296
|
+
},
|
1297
|
+
{
|
1298
|
+
code: 'required-property-error',
|
1299
|
+
data: { key: 'aspectRatio', pointer: '#/image/imageVariations/original' },
|
1300
|
+
message: 'The required property `aspectRatio` is missing at `#/image/imageVariations/original`',
|
1301
|
+
name: 'RequiredPropertyError',
|
1302
|
+
type: 'error',
|
1303
|
+
},
|
1304
|
+
],
|
1305
|
+
oneOf: [{ $ref: 'SquizImage.json' }],
|
1306
|
+
pointer: '#/image',
|
1307
|
+
value: '{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}',
|
1308
|
+
},
|
1309
|
+
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',
|
1310
|
+
},
|
1311
|
+
],
|
1312
|
+
'#/link': [
|
1313
|
+
{
|
1314
|
+
data: {
|
1315
|
+
errors: [
|
1316
|
+
{
|
1317
|
+
code: 'required-property-error',
|
1318
|
+
data: { key: 'url', pointer: '#/link' },
|
1319
|
+
message: 'The required property `url` is missing at `#/link`',
|
1320
|
+
name: 'RequiredPropertyError',
|
1321
|
+
type: 'error',
|
1322
|
+
},
|
1323
|
+
],
|
1324
|
+
oneOf: [{ $ref: 'SquizLink.json' }],
|
1325
|
+
pointer: '#/link',
|
1326
|
+
value: '{"name":"test-link","target":"_blank"}',
|
1327
|
+
},
|
1328
|
+
message: 'Value `{"name":"test-link","target":"_blank"}` in `#/link` does not match any given oneof schema',
|
1329
|
+
},
|
1330
|
+
],
|
1331
|
+
'#/text': [
|
1332
|
+
{
|
1333
|
+
data: {
|
1334
|
+
errors: [
|
1335
|
+
{
|
1336
|
+
code: 'any-of-error',
|
1337
|
+
data: {
|
1338
|
+
anyOf: [
|
1339
|
+
{ $ref: '#/definitions/HigherOrderFormattedNodes' },
|
1340
|
+
{ $ref: '#/definitions/BaseFormattedNodes' },
|
1341
|
+
],
|
1342
|
+
pointer: '#/text/0',
|
1343
|
+
value: { children: [{ type: 'text', value: 'hello' }] },
|
1344
|
+
},
|
1345
|
+
message: 'Object at `#/text/0` does not match any schema',
|
1346
|
+
name: 'AnyOfError',
|
1347
|
+
type: 'error',
|
1348
|
+
},
|
1349
|
+
],
|
1350
|
+
oneOf: [{ $ref: 'FormattedText.json' }],
|
1351
|
+
pointer: '#/text',
|
1352
|
+
value: '[{"children":[{"type":"text","value":"hello"}]}]',
|
1353
|
+
},
|
1354
|
+
message: 'Value `[{"children":[{"type":"text","value":"hello"}]}]` in `#/text` does not match any given oneof schema',
|
1355
|
+
},
|
1356
|
+
],
|
1357
|
+
});
|
1358
|
+
});
|
1359
|
+
it('should catch validation errors when invalid matrix-asset-uri is provided with invalid other squiz primitive types ', () => {
|
1360
|
+
const schema = {
|
1361
|
+
type: 'object',
|
1362
|
+
properties: {
|
1363
|
+
image: { type: 'SquizImage' },
|
1364
|
+
link: { type: 'SquizLink' },
|
1365
|
+
text: { type: 'FormattedText' },
|
1366
|
+
asset: {
|
1367
|
+
type: 'string',
|
1368
|
+
format: 'matrix-asset-uri',
|
1369
|
+
},
|
1370
|
+
},
|
1371
|
+
required: ['image', 'link', 'text', 'asset'],
|
1372
|
+
};
|
1373
|
+
const formattedText = [
|
1374
|
+
//@ts-expect-error - wrong type
|
1375
|
+
{
|
1376
|
+
children: [{ type: 'text', value: 'hello' }],
|
1377
|
+
},
|
1378
|
+
];
|
1379
|
+
const input = {
|
1380
|
+
image: {
|
1381
|
+
name: 'test-image.jpeg',
|
1382
|
+
imageVariations: {
|
1383
|
+
//@ts-expect-error - wrong type
|
1384
|
+
original: {
|
1385
|
+
width: 100,
|
1386
|
+
url: 'https://www.squiz.net',
|
1387
|
+
byteSize: 100,
|
1388
|
+
mimeType: 'image/jpeg',
|
1389
|
+
sha1Hash: '123',
|
1390
|
+
},
|
1391
|
+
},
|
1392
|
+
},
|
1393
|
+
//@ts-expect-error - wrong type
|
1394
|
+
link: {
|
1395
|
+
name: 'test-link',
|
1396
|
+
target: '_blank',
|
1397
|
+
},
|
1398
|
+
text: formattedText,
|
1399
|
+
// @ts-expect-error - wrong type
|
1400
|
+
asset: 'matrix://123',
|
1401
|
+
};
|
1402
|
+
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 `{"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', {
|
1403
|
+
'#/asset': [
|
1404
|
+
{
|
1405
|
+
data: {
|
1406
|
+
errors: {
|
1407
|
+
assetId: {
|
1408
|
+
data: { expected: /^\d+(?::.+)?$/, received: '' },
|
1409
|
+
message: 'Matrix Asset Id has invalid format, must match /^d+(?::.+)?$/',
|
1410
|
+
},
|
1411
|
+
scheme: {
|
1412
|
+
data: { expected: 'matrix-asset', received: 'matrix' },
|
1413
|
+
message: 'Uri scheme is invalid, must match "matrix-asset"',
|
1414
|
+
},
|
1415
|
+
},
|
1416
|
+
pointer: '#/asset',
|
1417
|
+
value: 'matrix://123',
|
1418
|
+
},
|
1419
|
+
message: 'Value matrix-asset-uri (matrix://123) in `#/asset` is not a valid matrix asset uri',
|
1420
|
+
},
|
1421
|
+
],
|
1422
|
+
'#/image': [
|
1423
|
+
{
|
1424
|
+
data: {
|
1425
|
+
errors: [
|
1426
|
+
{
|
1427
|
+
code: 'required-property-error',
|
1428
|
+
data: { key: 'height', pointer: '#/image/imageVariations/original' },
|
1429
|
+
message: 'The required property `height` is missing at `#/image/imageVariations/original`',
|
1430
|
+
name: 'RequiredPropertyError',
|
1431
|
+
type: 'error',
|
1432
|
+
},
|
1433
|
+
{
|
1434
|
+
code: 'required-property-error',
|
1435
|
+
data: { key: 'aspectRatio', pointer: '#/image/imageVariations/original' },
|
1436
|
+
message: 'The required property `aspectRatio` is missing at `#/image/imageVariations/original`',
|
1437
|
+
name: 'RequiredPropertyError',
|
1438
|
+
type: 'error',
|
1439
|
+
},
|
1440
|
+
],
|
1441
|
+
oneOf: [{ $ref: 'SquizImage.json' }],
|
1442
|
+
pointer: '#/image',
|
1443
|
+
value: '{"name":"test-image.jpeg","imageVariations":{"original":{"width":100,"url":"https://www.squiz.net","byteSize":100,"mimeType":"image/jpeg","sha1Hash":"123"}}}',
|
1444
|
+
},
|
1445
|
+
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',
|
1446
|
+
},
|
1447
|
+
],
|
1448
|
+
'#/link': [
|
1449
|
+
{
|
1450
|
+
data: {
|
1451
|
+
errors: [
|
1452
|
+
{
|
1453
|
+
code: 'required-property-error',
|
1454
|
+
data: { key: 'url', pointer: '#/link' },
|
1455
|
+
message: 'The required property `url` is missing at `#/link`',
|
1456
|
+
name: 'RequiredPropertyError',
|
1457
|
+
type: 'error',
|
1458
|
+
},
|
1459
|
+
],
|
1460
|
+
oneOf: [{ $ref: 'SquizLink.json' }],
|
1461
|
+
pointer: '#/link',
|
1462
|
+
value: '{"name":"test-link","target":"_blank"}',
|
1463
|
+
},
|
1464
|
+
message: 'Value `{"name":"test-link","target":"_blank"}` in `#/link` does not match any given oneof schema',
|
1465
|
+
},
|
1466
|
+
],
|
1467
|
+
'#/text': [
|
1468
|
+
{
|
1469
|
+
data: {
|
1470
|
+
errors: [
|
1471
|
+
{
|
1472
|
+
code: 'any-of-error',
|
1473
|
+
data: {
|
1474
|
+
anyOf: [
|
1475
|
+
{ $ref: '#/definitions/HigherOrderFormattedNodes' },
|
1476
|
+
{ $ref: '#/definitions/BaseFormattedNodes' },
|
1477
|
+
],
|
1478
|
+
pointer: '#/text/0',
|
1479
|
+
value: { children: [{ type: 'text', value: 'hello' }] },
|
1480
|
+
},
|
1481
|
+
message: 'Object at `#/text/0` does not match any schema',
|
1482
|
+
name: 'AnyOfError',
|
1483
|
+
type: 'error',
|
1484
|
+
},
|
1485
|
+
],
|
1486
|
+
oneOf: [{ $ref: 'FormattedText.json' }],
|
1487
|
+
pointer: '#/text',
|
1488
|
+
value: '[{"children":[{"type":"text","value":"hello"}]}]',
|
1489
|
+
},
|
1490
|
+
message: 'Value `[{"children":[{"type":"text","value":"hello"}]}]` in `#/text` does not match any given oneof schema',
|
1491
|
+
},
|
1492
|
+
],
|
1493
|
+
});
|
1494
|
+
});
|
1495
|
+
it('should validate when MatrixAssetType is being used for a squiz primitive type with resolver', () => {
|
1496
|
+
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1497
|
+
.addPrimitive(primitiveTypes_1.SquizImageType)
|
1498
|
+
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1499
|
+
.addPrimitive(primitiveTypes_1.FormattedTextType)
|
1500
|
+
.addResolver(primitiveTypes_1.SquizImageType, resolvableTypes_1.MatrixAssetType, () => {
|
1501
|
+
return {
|
1502
|
+
name: '',
|
1503
|
+
imageVariations: {
|
1504
|
+
original: {
|
1505
|
+
width: 0,
|
1506
|
+
height: 0,
|
1507
|
+
url: '',
|
1508
|
+
mimeType: '',
|
1509
|
+
byteSize: 0,
|
1510
|
+
sha1Hash: '',
|
1511
|
+
aspectRatio: '',
|
1512
|
+
},
|
1513
|
+
},
|
1514
|
+
};
|
1515
|
+
})
|
1516
|
+
.build();
|
1517
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1518
|
+
const schema = {
|
1519
|
+
type: 'object',
|
1520
|
+
properties: {
|
1521
|
+
image: { type: 'SquizImage' },
|
1522
|
+
link: { type: 'SquizLink' },
|
1523
|
+
text: { type: 'FormattedText' },
|
1524
|
+
asset: {
|
1525
|
+
type: 'string',
|
1526
|
+
format: 'matrix-asset-uri',
|
1527
|
+
},
|
1528
|
+
},
|
1529
|
+
required: ['image', 'link', 'text', 'asset'],
|
1530
|
+
};
|
1531
|
+
const formattedText = [
|
1532
|
+
{
|
1533
|
+
tag: 'p',
|
1534
|
+
type: 'tag',
|
1535
|
+
children: [{ type: 'text', value: 'hello' }],
|
1536
|
+
},
|
1537
|
+
];
|
1538
|
+
const input = {
|
1539
|
+
image: {
|
1540
|
+
matrixAssetId: '123',
|
1541
|
+
matrixIdentifier: 'identifier',
|
1542
|
+
matrixDomain: 'domain',
|
1543
|
+
},
|
1544
|
+
link: {
|
1545
|
+
name: 'test-link',
|
1546
|
+
url: 'https://www.squiz.net',
|
1547
|
+
target: '_blank',
|
1548
|
+
},
|
1549
|
+
text: formattedText,
|
1550
|
+
asset: 'matrix-asset://identifier/123',
|
1551
|
+
};
|
1552
|
+
expect(jsonSchemaService.validateInput(input, schema)).toEqual(true);
|
1553
|
+
});
|
1554
|
+
it('it should catch MatrixAssetType validation errors when being use for squiz primitive type with resolver', () => {
|
1555
|
+
const typeResolver = TypeResolverBuilder_1.TypeResolverBuilder.new()
|
1556
|
+
.addPrimitive(primitiveTypes_1.SquizImageType)
|
1557
|
+
.addPrimitive(primitiveTypes_1.SquizLinkType)
|
1558
|
+
.addPrimitive(primitiveTypes_1.FormattedTextType)
|
1559
|
+
.addResolver(primitiveTypes_1.SquizImageType, resolvableTypes_1.MatrixAssetType, () => {
|
1560
|
+
return {
|
1561
|
+
name: '',
|
1562
|
+
imageVariations: {
|
1563
|
+
original: {
|
1564
|
+
width: 0,
|
1565
|
+
height: 0,
|
1566
|
+
url: '',
|
1567
|
+
mimeType: '',
|
1568
|
+
byteSize: 0,
|
1569
|
+
sha1Hash: '',
|
1570
|
+
aspectRatio: '',
|
1571
|
+
},
|
1572
|
+
},
|
1573
|
+
};
|
1574
|
+
})
|
1575
|
+
.build();
|
1576
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(typeResolver, JsonValidationService_1.ComponentInputMetaSchema);
|
1577
|
+
const schema = {
|
1578
|
+
type: 'object',
|
1579
|
+
properties: {
|
1580
|
+
image: { type: 'SquizImage' },
|
1581
|
+
},
|
1582
|
+
required: ['image'],
|
1583
|
+
};
|
1584
|
+
const input = {
|
1585
|
+
//@ts-expect-error - intentionally invalid input
|
1586
|
+
image: {
|
1587
|
+
matrixIdentifier: 'identifier',
|
1588
|
+
},
|
1589
|
+
};
|
1590
|
+
expectToThrowErrorMatchingTypeAndMessage(() => jsonSchemaService.validateInput(input, schema), SchemaValidationError_1.SchemaValidationError, 'failed validation: Value `{"matrixIdentifier":"identifier"}` in `#/image` does not match any given oneof schema', {
|
1591
|
+
'#/image': [
|
1592
|
+
{
|
1593
|
+
data: {
|
1594
|
+
errors: [
|
1595
|
+
{
|
1596
|
+
code: 'required-property-error',
|
1597
|
+
data: { key: 'name', pointer: '#/image' },
|
1598
|
+
message: 'The required property `name` is missing at `#/image`',
|
1599
|
+
name: 'RequiredPropertyError',
|
1600
|
+
type: 'error',
|
1601
|
+
},
|
1602
|
+
{
|
1603
|
+
code: 'required-property-error',
|
1604
|
+
data: { key: 'imageVariations', pointer: '#/image' },
|
1605
|
+
message: 'The required property `imageVariations` is missing at `#/image`',
|
1606
|
+
name: 'RequiredPropertyError',
|
1607
|
+
type: 'error',
|
1608
|
+
},
|
1609
|
+
{
|
1610
|
+
code: 'required-property-error',
|
1611
|
+
data: { key: 'matrixAssetId', pointer: '#/image' },
|
1612
|
+
message: 'The required property `matrixAssetId` is missing at `#/image`',
|
1613
|
+
name: 'RequiredPropertyError',
|
1614
|
+
type: 'error',
|
1615
|
+
},
|
1616
|
+
],
|
1617
|
+
oneOf: [{ $ref: 'SquizImage.json' }, { $ref: 'MatrixAsset.json' }],
|
1618
|
+
pointer: '#/image',
|
1619
|
+
value: '{"matrixIdentifier":"identifier"}',
|
1620
|
+
},
|
1621
|
+
message: 'Value `{"matrixIdentifier":"identifier"}` in `#/image` does not match any given oneof schema',
|
1622
|
+
},
|
1623
|
+
],
|
1624
|
+
});
|
1625
|
+
});
|
1626
|
+
});
|
1051
1627
|
//# sourceMappingURL=JsonValidationService.spec.js.map
|