-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Form): add
superstruct
validation (#2363)
Co-authored-by: Benjamin Canac <[email protected]> Co-authored-by: Romain Hamel <[email protected]>
- Loading branch information
1 parent
7802aac
commit 5385944
Showing
10 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
docs/app/components/content/examples/form/FormExampleSuperstruct.vue
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 @@ | ||
<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> |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
|
@@ -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"> | ||
|