Skip to content

Commit

Permalink
Valibot requires 1.0.0-beta.3
Browse files Browse the repository at this point in the history
Fixes #503
Fixes #505
  • Loading branch information
ciscoheat committed Nov 15, 2024
1 parent ba7de2a commit 9012284
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Valibot updated to require `1.0.0-beta.3` (hoping for a stable release soon).

### Added

- Support for Vine 2.0.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"joi": "^17.13.1",
"superstruct": "^2.0.2",
"svelte": "3.x || 4.x || >=5.0.0-next.51",
"valibot": ">=0.33.0 <1",
"valibot": ">=1.0.0-beta.3",
"yup": "^1.4.0",
"zod": "^3.23.8"
},
Expand Down Expand Up @@ -172,7 +172,7 @@
"joi": "^17.13.3",
"json-schema-to-ts": "^3.1.1",
"superstruct": "^2.0.2",
"valibot": "^0.41.0",
"valibot": "^1.0.0-beta.3",
"yup": "^1.4.0",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.3"
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

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

20 changes: 20 additions & 0 deletions src/routes/(v2)/v2/valibot/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { superValidate, message } from '$lib/index.js';
import { valibot } from '$lib/adapters/valibot.js';
import { fail } from '@sveltejs/kit';
import { schema } from './schema.js';

export const load = async () => {
const form = await superValidate(valibot(schema));
return { form };
};

export const actions = {
default: async ({ request }) => {
const form = await superValidate(request, valibot(schema));
console.log(form);

if (!form.valid) return fail(400, { form });

return message(form, 'Form posted successfully!');
}
};
83 changes: 83 additions & 0 deletions src/routes/(v2)/v2/valibot/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<script lang="ts">
import { page } from '$app/stores';
import { superForm } from '$lib/index.js';
import SuperDebug from '$lib/index.js';
export let data;
const { form, errors, message, enhance } = superForm(data.form);
</script>

<SuperDebug data={$form} />

<h3>Nullable Valibot schema</h3>

{#if $message}
<!-- eslint-disable-next-line svelte/valid-compile -->
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}>
{$message}
</div>
{/if}

<form method="POST" use:enhance>
<label>
Name<br />
<input name="name" aria-invalid={$errors.name ? 'true' : undefined} bind:value={$form.name} />
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
</label>

<label>
Email<br />
<input
name="email"
type="email"
aria-invalid={$errors.email ? 'true' : undefined}
bind:value={$form.email}
/>
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
</label>

<button>Submit</button>
</form>

<hr />
<p><a target="_blank" href="https://superforms.rocks/api">API Reference</a></p>

<style>
.invalid {
color: red;
}
.status {
color: white;
padding: 4px;
padding-left: 8px;
border-radius: 2px;
font-weight: 500;
}
.status.success {
background-color: seagreen;
}
.status.error {
background-color: #ff2a02;
}
input {
background-color: #ddd;
}
a {
text-decoration: underline;
}
hr {
margin-top: 4rem;
}
form {
padding-top: 1rem;
padding-bottom: 1rem;
}
</style>
6 changes: 6 additions & 0 deletions src/routes/(v2)/v2/valibot/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { object, string, minLength, email, pipe } from 'valibot';

export const schema = object({
name: pipe(string(), minLength(2)),
email: pipe(string(), email())
});

0 comments on commit 9012284

Please sign in to comment.