# eleventy-plugin-validate

> A plugin for Eleventy to validate collection data.

Latest version **0.1.3** (published 2024-05-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install eleventy-plugin-validate
pnpm add eleventy-plugin-validate
yarn add eleventy-plugin-validate
bun add eleventy-plugin-validate
```

## Health

**Score 50/100 (C)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2024-05-16 |
| First published | 2024-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 15.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 11 |
| Author | uncenter |
| Maintainers | uncenter |
| Keywords | 11ty, eleventy, eleventy-plugin |

## Links

- npm: https://www.npmjs.com/package/eleventy-plugin-validate
- Repository: https://github.com/uncenter/eleventy-plugin-validate
- Homepage: https://github.com/uncenter/eleventy-plugin-validate#readme
- Issues: https://github.com/uncenter/eleventy-plugin-validate/issues
- npm.io page: https://npm.io/package/eleventy-plugin-validate

## Dependencies (3)

- [zod](https://npm.io/package/zod.md) ^3.22.4
- [kleur](https://npm.io/package/kleur.md) ^4.1.5
- [just-extend](https://npm.io/package/just-extend.md) ^6.2.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.1.3 (latest) — 2024-05-16
- 0.1.2 — 2024-01-18
- 0.1.1 — 2024-01-07
- 0.1.0 — 2024-01-07
- 0.0.4 — 2024-01-05
- 0.0.3 — 2024-01-05
- 0.0.2 — 2024-01-04
- 0.0.1 — 2024-01-04
- 0.0.0 — 2024-01-04

## README

# eleventy-plugin-validate

## Installation

```
npm i eleventy-plugin-validate
pnpm add eleventy-plugin-validate
yarn add eleventy-plugin-validate
bun add eleventy-plugin-validate
```

## Usage

Setup the plugin in your [Eleventy configuration file](https://www.11ty.dev/docs/config/#default-filenames).

### CJS

```js
const pluginValidate = require('eleventy-plugin-validate');
const { z } = require('zod');

module.exports = (eleventyConfig) => {
  eleventyConfig.addPlugin(pluginValidate, {
    // Select the Zod library for schemas:
    validator: 'zod',
    schemas: [
      {
        // `collections: ['posts']` tells the plugin
        // to run this schema on the 'posts' collection.
        // If you omit this property, the schema will run against
        // collection items from the 'all' collection (a default
        // collection that Eleventy generates for you).
        collections: ['posts'],

        // `schema` should be a schema made with the validator
        // library selected in the above 'validator' property.
        schema: z
          .object({
            title: z.string(),
            description: z.string(),
            draft: z.boolean(),
          })
          // I suggest adding .strict() to your schema
          // for even more accurate validation.
          // With .strict(), extra properties
          // you have not specified in the schema object
          // will cause an error. For example, if you have an
          // optional property "edited", but you misspell it as
          // "edtied", .strict() will warn you!
          .strict(),
      },
    ],
  });
};
```

### ESM (`@11ty/eleventy@v3` or later)

```js
import pluginValidate from 'eleventy-plugin-validate';
import { z } from 'zod';

export default (eleventyConfig) => {
  eleventyConfig.addPlugin(pluginValidate, {
    // Select the Zod library for schemas:
    validator: 'zod',
    schemas: [
      {
        // `collections: ['posts']` tells the plugin
        // to run this schema on the 'posts' collection.
        // If you omit this property, the schema will run against
        // collection items from the 'all' collection (a default
        // collection that Eleventy generates for you).
        collections: ['posts'],

        // `schema` should be a schema made with the validator
        // library selected in the above 'validator' property.
        schema: z
          .object({
            title: z.string(),
            description: z.string(),
            draft: z.boolean(),
          })
          // I suggest adding .strict() to your schema
          // for even more accurate validation.
          // With .strict(), extra properties
          // you have not specified in the schema object
          // will cause an error. For example, if you have an
          // optional property "edited", but you misspell it as
          // "edtied", .strict() will warn you!
          .strict(),
      },
    ],
  });
};
```

</details>

Run Eleventy, and voila! The plugin will warn you about collection items that do not pass schema validation.

For example:

```
> eleventy --serve

[eleventy-plugin-validate][./posts/hello-world.md] title: expected a string but received boolean
[eleventy-plugin-validate][./posts/hello-world.md] description: expected a string but received number
[eleventy-plugin-validate][./posts/hello-world.md] draft: expected a boolean but received string
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] Invalid frontmatter data provided (via Error)
...
```

## Caveats

This plugin uses the `addCollection` callback to access the entire data cascade of your site, which unfortunately means it adds an extra collection with no items in it. If you have [tag pages](https://www.11ty.dev/docs/quicktips/tag-pages/) (or anything similar), you'll need to use [pagination filtering](https://www.11ty.dev/docs/pagination/#filtering-values) to hide the `eleventy-plugin-validate` collection. I'm open to suggestions if you have another way of doing this!

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/eleventy-plugin-validate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
