Skip to content

Commit

Permalink
[#55,doctypes,README][m]: add a design sketch of the document types f…
Browse files Browse the repository at this point in the history
…unctionality in README.


- add design
- Use Zod for scheme validation
- Add an explanation of how markdowndb validates document types
- Examples of error messages during schema validation
  • Loading branch information
mohamedsalem401 authored Dec 8, 2023
1 parent 3ba2c81 commit ac80a1f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions DESIGN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### Define Your Schema with Zod Scheming

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: z.object({
post: {
date: z.string().refine((value) => /\d{4}-\d{2}-\d{2}/.test(value), {
message: "Invalid date format. Please use YYYY-MM-DD format for the 'date' field.",
}),
// 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 for different content types
// Example:
// page: {
// author: z.string(),
// text: z.string(),
// },
}),
};
```

### How Validation Works

In this example, the `post` schema is defined using Zod's `object` method, specifying the structure of the content. Each field within the schema, such as `date`, is assigned validation rules using Zod's methods, ensuring data integrity.

When MarkdownDB loads a Markdown file, it automatically validates the content against the defined schema using Zod. If any field fails validation, MarkdownDB throws an error with a detailed message indicating the specific issue. For instance:

- If the date has an invalid format, it throws an error like this: `Error: In 'blog.md' for the 'post' schema. Invalid date format. Please use YYYY-MM-DD format for the 'date' field.`
- If a required field is missing, it throws an error like this: `Error: Missing 'date' field in 'blog.md' for the 'post' schema.`

0 comments on commit ac80a1f

Please sign in to comment.