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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist/
markdown.db
*.tgz
.markdowndb/
.next/
mohamedsalem401 marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 50 additions & 0 deletions DESIGN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
### Define Your Schema

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:

```javascript
// markdowndb.config.js

module.exports = {
mohamedsalem401 marked this conversation as resolved.
Show resolved Hide resolved
schemas: {
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) {
mohamedsalem401 marked this conversation as resolved.
Show resolved Hide resolved
mohamedsalem401 marked this conversation as resolved.
Show resolved Hide resolved
// 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
},
// Define additional schemas if necessary
},
};
```

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.

### 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.


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.
Loading