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 · 10 comments
Closed

Use eleventyDataSchema in Data Cascade to validate data #879

pdehaan opened this issue Jan 23, 2020 · 10 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?

@kfranqueiro
Copy link

Adding a comment here RE https://fosstodon.org/@eleventy/112446508013942858

Piggybacking on the Zod example above: Zod supports a couple of data-massaging features such as coercion and transforms, which result in the output differing from the input.

It would be great to be able to take advantage of these features via elventyDataSchema as well - for example, this could be used to coerce computed template strings in front matter to other types. However, IIUC eleventyDataSchema currently treats the validation as a read-only operation, incapable of overriding the original data?

(It's potentially worth noting for this use case that Zod's parse functions strip keys not found in the schema by default, but they can be kept using passthrough())

@zachleat
Copy link
Member

@uncenter I think you want this comment here:

"Unfortunately you’re using code that monkey patched some Eleventy internals and it isn’t async-friendly. Change your code to use the async `read()` method on the template instead!",

let {data} = await item.template.read();

(it should reuse the cached read promise, if any exists)

@uncenter
Copy link
Contributor

@uncenter I think you want this comment here:

"Unfortunately you’re using code that monkey patched some Eleventy internals and it isn’t async-friendly. Change your code to use the async `read()` method on the template instead!",

let {data} = await item.template.read();

(it should reuse the cached read promise, if any exists)

Thank you! Updated the plugin and now it works with the latest canary! 🤩

@zachleat zachleat added the needs-documentation Documentation for this issue/feature is pending! label May 30, 2024
zachleat added a commit to 11ty/11ty-website that referenced this issue Sep 25, 2024
@zachleat
Copy link
Member

Preview docs building here: https://11ty-website-git-v3-11ty.vercel.app/docs/data-validate/

@zachleat zachleat removed the needs-documentation Documentation for this issue/feature is pending! label Sep 25, 2024
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

4 participants