-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ #55 , Data Validation and Document Types ] #88
Conversation
mohamedsalem401
commented
Dec 11, 2023
- Use zod
- Example "in code" for how to add schema validation to markdowndb
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The writing is excellent @mohamedsalem401 👏. Really clear, well-written example. Super easy to review 🙏👏
🏅
A few suggestions you could use to improve if you want.
## Define Your Schemas | ||
|
||
Create a configuration object and use the Zod library to define your schemas. For more information on defining Zod schemas, visit [Zod Documentation - Basic Usage](https://zod.dev/?id=basic-usage). In this example, we'll begin by creating a schema for a blog post. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd first show what the blog post markdown file looks like e.g.
Imagine that you have a folder of blog posts that you want to display.
Each blog post should have a well formatted date so you can show the date and display posts in the right order.
But it's easy to forget that date - or type it wrong. And then your site may crash (or look bad)!
How can we ensure each post has a good `date` field like this ...
// INSERT minimal example of blog post with date frontmatter e.g.
---
title: ...
date: 2023-12-05
...
---
My awesome post
...
|
||
MarkdownDB automatically validates your Markdown content against the defined schemas using Zod. Here's how the validation process works: | ||
|
||
- **Schemas Definition:** The `post` schema is defined using Zod's `object` method, specifying the structure of the content. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're going to need a way to specify which schemas apply to which files ... HOWEVER, we can leave that question to later. This is a great simple example!
Error: Missing 'date' field in 'blog.md' for the 'post' schema. | ||
``` | ||
|
||
Now you have robust schemas in place, ensuring the integrity of your Markdown content. Feel free to extend and customize the schemas to meet the specific requirements of your project. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like the tone. 👏