Skip to content
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

Merged
merged 6 commits into from
Dec 8, 2023
Merged

Add documents types design draft #77

merged 6 commits into from
Dec 8, 2023

Conversation

mohamedsalem401
Copy link
Contributor

No description provided.

Copy link

changeset-bot bot commented Nov 30, 2023

⚠️ No Changeset found

Latest commit: 3eed54a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

DESIGN.md Show resolved Hide resolved
DESIGN.md Outdated Show resolved Hide resolved
DESIGN.md Outdated Show resolved Hide resolved
- Use Zod for scheme validation
- Add an explanation of how markdowndb validates document types
@olayway olayway changed the title add design Add documents types design draft Dec 5, 2023
Copy link
Member

@rufuspollock rufuspollock left a 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
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rufuspollock

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. That's fine.

.gitignore Outdated Show resolved Hide resolved
@mohamedsalem401
Copy link
Contributor Author

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
}

It seems like you're interested in using the Zod annotations to create specific objects, such as a Post object, instead of just a generic File object when using getStaticProps in Next.js.

I think it should be like this:
1- Add type field in every file e.g. type: Post so that we can filter by this type property in the database.
2- Save every file object in the database so that it has the same properties as the scheme defined using zod.
3- To avoid using generic Typescript object we can use Zod to easily generate typescript declaration files.

@rufuspollock
Copy link
Member

It seems like you're interested in using the Zod annotations to create specific objects, such as a Post object, instead of just a generic File object when using getStaticProps in Next.js.

I think it should be like this:

1- Add type field in every file e.g. type: Post so that we can filter by this type property in the database.

Yes, or auto-add by folder.

2- Save every file object in the database so that it has the same properties as the scheme defined using zod.

Good question whether we "cast" on the way in or the way out. Probably best on the way in ...

3- To avoid using generic Typescript object we can use Zod to easily generate typescript declaration files.

Right. How would that work ... (and what would usage look like)

@mohamedsalem401
Copy link
Contributor Author

  1. Save every file object in the database so that it has the same properties as the schema defined using Zod.

Good question whether we "cast" on the way in or the way out. Probably best on the way in ...

  1. Should we start casting to .json files first or SQL DB first? Or can we make the simplest thing first and then add changes to any of them?
  2. What about fields that are common to every file (like tags and links)? Should I add them to every user-defined schema?
  1. To avoid using generic TypeScript objects, we can use Zod to easily generate TypeScript declaration files.

Right. How would that work, and what would usage look like?

We can extract the TypeScript type of any schema with z.infer<typeof mySchema>.

const A = z.string();
type AType = z.infer<typeof A>; // string

For more information, refer to the Zod documentation on type inference.

@rufuspollock
Copy link
Member

We can extract the TypeScript type of any schema with z.infer.

Yes i knew that. I meant how would it look like from a markdowndb user's code ....

@rufuspollock
Copy link
Member

I think this is "good enough to try" so merging now.

@rufuspollock rufuspollock merged commit ac80a1f into main Dec 8, 2023
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants