fix(core): check schema's nesting rules on contentCheck (#5500) (#5535)

This commit is contained in:
Leopold Pinkernell 2024-08-21 22:38:43 +02:00 committed by Nick the Sick
parent a22767e9e1
commit f805333f63
No known key found for this signature in database
GPG Key ID: F575992F156E5BCC
3 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---
fix: check for schema's nesting rules on contentCheck

View File

@ -45,7 +45,13 @@ export function createNodeFromContent(
return Fragment.fromArray(content.map(item => schema.nodeFromJSON(item)))
}
return schema.nodeFromJSON(content)
const node = schema.nodeFromJSON(content)
if (options.errorOnInvalidContent) {
node.check()
}
return node
} catch (error) {
if (options.errorOnInvalidContent) {
throw new Error('[tiptap error]: Invalid JSON content', { cause: error as Error })

View File

@ -242,4 +242,25 @@ describe('createNodeFromContent', () => {
]), { errorOnInvalidContent: true })
}).to.throw('[tiptap error]: Invalid JSON content')
})
it('if `errorOnInvalidContent` is true, will throw an error, when the JSON content does not follow the nesting rules of the schema', () => {
const content = {
type: 'paragraph',
content: [{
type: 'paragraph',
content: [{
type: 'text',
text: 'Example Text',
}],
}],
}
expect(() => {
createNodeFromContent(content, getSchemaByResolvedExtensions([
Document,
Paragraph,
Text,
]), { errorOnInvalidContent: true })
}).to.throw('[tiptap error]: Invalid JSON content')
})
})