Installation
Requirements
| Environment | Minimum version |
|---|---|
| Node.js | 20.12+ |
| Vue | 3.3.0+ (required) |
zod | 3.22.0+ (optional — only for /zod) |
yup | 1.3.0+ (optional — only for /yup) |
valibot | 1.0.0+ (optional — only for /valibot) |
primevue | 4.0.0+ (optional — only for /ui/primevue) |
naive-ui | 2.38.0+ (optional — only for /ui/naive) |
@vue/devtools-api | 6+ / 7+ / 8+ (optional — only for /devtools) |
Unlike most packages in this catalog, Vue itself is a required peer dependency here, not optional — the core (useForm, validators, parsers) is Vue-specific, not framework-agnostic.
Installation
bash
npm install @macrulez/vue-form-schemaOptional peer dependencies:
bash
npm install zod # Zod adapter
npm install yup # Yup adapter
npm install valibot # Valibot adapterUsing Nuxt?
@macrulez/nuxt-vue-form-schema auto-imports useForm, useFieldArray, the built-in validators, schema adapters and more — no manual import needed:
bash
npm install @macrulez/nuxt-vue-form-schemats
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['@macrulez/nuxt-vue-form-schema'],
})See the Nuxt Module page for options.
Quick start
vue
<script setup lang="ts">
import { useForm } from '@macrulez/vue-form-schema'
import type { FieldDefinition } from '@macrulez/vue-form-schema'
const schema: FieldDefinition[] = [
{ type: 'text', name: 'name', label: 'Full name', required: true },
{ type: 'email', name: 'email', label: 'Email', required: true },
{
type: 'select',
name: 'role',
label: 'Role',
options: [
{ label: 'Admin', value: 'admin' },
{ label: 'User', value: 'user' },
],
},
]
const { values, errors, touched, isValid, isSubmitting, submit, setField } = useForm({
schema,
validateOn: 'blur',
onSubmit: async (data) => {
await fetch('/api/users', { method: 'POST', body: JSON.stringify(data) })
},
})
</script>
<template>
<form @submit.prevent="submit">
<div v-for="field in schema" :key="field.name">
<label>{{ field.label }}</label>
<input
:type="field.type"
:value="values[field.name]"
@input="setField(field.name, ($event.target as HTMLInputElement).value)"
@blur="touched[field.name] = true"
/>
<span v-if="touched[field.name] && errors[field.name]">
{{ errors[field.name][0] }}
</span>
</div>
<button type="submit" :disabled="!isValid || isSubmitting">Submit</button>
</form>
</template>Or use FormRenderer for zero-markup rendering:
vue
<script setup lang="ts">
import { useForm } from '@macrulez/vue-form-schema'
import { FormRenderer } from '@macrulez/vue-form-schema/ui'
const form = useForm({ schema, onSubmit })
</script>
<template>
<FormRenderer :form="form" submit-label="Save" />
</template>