From a4cdff127c79ef16a17a308a686bdfdda5edec85 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Thu, 30 Nov 2023 16:38:41 +0200 Subject: [PATCH 1/4] add design --- .gitignore | 1 + DESIGN.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 DESIGN.md diff --git a/.gitignore b/.gitignore index 206c723..74b87da 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ dist/ markdown.db *.tgz .markdowndb/ +.next/ \ No newline at end of file diff --git a/DESIGN.md b/DESIGN.md new file mode 100644 index 0000000..c1d2531 --- /dev/null +++ b/DESIGN.md @@ -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. From f8e4bc77b35120c0adaa948c56ce4bc11804e199 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Tue, 5 Dec 2023 02:49:53 +0200 Subject: [PATCH 2/4] Update Document types design - Use Zod for scheme validation - Add an explanation of how markdowndb validates document types --- DESIGN.md | 58 +++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index c1d2531..8266fb0 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -1,50 +1,36 @@ -### Define Your Schema +### Define Your Schema with Zod Scheming -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: +MarkdownDB leverages the powerful Zod library for schema definition, allowing you to specify the structure and validation rules for your Markdown content. Below is an example configuration demonstrating the usage of Zod to define a schema for a blog post with a mandatory `date` field. ```javascript // markdowndb.config.js +import { z } from "zod"; module.exports = { - schemas: { + schemas: z.object({ 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 + date: z.string().refine((value) => /\d{4}-\d{2}-\d{2}/.test(value), { + message: "Invalid date format. Please use YYYY-MM-DD.", + }), + // Add more fields as needed, each with its own validation rules + // Example: + // title: z.string().min(1, "Title must have at least 1 character"), + // content: z.string(), }, - // Define additional schemas if necessary - }, + // Define additional schemas for different content types + // Example: + // page: { + // author: z.string(), + // text: z.string(), + // }, + }), }; ``` -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. +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 -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. +When you load a Markdown file using MarkdownDB, it automatically validates the content against the defined schema using Zod. If the validation fails for any field, MarkdownDB will throw an error, providing detailed error messages. This helps you quickly identify and rectify issues, ensuring your Markdown content adheres to the specified structure and validation rules. + +Feel free to extend the schema by adding more fields and their respective validation rules as needed for your specific use case. This flexibility allows you to define complex structures and ensure the integrity of your data. From d2538b8c22a109a730178671ff2764fb25c6907b Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Thu, 7 Dec 2023 01:22:26 +0200 Subject: [PATCH 3/4] Update .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 74b87da..206c723 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ dist/ markdown.db *.tgz .markdowndb/ -.next/ \ No newline at end of file From e2b2225daa45739e3da5f1dd0b78add5c4dfcd5f Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Fri, 8 Dec 2023 10:07:23 +0200 Subject: [PATCH 4/4] Examples of error messages during schema validation --- DESIGN.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 8266fb0..94c117b 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -10,7 +10,7 @@ module.exports = { schemas: z.object({ post: { date: z.string().refine((value) => /\d{4}-\d{2}-\d{2}/.test(value), { - message: "Invalid date format. Please use YYYY-MM-DD.", + message: "Invalid date format. Please use YYYY-MM-DD format for the 'date' field.", }), // Add more fields as needed, each with its own validation rules // Example: @@ -27,10 +27,11 @@ module.exports = { }; ``` -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 -When you load a Markdown file using MarkdownDB, it automatically validates the content against the defined schema using Zod. If the validation fails for any field, MarkdownDB will throw an error, providing detailed error messages. This helps you quickly identify and rectify issues, ensuring your Markdown content adheres to the specified structure and validation rules. +In this example, the `post` schema is defined using Zod's `object` method, specifying the structure of the content. Each field within the schema, such as `date`, is assigned validation rules using Zod's methods, ensuring data integrity. + +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: -Feel free to extend the schema by adding more fields and their respective validation rules as needed for your specific use case. This flexibility allows you to define complex structures and ensure the integrity of your data. +- 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.` \ No newline at end of file