Skip to content

Commit

Permalink
fix(core): check schema's nesting rules on contentCheck (#5500) (#5535)
Browse files Browse the repository at this point in the history
  • Loading branch information
lepult authored Aug 21, 2024
1 parent f8d79da commit 8d8d999
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/mean-pets-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---

fix: check for schema's nesting rules on contentCheck
8 changes: 7 additions & 1 deletion packages/core/src/helpers/createNodeFromContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
21 changes: 21 additions & 0 deletions tests/cypress/integration/core/createNodeFromContent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

0 comments on commit 8d8d999

Please sign in to comment.