UNPKG

@contentful/rich-text-types

Version:

Type definitions and constants for the Contentful rich text field type.

996 lines 37.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var blocks_1 = require("../blocks"); var inlines_1 = require("../inlines"); var validator_1 = require("../validator"); describe('validation', function () { it('fails if it is not document node', function () { // @ts-expect-error we force a wrong node type to check that it fails var document = { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: {} }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: ['document'], name: 'in', path: ['nodeType'], value: 'paragraph', }, ]); }); it('fails if it has an invalid shape', function () { // @ts-expect-error we force a wrong node type to check that it fails var document = { nodeType: blocks_1.BLOCKS.DOCUMENT }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { name: 'required', path: ['content'], details: 'The property "content" is required here', }, { name: 'required', path: ['data'], details: 'The property "data" is required here', }, ]); }); it('fails if it has nested documents', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, // @ts-expect-error we force a wrong node type to check that it fails content: [{ nodeType: blocks_1.BLOCKS.DOCUMENT, content: [], data: {} }], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'blockquote', 'embedded-asset-block', 'embedded-entry-block', 'embedded-resource-block', 'heading-1', 'heading-2', 'heading-3', 'heading-4', 'heading-5', 'heading-6', 'hr', 'ordered-list', 'paragraph', 'table', 'unordered-list', ], name: 'in', path: ['content', 0, 'nodeType'], value: 'document', }, ]); }); it('fails without a nodeType property', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, // @ts-expect-error we force a wrong node type to check that it fails content: [{ content: [], data: {} }], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'blockquote', 'embedded-asset-block', 'embedded-entry-block', 'embedded-resource-block', 'heading-1', 'heading-2', 'heading-3', 'heading-4', 'heading-5', 'heading-6', 'hr', 'ordered-list', 'paragraph', 'table', 'unordered-list', ], name: 'in', path: ['content', 0, 'nodeType'], value: undefined, }, ]); }); it('fails on custom nodeTypes (unknown nodeType)', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, // @ts-expect-error we force a wrong node type to check that it fails content: [{ nodeType: 'custom-node-type', content: [], data: {} }], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'blockquote', 'embedded-asset-block', 'embedded-entry-block', 'embedded-resource-block', 'heading-1', 'heading-2', 'heading-3', 'heading-4', 'heading-5', 'heading-6', 'hr', 'ordered-list', 'paragraph', 'table', 'unordered-list', ], name: 'in', path: ['content', 0, 'nodeType'], value: 'custom-node-type', }, ]); }); it('fails without a content property', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, // @ts-expect-error we force a wrong node type to check that it fails content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, data: {} }], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The property "content" is required here', name: 'required', path: ['content', 0, 'content'], value: undefined, }, ]); }); it('fails with a invalid content property', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, // @ts-expect-error we force a wrong type to check that it fails content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: 'Hello World', data: {} }], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The type of "content" is incorrect, expected type: Array', name: 'type', path: ['content', 0, 'content'], type: 'Array', value: 'Hello World', }, ]); }); it('fails without data property', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, // @ts-expect-error we force a wrong node type to check that it fails content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [] }], }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The property "data" is required here', name: 'required', path: ['data'], }, ]); }); it('fails with invalid data property', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: null }], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The type of "data" is incorrect, expected type: Object', name: 'type', path: ['content', 0, 'data'], type: 'Object', value: null, }, ]); }); it('fails if undefined is in the content list', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: {} }, undefined], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The type of "1" is incorrect, expected type: Object', name: 'type', path: ['content', 1], type: 'Object', value: undefined, }, ]); }); it('fails if undefined is in the content list of child nodes', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.UL_LIST, content: [ { nodeType: blocks_1.BLOCKS.LIST_ITEM, content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: {} }, undefined], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The type of "1" is incorrect, expected type: Object', name: 'type', path: ['content', 0, 'content', 0, 'content', 1], type: 'Object', value: undefined, }, ]); }); it('fails with unknown properties', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [], data: {}, // @ts-expect-error we force a wrong property to check that it fails myCustomProperty: 'Hello World', }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The property "myCustomProperty" is not expected', name: 'unexpected', path: ['myCustomProperty'], }, ]); }); it.each([blocks_1.BLOCKS.LIST_ITEM, blocks_1.BLOCKS.TABLE_ROW, blocks_1.BLOCKS.TABLE_HEADER_CELL, blocks_1.BLOCKS.TABLE_CELL])('fails with a invalid block node as children (nodeType: %s) of the root node', function (nodeType) { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { // @ts-expect-error we force a wrong node type to check that it fails nodeType: nodeType, content: [], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'blockquote', 'embedded-asset-block', 'embedded-entry-block', 'embedded-resource-block', 'heading-1', 'heading-2', 'heading-3', 'heading-4', 'heading-5', 'heading-6', 'hr', 'ordered-list', 'paragraph', 'table', 'unordered-list', ], name: 'in', path: ['content', 0, 'nodeType'], value: nodeType, }, ]); }); it.each(Object.values(inlines_1.INLINES))('fails with a inline node (%s) as direct children of the root node', function (nodeType) { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { // @ts-expect-error we force a wrong node type to check that it fails nodeType: nodeType, content: [], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'blockquote', 'embedded-asset-block', 'embedded-entry-block', 'embedded-resource-block', 'heading-1', 'heading-2', 'heading-3', 'heading-4', 'heading-5', 'heading-6', 'hr', 'ordered-list', 'paragraph', 'table', 'unordered-list', ], name: 'in', path: ['content', 0, 'nodeType'], value: nodeType, }, ]); }); it('fails with text as a direct children of the root node', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { // @ts-expect-error we force a wrong node type to check that it fails nodeType: 'text', data: {}, marks: [], value: 'Hello World', }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'blockquote', 'embedded-asset-block', 'embedded-entry-block', 'embedded-resource-block', 'heading-1', 'heading-2', 'heading-3', 'heading-4', 'heading-5', 'heading-6', 'hr', 'ordered-list', 'paragraph', 'table', 'unordered-list', ], name: 'in', path: ['content', 0, 'nodeType'], value: 'text', }, ]); }); it('fails with inline node and text as a direct children of the root node', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { // @ts-expect-error we force a wrong node type to check that it fails nodeType: 'text', data: {}, marks: [], value: 'Hello World', }, { // @ts-expect-error we force a wrong node type to check that it fails nodeType: inlines_1.INLINES.ASSET_HYPERLINK, data: { target: {} }, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'blockquote', 'embedded-asset-block', 'embedded-entry-block', 'embedded-resource-block', 'heading-1', 'heading-2', 'heading-3', 'heading-4', 'heading-5', 'heading-6', 'hr', 'ordered-list', 'paragraph', 'table', 'unordered-list', ], name: 'in', path: ['content', 0, 'nodeType'], value: 'text', }, ]); }); it.each([blocks_1.BLOCKS.OL_LIST, blocks_1.BLOCKS.UL_LIST])('fails for invalid block nodes inside of (%s)', function (nodeType) { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: nodeType, content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: {} }], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [blocks_1.BLOCKS.LIST_ITEM], name: 'in', path: ['content', 0, 'content', 0, 'nodeType'], value: blocks_1.BLOCKS.PARAGRAPH, }, ]); }); it('fails on text node directly inside of a list item node', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.UL_LIST, content: [ { nodeType: blocks_1.BLOCKS.LIST_ITEM, content: [ { nodeType: 'text', data: {}, value: 'Hello World', marks: [], }, ], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'blockquote', 'embedded-asset-block', 'embedded-entry-block', 'embedded-resource-block', 'heading-1', 'heading-2', 'heading-3', 'heading-4', 'heading-5', 'heading-6', 'hr', 'ordered-list', 'paragraph', 'unordered-list', ], name: 'in', path: ['content', 0, 'content', 0, 'content', 0, 'nodeType'], value: 'text', }, ]); }); it('fails on invalid block nodes inside of a table node', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.TABLE, content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: {} }], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [blocks_1.BLOCKS.TABLE_ROW], name: 'in', path: ['content', 0, 'content', 0, 'nodeType'], value: blocks_1.BLOCKS.PARAGRAPH, }, ]); }); it('fails on invalid block nodes inside of a table row node', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.TABLE, content: [ { nodeType: blocks_1.BLOCKS.TABLE_ROW, content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: {} }], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [blocks_1.BLOCKS.TABLE_CELL, blocks_1.BLOCKS.TABLE_HEADER_CELL], name: 'in', path: ['content', 0, 'content', 0, 'content', 0, 'nodeType'], value: blocks_1.BLOCKS.PARAGRAPH, }, ]); }); it('fails on invalid block nodes inside of a table header node', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.TABLE, content: [ { nodeType: blocks_1.BLOCKS.TABLE_ROW, content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: {} }], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [blocks_1.BLOCKS.TABLE_CELL, blocks_1.BLOCKS.TABLE_HEADER_CELL], name: 'in', path: ['content', 0, 'content', 0, 'content', 0, 'nodeType'], value: blocks_1.BLOCKS.PARAGRAPH, }, ]); }); it.each([blocks_1.BLOCKS.TABLE_CELL, blocks_1.BLOCKS.TABLE_HEADER_CELL])('fails on invalid node inside of %s', function (nodeType) { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.TABLE, content: [ { nodeType: blocks_1.BLOCKS.TABLE_ROW, content: [ { nodeType: nodeType, content: [{ nodeType: 'text', data: {}, marks: [], value: 'Hello World' }], data: {}, }, ], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [blocks_1.BLOCKS.PARAGRAPH], name: 'in', path: ['content', 0, 'content', 0, 'content', 0, 'content', 0, 'nodeType'], value: 'text', }, ]); }); it('fails if a table node has not at least one table row', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.TABLE, content: [], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Size must be at least 1', min: 1, name: 'size', path: ['content', 0, 'content'], value: [], }, ]); }); it('fails if a table row node has not at least one table cell', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.TABLE, content: [ { nodeType: blocks_1.BLOCKS.TABLE_ROW, content: [], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Size must be at least 1', min: 1, name: 'size', path: ['content', 0, 'content', 0, 'content'], value: [], }, ]); }); it.each([blocks_1.BLOCKS.TABLE_CELL, blocks_1.BLOCKS.TABLE_HEADER_CELL])('fails if a %s has not at least one child', function (nodeType) { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.TABLE, content: [ { nodeType: blocks_1.BLOCKS.TABLE_ROW, content: [ { nodeType: nodeType, content: [], data: {}, }, ], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Size must be at least 1', min: 1, name: 'size', path: ['content', 0, 'content', 0, 'content', 0, 'content'], value: [], }, ]); }); it('fails if inline nodes contains something else as a inline node or text', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [ { nodeType: inlines_1.INLINES.HYPERLINK, // @ts-expect-error we force a wrong node type to check that it fails content: [{ nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [], data: {} }], data: { uri: '' }, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: ['text'], name: 'in', path: ['content', 0, 'content', 0, 'content', 0, 'nodeType'], value: blocks_1.BLOCKS.PARAGRAPH, }, ]); }); it.each([ blocks_1.BLOCKS.HEADING_1, blocks_1.BLOCKS.HEADING_2, blocks_1.BLOCKS.HEADING_3, blocks_1.BLOCKS.HEADING_4, blocks_1.BLOCKS.HEADING_5, blocks_1.BLOCKS.HEADING_6, ])('fails if the headline node (%s) contains something else as a inline or text node', function (nodeType) { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: nodeType, content: [ { nodeType: blocks_1.BLOCKS.QUOTE, content: [{ nodeType: 'text', value: 'Hello World', data: {}, marks: [] }], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [ 'asset-hyperlink', 'embedded-entry-inline', 'embedded-resource-inline', 'entry-hyperlink', 'hyperlink', 'resource-hyperlink', 'text', ], name: 'in', path: ['content', 0, 'content', 0, 'nodeType'], value: blocks_1.BLOCKS.QUOTE, }, ]); }); it('fails on invalid block nodes inside of a quote node', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.QUOTE, content: [ { nodeType: blocks_1.BLOCKS.HEADING_1, content: [{ nodeType: 'text', value: 'Hello World', data: {}, marks: [] }], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'Value must be one of expected values', expected: [blocks_1.BLOCKS.PARAGRAPH], name: 'in', path: ['content', 0, 'content', 0, 'nodeType'], value: blocks_1.BLOCKS.HEADING_1, }, ]); }); it('fails without value property on text nodes', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [ { nodeType: 'text', value: null, data: {}, marks: [], }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The type of "value" is incorrect, expected type: String', name: 'type', path: ['content', 0, 'content', 0, 'value'], type: 'String', value: null, }, ]); }); it('fails with invalid row/colspan on table cell nodes', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.TABLE, content: [ { nodeType: blocks_1.BLOCKS.TABLE_ROW, content: [ { nodeType: blocks_1.BLOCKS.TABLE_CELL, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [{ nodeType: 'text', value: 'Hello Table', data: {}, marks: [] }], data: {}, }, ], data: { rowspan: 'argh' }, }, ], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The type of "rowspan" is incorrect, expected type: Number', name: 'type', path: ['content', 0, 'content', 0, 'content', 0, 'data', 'rowspan'], type: 'Number', value: 'argh', }, ]); }); it.each([inlines_1.INLINES.ASSET_HYPERLINK, inlines_1.INLINES.ENTRY_HYPERLINK, inlines_1.INLINES.RESOURCE_HYPERLINK])('fails with invalid properties for %s', function (nodeType) { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [ { nodeType: nodeType, data: {}, content: [{ nodeType: 'text', value: "Hello ".concat(nodeType), data: {}, marks: [] }], }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The property "target" is required here', name: 'required', path: ['content', 0, 'content', 0, 'data', 'target'], }, ]); }); it('fails with invalid properties for hypperlink node', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [ { nodeType: inlines_1.INLINES.HYPERLINK, data: {}, content: [{ nodeType: 'text', value: 'Hello hyperlink', data: {}, marks: [] }], }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The property "uri" is required here', name: 'required', path: ['content', 0, 'content', 0, 'data', 'uri'], }, ]); }); it.each([inlines_1.INLINES.EMBEDDED_ENTRY, inlines_1.INLINES.EMBEDDED_RESOURCE])('fails with invalid properties for %s', function (nodeType) { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [ { nodeType: nodeType, data: {}, content: [], }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([ { details: 'The property "target" is required here', name: 'required', path: ['content', 0, 'content', 0, 'data', 'target'], }, ]); }); it('succeeds with a valid structure', function () { var document = { nodeType: blocks_1.BLOCKS.DOCUMENT, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [{ nodeType: 'text', value: 'Hello World', data: {}, marks: [] }], data: {}, }, { nodeType: blocks_1.BLOCKS.HEADING_1, content: [{ nodeType: 'text', value: 'Hello Headline', data: {}, marks: [] }], data: {}, }, { nodeType: blocks_1.BLOCKS.UL_LIST, content: [ { nodeType: blocks_1.BLOCKS.LIST_ITEM, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [{ nodeType: 'text', value: 'Hello List', data: {}, marks: [] }], data: {}, }, ], data: {}, }, ], data: {}, }, { nodeType: blocks_1.BLOCKS.TABLE, content: [ { nodeType: blocks_1.BLOCKS.TABLE_ROW, content: [ { nodeType: blocks_1.BLOCKS.TABLE_CELL, content: [ { nodeType: blocks_1.BLOCKS.PARAGRAPH, content: [{ nodeType: 'text', value: 'Hello Table', data: {}, marks: [] }], data: {}, }, ], data: { rowspan: 2, colspan: 2 }, }, ], data: {}, }, ], data: {}, }, ], data: {}, }; expect((0, validator_1.validateRichTextDocument)(document)).toEqual([]); }); }); //# sourceMappingURL=validation.test.js.map