-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use Zod for scheme validation - Add an explanation of how markdowndb validates document types
- Loading branch information
1 parent
a4cdff1
commit f8e4bc7
Showing
1 changed file
with
22 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,36 @@ | ||
### Define Your Schema | ||
### Define Your Schema with Zod Scheming | ||
|
||
To define the schema, you can use MarkdownDB's flexible schema definition syntax. Below is an example for a blog post with a mandatory `date` field: | ||
MarkdownDB leverages the powerful Zod library for schema definition, allowing you to specify the structure and validation rules for your Markdown content. Below is an example configuration demonstrating the usage of Zod to define a schema for a blog post with a mandatory `date` field. | ||
|
||
```javascript | ||
// markdowndb.config.js | ||
import { z } from "zod"; | ||
|
||
module.exports = { | ||
schemas: { | ||
schemas: z.object({ | ||
post: { | ||
date: { | ||
type: "string", | ||
required: true, | ||
validate: (fileObject) => { | ||
const dateFormat = /\d{4}-\d{2}-\d{2}/; // YYYY-MM-DD | ||
if (!dateFormat.test(fileObject.date)) { | ||
return { | ||
status: false, | ||
message: "Invalid date format. Please use YYYY-MM-DD.", | ||
}; | ||
} | ||
return { | ||
status: true, | ||
}; | ||
}, | ||
compute(fileObject, ast) { | ||
// this function returns the first date in a file | ||
const nodes = selectAll("*", ast); | ||
nodes.map((node: any) => { | ||
if (node.type === "text") { | ||
const dateFormat = /\d{4}-\d{2}-\d{2}/; | ||
if (!node.value.test(fileObject.date)) { | ||
return node.value; | ||
} | ||
} | ||
}); | ||
}, | ||
}, | ||
// Add more fields as needed | ||
date: z.string().refine((value) => /\d{4}-\d{2}-\d{2}/.test(value), { | ||
message: "Invalid date format. Please use YYYY-MM-DD.", | ||
}), | ||
// Add more fields as needed, each with its own validation rules | ||
// Example: | ||
// title: z.string().min(1, "Title must have at least 1 character"), | ||
// content: z.string(), | ||
}, | ||
// Define additional schemas if necessary | ||
}, | ||
// Define additional schemas for different content types | ||
// Example: | ||
// page: { | ||
// author: z.string(), | ||
// text: z.string(), | ||
// }, | ||
}), | ||
}; | ||
``` | ||
|
||
In this example, the `date` field is defined as a required string. It also includes custom validation logic to ensure that the date follows the format YYYY-MM-DD. | ||
In this example, the `post` schema is defined using Zod's `object` method, specifying the structure of the content. The `date` field is required and must adhere to the specified date format using Zod's `string` and `refine` methods for custom validation. | ||
|
||
### How Validation Works | ||
|
||
When you load a Markdown file, MarkdownDB will validate it against the specified schema. If the validation fails, MarkdownDB will throw an error, providing detailed error messages to help you identify and fix the issues. | ||
When you load a Markdown file using MarkdownDB, it automatically validates the content against the defined schema using Zod. If the validation fails for any field, MarkdownDB will throw an error, providing detailed error messages. This helps you quickly identify and rectify issues, ensuring your Markdown content adheres to the specified structure and validation rules. | ||
|
||
Feel free to extend the schema by adding more fields and their respective validation rules as needed for your specific use case. This flexibility allows you to define complex structures and ensure the integrity of your data. |