Skip to content

Commit

Permalink
add design
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsalem401 committed Nov 30, 2023
1 parent 6e67120 commit a4cdff1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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/
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 = {
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) {
// 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

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.

0 comments on commit a4cdff1

Please sign in to comment.