Skip to content

Commit

Permalink
[ #55 , Data Validation and Document Types ] (#88) ( not yet implemented
Browse files Browse the repository at this point in the history
* Add basic example

* Update the example
  • Loading branch information
mohamedsalem401 committed Dec 12, 2023
1 parent 91372eb commit 9493f58
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
67 changes: 67 additions & 0 deletions examples/document-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# MarkdownDB Schemas Configuration Tutorial

MarkdownDB enables you to establish and enforce the structure of your Markdown content using the Zod library for schema definitions. This tutorial guides you through the process of defining schemas for a blog post, ensuring a mandatory `date` field. The schemas play a crucial role in error prevention and ensure the predicted structure in Markdown files.

## 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. For instance:
```md
---
title: blog post
date: 2023-12-05
...
---

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
```

Subsequently, we'll use the `client.indexFolder` method along with the defined schemas to scan and index the folder containing your Markdown files.

```javascript
// index.js
import { z } from "zod";
import { MarkdownDB } from "mddb";

// Define the schemas using Zod
const schemas = {
post: z.object({
date: z.string().refine((value) => /\d{4}-\d{2}-\d{2}/.test(value), {
message:
"Invalid date format. Please use YYYY-MM-DD format for the 'date' field.",
}),
}),
};

// Code for initializing the connection
const client = new MarkdownDB({
client: "sqlite3",
connection: {
filename: "markdown.db",
},
});
const mddb = await client.init();

// Index the folder with MarkdownDB
await client.indexFolder({
folderPath: contentPath,
schemas: schemas,
});
```

## How Validation Works

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.
- **Validation Errors:** If validation fails, MarkdownDB throws an error with a detailed message. For example:
- 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.
```

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.
30 changes: 30 additions & 0 deletions examples/document-types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from "zod";
import { MarkdownDB } from "mddb";

const dbPath = "markdown.db";
const client = new MarkdownDB({
client: "sqlite3",
connection: {
filename: dbPath,
},
});
await client.init();

// Define the schemas using zod
const schemas = {
post: z.object({
date: z.string().refine((value) => /\d{4}-\d{2}-\d{2}/.test(value), {
message:
"Invalid date format. Please use YYYY-MM-DD format for the 'date' field.",
}),
}),
};

// Index the folder with the files
await client.indexFolder({
folderPath: contentPath,
ignorePatterns: ignorePatterns,
schemas: schemas
});

process.exit(0);

0 comments on commit 9493f58

Please sign in to comment.