Generate Vue 3/Vuetify forms from JSON Schema using JSON Forms.
npm install @authvia/ajv-schema-formsA Vue 3 library for generating forms from JSON Schema using JSON Forms. This library provides a simple way to create dynamic forms with validation based on JSON Schema specifications.
Presuming you already have a Vue3 application configured usage is pretty straight forward.
1. Install the module: npm install --save @authvia/ajv-schema-forms
1. Register plugins
``javascript`
// Make sure vuetify is set to auto import components.
app.use(vuetify)
app.use(ajvSchemaFormPlugin)
If vuetify styles are not enabled in your vue 3 project your index.html will need to add these two CSS resources.
`html`
The main component for rendering forms from JSON Schema.
| Prop | Type | Required | Default | Description |
|------|------|----------|---------|-------------|
| schema | Schema | Yes | - | JSON Schema defining the form structure and validation rules |uischema
| | Object | No | undefined | JSON Forms UI Schema defining the form layout and presentation |data
| | Object | No | {} | Initial data to populate the form |variant
| | String | No | 'filled' | Vuetify input variant: 'filled', 'outlined', 'plain', 'underlined', 'solo', 'solo-inverted', 'solo-filled'. Note: This is now controlled by the variant property in uischema |orientation
| | String | No | 'vertical' | Form layout orientation: 'vertical' (stacked) or 'horizontal' (side-by-side). Note: This is now controlled by the type in uischema (VerticalLayout or HorizontalLayout) |disabled
| | Boolean | No | false | Disables all interaction with the form |loading
| | Boolean | No | false | Puts the form in a loading visual state |showSubmitButton
| | Boolean | No | true | Controls whether the submit button is shown |errors
| | Object | No | {} | Validation errors to display on the form |
- submit: Emitted when the form is submitted with valid data
- submit-button: Custom submit button slot
The component supports separate schema and uischema props for better separation of concerns:
- schema: Contains the data structure, validation rules, and data-specific properties
- uischema: Contains UI presentation rules and UI-specific properties
#### Schema (Data Structure)
`javascript`
const schema = {
type: 'object',
required: ['name', 'email'],
properties: {
name: {
$id: '#root/name',
title: 'Name',
type: 'string',
description: 'Enter your full name',
maxLength: 50
},
email: {
$id: '#root/email',
title: 'Email',
type: 'string',
format: 'email',
description: 'Enter your email address'
}
}
}
#### UISchema (UI Presentation)
`javascript`
const uischema = {
type: 'VerticalLayout',
elements: [
{
type: 'Control',
scope: '#/properties/name',
label: 'Name',
options: {
multi: true // Makes it a textarea
}
},
{
type: 'Control',
scope: '#/properties/email',
label: 'Email',
options: {
format: 'email'
}
}
]
}
`vue
:uischema="myUISchema"
@submit="handleSubmit"
variant="outlined"
/>
`
vue
:schema="schema"
:uischema="uischema"
@submit="handleSubmit"
/>
`$3
`vue
:schema="schema"
:uischema="{ type: 'HorizontalLayout', variant: 'outlined', elements: [...] }"
@submit="handleSubmit"
/>
:schema="schema"
:uischema="{ type: 'VerticalLayout', variant: 'solo', elements: [...] }"
@submit="handleSubmit"
/>
`$3
`vue
:schema="schema"
:uischema="uischema"
:data="initialData"
@submit="handleSubmit"
/>
`$3
`vue
:schema="schema"
:uischema="uischema"
@submit="handleSubmit"
/>
`JSON Schema Support
This component uses JSON Forms for rendering and validation, providing robust support for:
- String, number, integer, and boolean types
- Enum values with dropdown selection
- Money format with cents conversion
- Multiline text areas
- Pattern validation
- Min/max length and value constraints
- Required field validation
UI-Specific Properties
The following properties are handled in the uischema:
-
format: 'money' - Renders money input with cents conversion
- multi: true - Renders textarea instead of text input
- multiplier: number - Applies multiplier to money fields
- readonly: boolean - Makes field read-onlyMigration from AJV
This library has been migrated from manual AJV validation to JSON Forms for better maintainability and feature support. The API remains largely the same, but validation is now handled automatically by JSON Forms.
Schema/UISchema Separation
The latest version introduces clean separation between data structure (schema) and UI presentation (uischema), providing better maintainability and flexibility.
$3
Form orientation is now controlled by the
type property in the uischema:
- VerticalLayout: Form fields are stacked vertically (default)
- HorizontalLayout: Form fields are arranged side-by-side horizontally$3
Form styling variant is now controlled by the
variant property in the uischema:
- filled: Default filled style (default)
- outlined: Outlined border style
- plain: Minimal styling
- underlined: Bottom border only
- solo: Elevated appearance
- solo-inverted: Inset appearance
- solo-filled`: Elevated with backgroundThis approach follows JSON Forms conventions and provides more granular control over form styling.
The component now supports JSON Schema array types with full CRUD operations:
- Dynamic arrays: Add/remove items dynamically
- Complex items: Array items can be objects with multiple properties
- Nested validation: Each array item follows the schema validation rules
- Custom labels: Configurable add/remove button labels
- Detail layouts: Customizable layout for array item details
Arrays are rendered using JSON Forms' built-in array renderer, providing a user-friendly interface for managing collections of data.