-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dd1f16
commit d7ef5fb
Showing
3 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
website/src/routes/guides/(advanced)/json-schema/index.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters