-
-
Notifications
You must be signed in to change notification settings - Fork 684
/
Copy pathdata-validate.md
34 lines (26 loc) · 1.05 KB
/
data-validate.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
---
eleventyNavigation:
parent: Data Cascade
key: Validate Data
order: 6
---
# Validate Data
Use the special `eleventyDataSchema` data property to validate data in your Data Cascade. You can set this anywhere in your Data Cascade (front matter, directory data file, global data, etc).
You can use any schema or validation library to achieve this. In this example, we’re using [`zod`](https://zod.dev/).
## Example: Checking that `draft` is boolean
In the following example, each content template with an `eleventyDataSchema` callback (in this example, any templates in the `blog` folder) is checked to make sure the value of any `draft` assignments must be `boolean` or `undefined`. If not, we throw an error.
<div class="codetitle">blog/blog.11tydata.js</div>
```js
import { z } from "zod";
import { fromZodError } from 'zod-validation-error';
export default {
eleventyDataSchema: function(data) {
let result = z.object({
draft: z.boolean().or(z.undefined()),
}).safeParse(data);
if(result.error) {
throw fromZodError(result.error);
}
}
};
```