-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add documents types design draft #77
Conversation
|
- Use Zod for scheme validation - Add an explanation of how markdowndb validates document types
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.
LGTM as a first pass.
I'm not yet quite sure how you specify and use the zod annotations and how this then helps consume the objects e.g. i'd like to see an example where i get a Post object (rather than just a File
) in getStaticProps ...
e.g.
getStaticProps() {
markdowndb.get(type=posts) // returns created Post type object rather than File ... somehow
}
|
||
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 |
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.
Could you provide a short example of what output will look like?
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.
So I think what you mean by output
is either:
1- Error messages if a given file e.g. blog.md
is missing a field e.g. author
will be Error: Missing 'author' field in 'blog.md' for the 'POST' scheme. Please ensure that the required field is included in the file.
2- Typescript declaration files .d.ts
for nextjs Typescript users.
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.
@mohamedsalem401 i meant the first. And will that include the full path to the markdown file etc.
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.
@mohamedsalem401 i meant the first. And will that include the full path to the markdown file etc.
I have added the following section in the Design.md
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.
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.
OK. That's fine.
It seems like you're interested in using the Zod annotations to create specific objects, such as a I think it should be like this: |
Yes, or auto-add by folder.
Good question whether we "cast" on the way in or the way out. Probably best on the way in ...
Right. How would that work ... (and what would usage look like) |
We can extract the TypeScript type of any schema with const A = z.string();
type AType = z.infer<typeof A>; // string For more information, refer to the Zod documentation on type inference. |
Yes i knew that. I meant how would it look like from a markdowndb user's code .... |
I think this is "good enough to try" so merging now. |
No description provided.