Skip to content

JSON content validator #2215

Discussion options

You must be logged in to vote

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.

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;
  }
};

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@CapitaineToinon
Comment options

@nabramow
Comment options

@jasonhao518
Comment options

@mathers101
Comment options

Answer selected by estarossa0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
7 participants