Data Schema

WeaveDB utilizes JSON Schema (opens in a new tab) to validate incoming data.

It's essential to set a precise data schema and access control rules to each collection as otherwise WeaveDB is permissionless and anyone can put arbitrary data.

To validate write data, WeaveDB uses jsonschema (opens in a new tab) with a restriction that you cannot pass validator functions.

In this example, we consider bookmarks as the name of our collection.

Set a schema to a collection:

const schema = {
  type: "object",
  required: ["article_id", "date", "user_address"],
  properties: {
    article_id: {
      type: "string",
    },
    user_address: {
      type: "string",
    },
    date: {
      type: "number",
    },
  },
}
await db.setSchema(schema, "bookmarks")
💡

In the following sample code, db represents the state variable of your database instance. For reference, see Initialize WeaveDB

Get the schema of a collection:

await db.getSchema("bookmarks")

Remove a schema from a collection:

await db.removeSchema(schema, "bookmarks")