Skip to content

Commit

Permalink
Add JSON Schema guide to website
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Sep 13, 2024
1 parent 2dd1f16 commit d7ef5fb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
39 changes: 39 additions & 0 deletions website/src/routes/guides/(advanced)/json-schema/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: JSON Schema
description: >-
This guide will show you how to convert my schemas to JSON Schema format.
contributors:
- fabian-hiller
---

import { Link } from '@builder.io/qwik-city';

# JSON Schema

In favor of a larger feature set and smaller bundle size, I am not implemented with JSON Schema in mind. However, in some use cases, you may still need a JSON Schema. This guide will show you how to convert my schemas to JSON Schema format.

## Valibot to JSON Schema

A large part of my schemas are JSON Schema compatible and can be easily converted to the JSON Schema format using my official `toJsonSchema` function. This function is provided via a separat package called [`@valibot/to-json-schema`](https://github.com/fabian-hiller/valibot/tree/main/packages/to-json-schema).

> See the [README](https://github.com/fabian-hiller/valibot/blob/main/packages/to-json-schema/README.md) of the `@valibot/to-json-schema` package for more details.
```ts
import { toJsonSchema } from '@valibot/to-json-schema';
import * as v from 'valibot';

const ValibotEmailSchema = v.pipe(v.string(), v.email());
const JsonEmailSchema = toJsonSchema(ValibotEmailSchema); // { type: 'string', format: 'email' }
```

## Cons of JSON Schema

My schemas intentionally do not output JSON Schema natively. This is because JSON Schema is limited to JSON-compliant data structures. In addition, more advanced features like transformations are not supported. Since I want to leverage the full power of TypeScript, I output a custom format instead.

Another drawback of JSON Schema is that JSON Schema itself does not contain any validation logic. Therefore, an additional function is required that can validate the entire JSON Schema specification. This approach is usually not tree-shakable and results in a large bundle size.

In contrast, my API design and implementation is completely modular. Every schema is independent and contains its own validation logic. This allows my schemas to be plugged together like LEGO bricks, resulting in a much smaller bundle size due to tree shaking.

## Pros of JSON Schema

Despite these drawbacks, JSON Schema is still widely used in the industry because it also has many advantages. For example, JSON Schemas can be used across programming languages and tools. In addition, JSON Schemas are serializable and can be easily stored in a database or transmitted over a network.
3 changes: 2 additions & 1 deletion website/src/routes/guides/(get-started)/ecosystem/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ Use the button at the bottom left of this page to add your project to this ecosy

### Valibot to X

- [TypeSchema](https://typeschema.com/): Universal adapter for schema validation
- [@gcornut/valibot-json-schema](https://github.com/gcornut/valibot-json-schema): CLI & JS utility to convert Valibot to JSON schema
- [@valibot/to-json-schema](https://github.com/fabian-hiller/valibot/tree/main/packages/to-json-schema): The official JSON schema converter for Valibot
- [TypeSchema](https://typeschema.com/): Universal adapter for schema validation

### X to Valibot

Expand Down
1 change: 1 addition & 0 deletions website/src/routes/guides/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

- [Naming](/guides/naming-convention/)
- [Async](/guides/async-validation/)
- [JSON Schema](/guides/json-schema/)
- [i18n](/guides/internationalization/)

## Migration
Expand Down

0 comments on commit d7ef5fb

Please sign in to comment.