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

Use eleventyDataSchema in Data Cascade to validate data #879

Closed
pdehaan opened this issue Jan 23, 2020 · 6 comments
Closed

Use eleventyDataSchema in Data Cascade to validate data #879

pdehaan opened this issue Jan 23, 2020 · 6 comments
Labels
enhancement feature: 🛠 configuration Related to Eleventy’s Configuration file feature: 🏔 data cascade Issues related to the Data Cascade feature

Comments

@pdehaan
Copy link
Contributor

pdehaan commented Jan 23, 2020

https://www.11ty.dev/docs/config/#linters are super cool, but I was hoping to have a linter for the raw input files so I could do a front-matter linter which would throw errors or otherwise report if a file was missing a "required" field in a file's front-matter.

Is something like that possible, or should I try and do that while defining a custom collection?
Or should it be something that I write separately using glob and grey-matter as a separate script?

@zachleat zachleat added enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. and removed education labels Apr 9, 2024
@zachleat
Copy link
Member

zachleat commented Apr 9, 2024

You could do this with the new #1206 access to rawInput and that might be the best option for now. Filing this as an enhancement request!

Marginally related to #867!

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don’t forget to upvote the top comment with 👍!

@zachleat zachleat closed this as completed Apr 9, 2024
@zachleat zachleat added feature: 🏔 data cascade Issues related to the Data Cascade feature feature: 🛠 configuration Related to Eleventy’s Configuration file labels Apr 16, 2024
@zachleat
Copy link
Member

Seems related to #3251 too

@zachleat zachleat added this to the Eleventy 3.0.0 milestone Apr 17, 2024
@zachleat zachleat removed the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label Apr 17, 2024
@zachleat
Copy link
Member

zachleat commented Apr 17, 2024

This is shipping in 3.0.0-alpha.7 as the eleventyDataSchema property (function callback, async-friendly) in the data cascade. Here’s an example using zod in a directory data file (e.g. blog.11tydata.js) that checks to make sure a draft property is boolean:

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);
		}
	}
};

@zachleat zachleat changed the title Linters for frontmatter in templates? Use eleventyDataSchema in Data Cascade to validate data Apr 17, 2024
@zachleat
Copy link
Member

Shout out to https://github.com/uncenter/eleventy-plugin-validate by @uncenter

@uncenter
Copy link
Contributor

uncenter commented Apr 18, 2024

Shout out to uncenter/eleventy-plugin-validate by @uncenter

Thanks! While we are on this topic... is there an easier way to narrow down the source of data? I'm using item.template._frontMatter?.data at the moment to just look at front matter and I can't tell if this new feature lets me do that or not.

@uncenter
Copy link
Contributor

is shipping in 3.0.0-alpha.7 as the eleventyDataSchema property (function callback, async-friendly) in the data cascade. Here’s an example using zod in a directory data file (e.g. blog.11tydata.js) that checks to make sure a draft property is boolean:

Looks like my little hack to access specifically front matter data got removed... 70df967#r141478000. It would be nice to be able to still, maybe with an unprefixed property?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature: 🛠 configuration Related to Eleventy’s Configuration file feature: 🏔 data cascade Issues related to the Data Cascade feature
Projects
None yet
Development

No branches or pull requests

3 participants