JSON content validator #2215
Answered
by
christianheine
estarossa0
asked this question in
Questions & Help
-
Hi, I'm using json format to save editor content in my db, and I would like to know is there is a proper way to validate the received data from client as a valid editor content. thank you 🙏 |
Beta Was this translation helpful? Give feedback.
Answered by
christianheine
Feb 5, 2022
Replies: 2 comments 4 replies
-
i have the same question |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can retrieve the underlying ProseMirror node and check it for consistency. The import { Node } from "prosemirror-model";
import { Extensions, getSchema, JSONContent } from "@tiptap/core";
const validateSchema = (doc: JSONContent, extensions: Extensions): boolean => {
try {
const schema = getSchema(extensions);
const contentNode = Node.fromJSON(schema, doc);
contentNode.check();
return true;
} catch (e) {
return false;
}
}; |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
estarossa0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can retrieve the underlying ProseMirror node and check it for consistency. The
check
method will throw on invalid schemas. Below implementation was inspired by the generateHTML function - which is using a similar approach.