Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Dependencies and AdonisJS build
node_modules
build
package-lock.json

# Secrets
.env
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
6 changes: 6 additions & 0 deletions content/docs/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
"contentPath": "./guides/helpers.md",
"category": "Guides"
},
{
"permalink": "json_schema",
"title": "JSON Schema",
"contentPath": "./guides/json_schema.md",
"category": "Guides"
},
{
"permalink": "types/string",
"title": "String",
Expand Down
75 changes: 75 additions & 0 deletions content/docs/guides/json_schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
summary: Convert Vine Schema to JSON Schema
---

# JSON Schema

[JSON Schema](https://json-schema.org/) is a standard for describing the structure of JSON with JSON. It's widely used in [OpenAPI](https://www.openapis.org/) definitions and defining [structured outputs](https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat) for AI.

With VineJS you can **convert Vine schemas into JSON Schemas** using the `toJSONSchema` method.

```ts
import vine from '@vinejs/vine'

const schema = vine.create(vine.object({
firstName: vine.string().optional(),
lastName: vine.string().optional(),
email: vine.string().email(),
}))

schema.toJSONSchema()
// => {
// type: 'object',
// properties: {
// firstName: { type: 'string' },
// lastName: { type: 'string' }
// email: { type: 'string', format: 'email' }
// },
// required: [ 'email' ],
// additionalProperties: false,
// }
```

:::warning

JSON Schema cannot represent all complex schemas that VineJS can create. We always try to find the closest equivalent, otherwise we represent it as an `Any` type.

:::

## Customize Schema

You can customize the computed JSON schema by using the `meta` modifier.

```ts
vine.string().email().meta({ title: 'Login email' })
// => {
// type: 'string',
// format: 'email',
// title: 'Login email'
// }
```

## Custom rules

You can pass a `toJSONSchema` option when creating [custom rules](../extend/custom_rules.md) to modify the computed JSON schema.

```ts
const lowercaseRule = vine.createRule(
(value, _, field) => {
if (!value.match(/[a-z]/)) {
field.report('The {{ field }} field can only contain lowercase letters', 'lowercase', field)
}
},
{
toJSONSchema(schema) {
schema.regex = '/[a-z]/'
}
}
)

vine.string().use(lowercaseRule())
// => {
// type: 'string',
// regex: '/[a-z]/',
// }
```
Loading