pickstack

Svelte + Yup

Schema validation with a chainable API (Yup 1.7+, a Standard Schema). This page shows exactly what pickstack adds to a Svelte project for Yup: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.

$ npm create pickstack@latest my-app -- --framework svelte --modules tanstack-form,yup --yes
Customize in the configurator

Packages it adds

PackageVersionType
@tanstack/svelte-form^1.33.5runtime
yup^1.7.1runtime

New files

src/lib/SignupForm.svelte
<script lang="ts">
  import { createForm } from "@tanstack/svelte-form";
  import { object, string } from "yup";

  const schema = object({
    email: string()
      .required("Enter a valid email address.")
      .email("Enter a valid email address."),
  });

  let joined = $state("");
  const form = createForm(() => ({
    defaultValues: { email: "" },
    validators: { onSubmitAsync: schema },
    onSubmit: ({ value }) => (joined = value.email),
  }));
</script>

{#if joined}
  <p class="text">Thanks, {joined} is on the list.</p>
{:else}
  <form
    novalidate
    class="form"
    onsubmit={(event) => {
      event.preventDefault();
      form.handleSubmit();
    }}
  >
    <form.Field name="email">
      {#snippet children(field)}
        <label class="field">
          Email
          <input
            type="email"
            class="input"
            name={field.name}
            value={field.state.value}
            onblur={field.handleBlur}
            oninput={(event) => field.handleChange(event.currentTarget.value)}
          />
          {#if field.state.meta.errors[0]}
            <span role="alert" class="error">
              {field.state.meta.errors[0].message}
            </span>
          {/if}
        </label>
      {/snippet}
    </form.Field>
    <button type="submit" class="btn">Sign up</button>
  </form>
{/if}

Changes to existing files

src/app.css (changed)
⋯ 87 unchanged lines   background: #334155; }  +.form {+  display: flex;+  width: 100%;+  max-width: 24rem;+  flex-direction: column;+  gap: 0.75rem;+}+ +.field {+  display: flex;+  flex-direction: column;+  gap: 0.25rem;+  font-size: 0.875rem;+  font-weight: 500;+}+ +.input {+  border: 1px solid #cbd5e1;+  border-radius: 0.5rem;+  padding: 0.5rem 0.75rem;+  font: inherit;+}+ +.error {+  color: #dc2626;+  font-weight: 400;+}+ 
src/App.svelte (changed)
 <script lang="ts">   import Counter from "./lib/Counter.svelte";+  import SignupForm from "./lib/SignupForm.svelte"; </script>   <main class="app">   <h1 class="title">my-app</h1>   <Counter />+  <SignupForm /> </main>  

Project files

Related templates