If you want to update this page or add new content, please submit a pull request to the Homepage.
Schema Validation
Schema Validation allows you to declaratively define document structures and enforce them during collaborative editing. When a schema is attached to a document, every local edit is validated against the schema rules, preventing invalid changes from being applied.
How It Works
Schemas are defined using a TypeScript-like DSL, stored on the server, and delivered to clients when they attach a document. The SDK validates every doc.update() call against the schema rules before applying changes.
- Define a schema in the Dashboard using the Schema DSL
- A client attaches a document with a schema reference
- The server validates that the existing document conforms to the schema
- Schema rules are sent to the client along with the document
- The client calls
doc.update()to edit the document - The SDK validates the updated document against the schema rules
- If valid, changes are applied; if invalid, the update is rejected with an error
Defining a Schema
Schemas use a TypeScript Type Alias syntax to define document structures. You can create schemas in the Dashboard's schema editor.
Basic Example
1type Document = {2 title: string;3 completed: boolean;4 priority: integer;5};
Rich Document Example
1type Document = {2 title: string;3 content: yorkie.Tree<{4 doc: { content: "block+"; };5 paragraph: { content: "text*"; marks: "bold italic link"; group: "block"; };6 heading: { content: "text*"; marks: "bold"; group: "block"; };7 blockquote: { content: "block+"; group: "block"; };8 text: {};9 }>;10 tags: yorkie.Array<string>;11 metadata: {12 author: string;13 updatedAt: date;14 };15};
Supported Types
| Category | Types | Description |
|---|---|---|
| Primitive | string, boolean, integer, double, long, date, bytes, null | Basic value types |
| Yorkie | yorkie.Text | Collaborative plain text |
| Yorkie | yorkie.Tree | Collaborative rich text tree (supports Tree Schema) |
| Yorkie | yorkie.Counter | Collaborative counter |
| Yorkie | yorkie.Object | Collaborative JSON object |
| Yorkie | yorkie.Array | Collaborative JSON array |
| Structural | Optional (?), Union (|), Nested objects | Type composition |
Tree Schema
For yorkie.Tree, you can define node types with ProseMirror-compatible content expressions:
| Field | Description | Example |
|---|---|---|
content | Allowed children as a content expression | "text*", "block+", "(heading | paragraph)+" |
marks | Space-separated allowed mark types | "bold italic link" |
group | Group name referenced in other content expressions | "block" |
Content expression syntax:
| Syntax | Meaning |
|---|---|
name | Exactly one node of this type or group |
name* | Zero or more |
name+ | One or more |
name? | Zero or one |
a b | Sequence (a followed by b) |
a | b | Alternative (a or b) |
Attaching a Schema to a Document
Specify a schema when attaching a document using the schema option:
1import yorkie from 'yorkie-js-sdk';23const client = new yorkie.Client('https://api.yorkie.dev');4await client.activate();56const doc = new yorkie.Document('my-doc');7await client.attach(doc, {8 schema: 'my-schema@1', // Format: "schema-name@version"9});
The schema reference uses the format name@version, where:
| Part | Description |
|---|---|
name | The schema name as created in the Dashboard |
version | The schema version number (1, 2, 3, ...) |
Handling Validation Errors
When a doc.update() call violates the schema, the SDK throws an error. Use try-catch to handle validation failures:
1try {2 doc.update((root) => {3 root.title = 123; // Error: schema expects string4 });5} catch (err) {6 if (err.code === 'ErrDocumentSchemaValidationFailed') {7 console.error('Schema violation:', err.message);8 // e.g., "schema validation failed: expected string at path $.title"9 }10}
Validation Behavior in Concurrent Editing
Schema validation uses a best-effort approach for collaborative environments:
| Scenario | Behavior |
|---|---|
| Local edits | Validated against schema rules. Invalid changes are rejected with an error. |
| Remote changes | Applied without validation. Temporary violations may occur. |
| Next local edit | Validated against the current state, including remote changes. |
This means that after receiving remote changes, the document may temporarily be in a state that doesn't conform to the schema. The next local edit will be validated against the current state, so any structural issues must be resolved before further local edits can succeed.
Schema Version Management
Schemas are managed immutably. Each update creates a new version:
- v1: Initial schema definition
- v2: Updated schema with new fields or modified constraints
- v3: Further revisions
Documents remain bound to the schema version they were attached with. To use a new schema version, detach and reattach the document with the updated schema reference.
Use Cases
- Structured documents: Enforce consistent structure for collaborative forms, surveys, or configuration editors
- Rich text editors: Define allowed node types, content models, and marks for ProseMirror-compatible editors
- Data integrity: Prevent invalid data types from being written to shared documents
- Multi-client consistency: Ensure all clients editing a document follow the same structural rules