# astro-zod-to-json-schema

> Auto-generate JSON schemas from your Astro's Content Collections Zod schemas.

Latest version **0.0.2** (published 2023-10-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install astro-zod-to-json-schema
pnpm add astro-zod-to-json-schema
yarn add astro-zod-to-json-schema
bun add astro-zod-to-json-schema
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2023-10-24 |
| First published | 2023-10-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | julian.cataldo |
| Keywords | astro-component, withastro, zod, codegen, generator, linting, remark, remark-lint, validation, content-collections, json-schema, optimization |

## Links

- npm: https://www.npmjs.com/package/astro-zod-to-json-schema
- npm.io page: https://npm.io/package/astro-zod-to-json-schema

## Dependencies (1)

- [zod-to-json-schema](https://npm.io/package/zod-to-json-schema.md) ^3.21.4

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.0.2 (latest) — 2023-10-24
- 0.0.1 — 2023-10-24
- 0.0.0 — 2023-10-24

## README

# Astro Content Collections Zod to JSON Schemas

## Installation

```sh
npm install astro-zod-to-json-schema
```

Configure Content Collections:

```diff
import { defineCollection, z } from 'astro:content';
+ import { astroZodCollectionsToJsonSchemas } from 'astro-zod-to-json-schema';

const blog = defineCollection({
	// Type-check frontmatter using a schema
	schema: z.object({
		title: z.string(),
		description: z.string(),
		// Transform string to Date object
		pubDate: z.coerce.date(),
		updatedDate: z.coerce.date().optional(),
		heroImage: z.string().optional(),
	}),
});

export const collections = { blog };

+ await astroZodCollectionsToJsonSchemas(collections);
```

Everytime you edit `/src/content/config.ts` file while using `astro dev`, each collection schemas will be automatically emitted, alongside Astro's own typings generation process:

```diff
.
├── ...
├── src
│   ├── content
│   │   ├── blog
+   │   │   ├── _blog.schema.json
│   │   │   ├── first-post.md
│   │   │   ├── second-post.md
│   │   │   ├── third-post.md
│   │   │   └── ...
│   │   └── config.ts
└── ...

18 directories, 35 files

```

Note the **underscore**, so it's ignored by Content Collection loader (analogously to the `pages` folder).  
You can still **import them as regular JSON files** into your project though!  
For example if you need to use them for **OpenAPI**, **AJV**, or anytime you need a
**serialized version of your Zod schemas**, really.

_That's all folks!_

---

Now that you have those sweet serialized schemas, you can
leverage the immense JSON schemas eco-system (see below).

## Use cases

### Lint / validate your Markdown frontmatter

![Demo screenshot of frontmatter schema linter 1](https://res.cloudinary.com/dzfylx93l/image/upload/v1667090782/eslint-plugin-mdx-1.png)

Install remark + plugins:

```sh
npm install -D \
  remark remark-cli \
  remark-frontmatter \
  remark-lint-frontmatter-schema
```

Create the remark config:

```yaml
# ./demo/.remarkrc.yaml

plugins:
  - remark-frontmatter

  - - remark-lint-frontmatter-schema
    - schemas:
        src/content/blog/_blog.schema.json:
          - src/content/blog/*.{md,mdx}
```

> **Note**  
> You can also put it in `./demo/src/content/.remarkrc.yaml`.  
> `remark-lint-frontmatter-schema` will resolve relative paths accordingly from where it's placed (e.g `blog/_blog.schema.json`).

See the [remark-lint-frontmatter-schema](https://github.com/JulianCataldo/remark-lint-frontmatter-schema) full documentation.

### Auto-generating forms UIs

See the [JSON Schema Form Element](https://github.com/json-schema-form-element/jsfe) library.

### Uses with OpenAPI

See the [Astro OpenAPI](https://github.com/JulianCataldo/astro-openapi) library.

## Future maintenance concerns

_Note that those concerns only affect developer maintenance, not really the end user._

`SchemaContext` is subject to changes. This library is stubbing it.  
Typings are not implemented (lacking some comprehension / affordance of Astro's own internal / public APIs for now).  
Also some of the magic is happening in user-land (codegen, Vite virtual modules, etc.), making things a bit more hazardous.  
See notes in code comments.

---
_Source: https://npm.io/package/astro-zod-to-json-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
