Skip to content

Commit

Permalink
feat(Form): add superstruct validation (#2363)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <[email protected]>
Co-authored-by: Romain Hamel <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2024
1 parent 7802aac commit 5385944
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<script setup lang="ts">
import { object, string, nonempty, refine, type Infer } from 'superstruct'
import type { FormSubmitEvent } from '#ui/types'
const schema = object({
email: nonempty(string()),
password: refine(string(), 'Password', (value) => {
if (value.length >= 8) return true
return 'Must be at least 8 characters'
})
})
const state = reactive({
email: '',
password: ''
})
type Schema = Infer<typeof schema>
async function onSubmit(event: FormSubmitEvent<Schema>) {
console.log(event.data)
}
</script>

<template>
<UForm :schema="schema" :state="state" class="space-y-4" @submit="onSubmit">
<UFormField label="Email" name="email">
<UInput v-model="state.email" />
</UFormField>

<UFormField label="Password" name="password">
<UInput v-model="state.password" type="password" />
</UFormField>

<UButton type="submit">
Submit
</UButton>
</UForm>
</template>
12 changes: 10 additions & 2 deletions docs/content/3.components/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ links:

## Usage

Use the Form component to validate form data using schema libraries such as [Zod](https://github.com/colinhacks/zod), [Yup](https://github.com/jquense/yup), [Joi](https://github.com/hapijs/joi), [Valibot](https://github.com/fabian-hiller/valibot), or your own validation logic.
Use the Form component to validate form data using schema libraries such as [Zod](https://github.com/colinhacks/zod), [Yup](https://github.com/jquense/yup), [Joi](https://github.com/hapijs/joi), [Valibot](https://github.com/fabian-hiller/valibot), [Superstruct](https://github.com/ianstormtaylor/superstruct) or your own validation logic.

It works with the [FormField](/components/form-field) component to display error messages around form elements automatically.

### Schema Validation

It requires two props:
- `state` - a reactive object holding the form's state.
- `schema` - a schema object from a validation library like [Zod](https://github.com/colinhacks/zod), [Yup](https://github.com/jquense/yup), [Joi](https://github.com/hapijs/joi) or [Valibot](https://github.com/fabian-hiller/valibot).
- `schema` - a schema object from a validation library like [Zod](https://github.com/colinhacks/zod), [Yup](https://github.com/jquense/yup), [Joi](https://github.com/hapijs/joi), [Valibot](https://github.com/fabian-hiller/valibot) or [Superstruct](https://github.com/ianstormtaylor/superstruct).

::warning
**No validation library is included** by default, ensure you **install the one you need**.
Expand Down Expand Up @@ -54,6 +54,14 @@ It requires two props:
class: 'w-60'
---
::

::component-example{label="Superstruct"}
---
name: 'form-example-superstruct'
props:
class: 'w-60'
---
::
::

Errors are reported directly to the [FormField](/components/form-field) component based on the `name` prop. This means the validation rules defined for the `email` attribute in your schema will be applied to `<FormField name="email">`{lang="vue"}.
Expand Down
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"nuxt-og-image": "^3.0.6",
"prettier": "^3.3.3",
"shiki-transformer-color-highlight": "^0.2.0",
"superstruct": "^2.0.2",
"ufo": "^1.5.4",
"valibot": "^0.42.1",
"yup": "^1.4.0",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"joi": "^17.13.3",
"nuxt": "^3.13.2",
"release-it": "^17.10.0",
"superstruct": "^2.0.2",
"valibot": "^0.42.1",
"vitest": "^2.1.3",
"vitest-environment-nuxt": "^1.0.1",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/runtime/components/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface FormSlots {
import { provide, inject, nextTick, ref, onUnmounted, onMounted, computed, useId, readonly } from 'vue'
import { useEventBus } from '@vueuse/core'
import { formOptionsInjectionKey, formInputsInjectionKey, formBusInjectionKey, formLoadingInjectionKey } from '../composables/useFormField'
import { getYupErrors, isYupSchema, getValibotErrors, isValibotSchema, getZodErrors, isZodSchema, getJoiErrors, isJoiSchema, getStandardErrors, isStandardSchema } from '../utils/form'
import { getYupErrors, isYupSchema, getValibotErrors, isValibotSchema, getZodErrors, isZodSchema, getJoiErrors, isJoiSchema, getStandardErrors, isStandardSchema, getSuperStructErrors, isSuperStructSchema } from '../utils/form'
import { FormValidationException } from '../types/form'
const props = withDefaults(defineProps<FormProps<T>>(), {
Expand Down Expand Up @@ -113,6 +113,8 @@ async function getErrors(): Promise<FormErrorWithId[]> {
errs = errs.concat(await getJoiErrors(props.state, props.schema))
} else if (isValibotSchema(props.schema)) {
errs = errs.concat(await getValibotErrors(props.state, props.schema))
} else if (isSuperStructSchema(props.schema)) {
errs = errs.concat(await getSuperStructErrors(props.state, props.schema))
} else if (isStandardSchema(props.schema)) {
errs = errs.concat(await getStandardErrors(props.state, props.schema))
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/runtime/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Schema as JoiSchema } from 'joi'
import type { ObjectSchema as YupObjectSchema } from 'yup'
import type { GenericSchema as ValibotSchema, GenericSchemaAsync as ValibotSchemaAsync, SafeParser as ValibotSafeParser, SafeParserAsync as ValibotSafeParserAsync } from 'valibot'
import type { GetObjectField } from './utils'
import type { Struct as SuperstructSchema } from 'superstruct'

export interface Form<T> {
validate (opts?: { name: string | string[], silent?: false, nested?: boolean }): Promise<T | false>
Expand All @@ -19,9 +20,12 @@ export interface Form<T> {
export type FormSchema<T extends Record<string, any>> =
| ZodSchema
| YupObjectSchema<T>
| ValibotSchema | ValibotSchemaAsync
| ValibotSafeParser<any, any> | ValibotSafeParserAsync<any, any>
| ValibotSchema
| ValibotSchemaAsync
| ValibotSafeParser<any, any>
| ValibotSafeParserAsync<any, any>
| JoiSchema<T>
| SuperstructSchema<any, any>
| StandardSchema

export type FormInputEvents = 'input' | 'blur' | 'change'
Expand Down
22 changes: 22 additions & 0 deletions src/runtime/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ZodSchema } from 'zod'
import type { ValidationError as JoiError, Schema as JoiSchema } from 'joi'
import type { ObjectSchema as YupObjectSchema, ValidationError as YupError } from 'yup'
import type { GenericSchema as ValibotSchema, GenericSchemaAsync as ValibotSchemaAsync, SafeParser as ValibotSafeParser, SafeParserAsync as ValibotSafeParserAsync } from 'valibot'
import type { Struct } from 'superstruct'
import type { FormError } from '../types/form'

export function isYupSchema(schema: any): schema is YupObjectSchema<any> {
Expand All @@ -29,6 +30,15 @@ export async function getYupErrors(state: any, schema: YupObjectSchema<any>): Pr
}
}

export function isSuperStructSchema(schema: any): schema is Struct<any, any> {
return (
'schema' in schema
&& typeof schema.coercer === 'function'
&& typeof schema.validator === 'function'
&& typeof schema.refiner === 'function'
)
}

export function isZodSchema(schema: any): schema is ZodSchema {
return schema.parse !== undefined
}
Expand Down Expand Up @@ -98,3 +108,15 @@ export async function getStandardErrors(
message: issue.message
})) || []
}

export async function getSuperStructErrors(state: any, schema: Struct<any, any>): Promise<FormError[]> {
const [err] = schema.validate(state)
if (err) {
const errors = err.failures()
return errors.map(error => ({
message: error.message,
name: error.path.join('.')
}))
}
return []
}
10 changes: 10 additions & 0 deletions test/components/Form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { z } from 'zod'
import * as yup from 'yup'
import Joi from 'joi'
import * as valibot from 'valibot'
import { object, string, nonempty, refine } from 'superstruct'
import ComponentRender from '../component-render'
import type { FormProps, FormSlots } from '../../src/runtime/components/Form.vue'
import { renderForm } from '../utils/form'
Expand Down Expand Up @@ -65,6 +66,15 @@ describe('Form', () => {
}))
}
],
['superstruct', {
schema: object({
email: nonempty(string()),
password: refine(string(), 'Password', (value) => {
if (value.length >= 8) return true
return 'Must be at least 8 characters'
})
})
}],
['custom', {
async validate(state: any) {
const errs = []
Expand Down
62 changes: 62 additions & 0 deletions test/components/__snapshots__/Form.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,68 @@ exports[`Form > renders with default slot correctly 1`] = `"<form id="v-0-0" cla
exports[`Form > renders with state correctly 1`] = `"<form id="v-0-0" class=""></form>"`;
exports[`Form > superstruct validation works > with error 1`] = `
"<form id="42" class="">
<div class="text-sm">
<div class="">
<!--v-if-->
<!--v-if-->
</div>
<div class="">
<div class="relative inline-flex items-center"><input id="email" type="text" name="email" class="w-full rounded-[calc(var(--ui-radius)*1.5)] border-0 placeholder:text-[var(--ui-text-dimmed)] focus:outline-none disabled:cursor-not-allowed disabled:opacity-75 transition-colors px-2.5 py-1.5 text-sm gap-1.5 text-[var(--ui-text-highlighted)] bg-[var(--ui-bg)] ring ring-inset ring-[var(--ui-border-accented)] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--ui-primary)]" autocomplete="off" value="[email protected]">
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>
</div>
<div class="text-sm">
<div class="">
<!--v-if-->
<!--v-if-->
</div>
<div class="">
<div class="relative inline-flex items-center"><input id="password" type="text" name="password" autocomplete="off" class="w-full rounded-[calc(var(--ui-radius)*1.5)] border-0 placeholder:text-[var(--ui-text-dimmed)] focus:outline-none disabled:cursor-not-allowed disabled:opacity-75 transition-colors px-2.5 py-1.5 text-sm gap-1.5 text-[var(--ui-text-highlighted)] bg-[var(--ui-bg)] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--ui-error)] ring ring-inset ring-[var(--ui-error)]" value="short">
<!--v-if-->
<!--v-if-->
</div>
<p class="mt-2 text-[var(--ui-error)]">Must be at least 8 characters</p>
</div>
</div>
</form>"
`;
exports[`Form > superstruct validation works > without error 1`] = `
"<form id="42" class="">
<div class="text-sm">
<div class="">
<!--v-if-->
<!--v-if-->
</div>
<div class="">
<div class="relative inline-flex items-center"><input id="email" type="text" name="email" class="w-full rounded-[calc(var(--ui-radius)*1.5)] border-0 placeholder:text-[var(--ui-text-dimmed)] focus:outline-none disabled:cursor-not-allowed disabled:opacity-75 transition-colors px-2.5 py-1.5 text-sm gap-1.5 text-[var(--ui-text-highlighted)] bg-[var(--ui-bg)] ring ring-inset ring-[var(--ui-border-accented)] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--ui-primary)]" autocomplete="off" value="[email protected]">
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>
</div>
<div class="text-sm">
<div class="">
<!--v-if-->
<!--v-if-->
</div>
<div class="">
<div class="relative inline-flex items-center"><input id="password" type="text" name="password" autocomplete="off" class="w-full rounded-[calc(var(--ui-radius)*1.5)] border-0 placeholder:text-[var(--ui-text-dimmed)] focus:outline-none disabled:cursor-not-allowed disabled:opacity-75 transition-colors px-2.5 py-1.5 text-sm gap-1.5 text-[var(--ui-text-highlighted)] bg-[var(--ui-bg)] ring ring-inset ring-[var(--ui-border-accented)] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--ui-primary)]" value="validpassword">
<!--v-if-->
<!--v-if-->
</div>
<!--v-if-->
</div>
</div>
</form>"
`;
exports[`Form > valibot safeParser validation works > with error 1`] = `
"<form id="42" class="">
<div class="text-sm">
Expand Down

0 comments on commit 5385944

Please sign in to comment.