@squiz/dx-json-schema-lib
Advanced tools
Comparing version 1.10.0-alpha.0 to 1.11.0-alpha.0
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "SquizFormattedTextSchema.json", | ||
"type": "array", | ||
@@ -4,0 +5,0 @@ "title": "FormattedText", |
@@ -9,6 +9,7 @@ "use strict"; | ||
const __1 = require("../.."); | ||
const FORMATTED_TEXT_SCHEMA_ID = 'SquizFormattedTextSchema.json'; | ||
async function resolveFormattedTextNodes(contentItem, contentSchema, resolvers, { withJoin } = {}) { | ||
const resolvedDataSetters = []; | ||
__1.ContentSchemaSchema.each(contentItem, (schema, data, pointer) => { | ||
if (schema.type === 'FormattedText') { | ||
__1.ComponentInputSchema.each(contentItem, (schema, data, pointer) => { | ||
if (schema.type === 'FormattedText' || schema.$id === FORMATTED_TEXT_SCHEMA_ID) { | ||
const formattedText = data; | ||
@@ -29,3 +30,3 @@ const textResolvers = formattedText.map((t) => resolveFormattedText(t, resolvers)); | ||
} | ||
}, contentSchema); | ||
}, __1.ComponentInputSchema.compileSchema(contentSchema)); | ||
for (const setResolvedData of await Promise.all(resolvedDataSetters)) { | ||
@@ -32,0 +33,0 @@ contentItem = setResolvedData(contentItem); |
@@ -226,3 +226,52 @@ "use strict"; | ||
}); | ||
it('should resolve a FormattedText value defined in a $ref', async () => { | ||
const testSchema = { | ||
type: 'object', | ||
properties: { | ||
normal: { type: 'string' }, | ||
number: { type: 'number' }, | ||
formattedText: { $ref: '#/definitions/FormattedText' }, | ||
}, | ||
required: ['normal', 'number', 'formattedText'], | ||
definitions: { | ||
FormattedText: { type: 'FormattedText' }, | ||
}, | ||
}; | ||
const testItem = { | ||
normal: 'string', | ||
number: 312, | ||
formattedText: [ | ||
{ | ||
type: 'link-to-matrix-asset', | ||
matrixIdentifier: 'abc123', | ||
matrixDomain: 'example.com', | ||
matrixAssetId: '12345', | ||
target: '_blank', | ||
children: [{ type: 'text', value: 'linked text' }], | ||
}, | ||
{ | ||
type: 'matrix-image', | ||
matrixIdentifier: 'abc123', | ||
matrixDomain: 'example.com', | ||
matrixAssetId: '54321', | ||
}, | ||
{ | ||
type: 'text', | ||
value: 'This is some text', | ||
}, | ||
], | ||
}; | ||
const expectedItem = { | ||
normal: 'string', | ||
number: 312, | ||
formattedText: [ | ||
{ type: 'text', value: 'Link to asset 12345' }, | ||
{ type: 'text', value: 'Image 54321' }, | ||
{ type: 'text', value: 'This is some text' }, | ||
], | ||
}; | ||
const result = await (0, resolveFormattedTextNodes_1.resolveFormattedTextNodes)(testItem, testSchema, testResolverMap); | ||
expect(result).toEqual(expectedItem); | ||
}); | ||
}); | ||
//# sourceMappingURL=resolveFormattedTextNodes.spec.js.map |
@@ -1,4 +0,3 @@ | ||
import { Draft07, JSONSchema, Draft } from 'json-schema-library'; | ||
import { JSONSchema, Draft } from 'json-schema-library'; | ||
export declare const ComponentInputSchema: Draft; | ||
export declare const ContentSchemaSchema: Draft07; | ||
export declare class JsonValidationService { | ||
@@ -5,0 +4,0 @@ validateManifest(manifest: unknown, version: 'v1'): true; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.JsonValidationService = exports.ContentSchemaSchema = exports.ComponentInputSchema = void 0; | ||
exports.JsonValidationService = exports.ComponentInputSchema = void 0; | ||
const DxComponentInputSchema_json_1 = __importDefault(require("./manifest/v1/DxComponentInputSchema.json")); | ||
@@ -88,4 +88,2 @@ const DxComponentIcons_json_1 = __importDefault(require("./manifest/v1/DxComponentIcons.json")); | ||
v1Schema.addRemoteSchema('http://json-schema.org/draft-07/schema#', Draft_07_json_1.default); | ||
exports.ContentSchemaSchema = new json_schema_library_1.Draft07(DxComponentInputSchema_json_1.default, defaultConfig); | ||
exports.ContentSchemaSchema.addRemoteSchema('DxComponentInputSchema.json/DxContentMetaSchema.json', DxContentMetaSchema_json_1.default); | ||
class JsonValidationService { | ||
@@ -103,3 +101,3 @@ validateManifest(manifest, version) { | ||
validateContentSchema(contentSchema) { | ||
return this.processValidationResult(exports.ContentSchemaSchema.validate(contentSchema)); | ||
return this.processValidationResult(exports.ComponentInputSchema.validate(contentSchema)); | ||
} | ||
@@ -106,0 +104,0 @@ validateComponentInput(functionInputSchema, inputValue) { |
@@ -306,2 +306,43 @@ "use strict"; | ||
}); | ||
it('should validate a FormattedText value when the schema is a $ref', async () => { | ||
const formattedText = [ | ||
{ | ||
tag: 'p', | ||
type: 'tag', | ||
children: [{ type: 'text', value: 'hello' }], | ||
}, | ||
]; | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
'my-input': { $ref: '#/definitions/FormattedText' }, | ||
}, | ||
definitions: { | ||
FormattedText: { type: 'FormattedText' }, | ||
}, | ||
required: ['my-input'], | ||
}; | ||
const value = { | ||
'my-input': formattedText, | ||
}; | ||
expect(jsonValidationService.validateComponentInput(schema, value)).toEqual(true); | ||
}); | ||
it('should error when a FormattedText value is invalid when the schema is a $ref', async () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
'my-input': { $ref: '#/definitions/FormattedText' }, | ||
}, | ||
definitions: { | ||
FormattedText: { type: 'FormattedText' }, | ||
}, | ||
required: ['my-input'], | ||
}; | ||
const value = { | ||
'my-input': { bad: 'data' }, | ||
}; | ||
expectToThrowErrorMatchingTypeAndMessage(() => { | ||
jsonValidationService.validateComponentInput(schema, value); | ||
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Expected `[object Object]` (object) in `#/my-input` to be of type `array`'); | ||
}); | ||
}); | ||
@@ -308,0 +349,0 @@ describe('standard inputs', () => { |
{ | ||
"name": "@squiz/dx-json-schema-lib", | ||
"version": "1.10.0-alpha.0", | ||
"version": "1.11.0-alpha.0", | ||
"description": "", | ||
@@ -36,3 +36,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "a19663513d757ad95579e8f01cbb0ac417a71388" | ||
"gitHead": "540c1004d0f424fed9d66ad6c6ff5125b2b0283a" | ||
} |
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "SquizFormattedTextSchema.json", | ||
"type": "array", | ||
@@ -4,0 +5,0 @@ "title": "FormattedText", |
@@ -246,2 +246,55 @@ import { FormattedNodes } from './formattedText'; | ||
}); | ||
it('should resolve a FormattedText value defined in a $ref', async () => { | ||
const testSchema = { | ||
type: 'object', | ||
properties: { | ||
normal: { type: 'string' }, | ||
number: { type: 'number' }, | ||
formattedText: { $ref: '#/definitions/FormattedText' }, | ||
}, | ||
required: ['normal', 'number', 'formattedText'], | ||
definitions: { | ||
FormattedText: { type: 'FormattedText' }, | ||
}, | ||
}; | ||
const testItem = { | ||
normal: 'string', | ||
number: 312, | ||
formattedText: [ | ||
{ | ||
type: 'link-to-matrix-asset', | ||
matrixIdentifier: 'abc123', | ||
matrixDomain: 'example.com', | ||
matrixAssetId: '12345', | ||
target: '_blank', | ||
children: [{ type: 'text', value: 'linked text' }], | ||
}, | ||
{ | ||
type: 'matrix-image', | ||
matrixIdentifier: 'abc123', | ||
matrixDomain: 'example.com', | ||
matrixAssetId: '54321', | ||
}, | ||
{ | ||
type: 'text', | ||
value: 'This is some text', | ||
}, | ||
], | ||
}; | ||
const expectedItem = { | ||
normal: 'string', | ||
number: 312, | ||
formattedText: [ | ||
{ type: 'text', value: 'Link to asset 12345' }, | ||
{ type: 'text', value: 'Image 54321' }, | ||
{ type: 'text', value: 'This is some text' }, | ||
], | ||
}; | ||
const result = await resolveFormattedTextNodes(testItem, testSchema, testResolverMap); | ||
expect(result).toEqual(expectedItem); | ||
}); | ||
}); |
import JSONQuery from '@sagold/json-query'; | ||
import { JSONSchema } from 'json-schema-library'; | ||
import { ContentSchemaSchema } from '../..'; | ||
import { ComponentInputSchema } from '../..'; | ||
import { | ||
@@ -25,2 +25,4 @@ BaseFormattedNodes, | ||
const FORMATTED_TEXT_SCHEMA_ID = 'SquizFormattedTextSchema.json'; | ||
export type ResolvedChildNode< | ||
@@ -47,6 +49,6 @@ DEFAULT_NODES extends FormattedNode, | ||
const resolvedDataSetters: Promise<(newItem: typeof contentItem) => typeof contentItem>[] = []; | ||
ContentSchemaSchema.each( | ||
ComponentInputSchema.each( | ||
contentItem, | ||
(schema, data, pointer) => { | ||
if (schema.type === 'FormattedText') { | ||
if (schema.type === 'FormattedText' || schema.$id === FORMATTED_TEXT_SCHEMA_ID) { | ||
const formattedText = data as FormattedText; | ||
@@ -70,3 +72,3 @@ const textResolvers = formattedText.map((t) => resolveFormattedText(t, resolvers)); | ||
}, | ||
contentSchema, | ||
ComponentInputSchema.compileSchema(contentSchema), | ||
); | ||
@@ -73,0 +75,0 @@ |
@@ -401,2 +401,52 @@ import { JsonValidationService } from './JsonValidationService'; | ||
}); | ||
it('should validate a FormattedText value when the schema is a $ref', async () => { | ||
const formattedText: FormattedText = [ | ||
{ | ||
tag: 'p', | ||
type: 'tag', | ||
children: [{ type: 'text', value: 'hello' }], | ||
}, | ||
]; | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
'my-input': { $ref: '#/definitions/FormattedText' }, | ||
}, | ||
definitions: { | ||
FormattedText: { type: 'FormattedText' }, | ||
}, | ||
required: ['my-input'], | ||
}; | ||
const value = { | ||
'my-input': formattedText, | ||
}; | ||
expect(jsonValidationService.validateComponentInput(schema, value)).toEqual(true); | ||
}); | ||
it('should error when a FormattedText value is invalid when the schema is a $ref', async () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
'my-input': { $ref: '#/definitions/FormattedText' }, | ||
}, | ||
definitions: { | ||
FormattedText: { type: 'FormattedText' }, | ||
}, | ||
required: ['my-input'], | ||
}; | ||
const value = { | ||
'my-input': { bad: 'data' }, | ||
}; | ||
expectToThrowErrorMatchingTypeAndMessage( | ||
() => { | ||
jsonValidationService.validateComponentInput(schema, value); | ||
}, | ||
SchemaValidationError, | ||
'failed validation: Expected `[object Object]` (object) in `#/my-input` to be of type `array`', | ||
); | ||
}); | ||
}); | ||
@@ -403,0 +453,0 @@ |
@@ -99,5 +99,2 @@ import DxComponentInputSchema from './manifest/v1/DxComponentInputSchema.json'; | ||
export const ContentSchemaSchema = new Draft07(DxComponentInputSchema, defaultConfig); | ||
ContentSchemaSchema.addRemoteSchema('DxComponentInputSchema.json/DxContentMetaSchema.json', DxContentMetaSchema); | ||
export class JsonValidationService { | ||
@@ -116,3 +113,3 @@ validateManifest(manifest: unknown, version: 'v1') { | ||
validateContentSchema(contentSchema: JSONSchema) { | ||
return this.processValidationResult(ContentSchemaSchema.validate(contentSchema)); | ||
return this.processValidationResult(ComponentInputSchema.validate(contentSchema)); | ||
} | ||
@@ -119,0 +116,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
480270
12489