npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zodactive-form/svelte

v0.0.3

Published

Svelte wrapper for @zodactive-form/core

Downloads

1

Readme

Zodactive Form aims to provide very simple form reactivity based on the Zod validation library. This package wraps @zodactive-form/core to work with Svelte's reactivity and provides simple example components.

npm size libera manifesto

Preface

This is not an official Zod library. This is a personal project which is mainly meant to be used by my other projects. However, you are free to use it if you find it useful.

In addition, this library is under development and not all functionality from zod is supported yet.

Description

This Svelte library is a wrapper on top of @zodactive-form/core and provides components to quickly get started with reactive forms validated with Zod.

It provides the following:

  • useForm(zodSchema): A wrapper around useZodactiveForm() which uses writable() for reactivity;
  • ZForm: A simple form component which emits on:submit only when submitting while the form is valid;
  • ZFormField: A simple wrapper around <input> which is connected to the useForm() and will reactively show errors;

Dependencies

@zodactive-form/vue has the following dependencies:

  • Zod: The validation library used by the zodactive-form family;
  • Svelte: The reactive ui framework which this package wraps around;
  • @zodactive-form/core: Contains the main form validation logic;

Installation

As a simple npm package, it can be installed using your favorite package manager:

npm install @zodactive-form/svelte

Usage

First, create a Zod schema, then pass it to useForm() to generate the reactive context. You can then use this reactive context however which you like:

<script lang="ts">
    import { z } from 'zod';
    import { useForm } from '@zodactive-form/svelte';
    
    const userSchema = z.object({
        username: z.string().min(3),
        password: z.string().min(3),
        confirmPassword: z.string().min(3),
    }).refine(({ password, confirmPassword }) => password === confirmPassword);
    
    const { 
      form, // A writable() containing the whole form, including errors
      formErrors, // A writable() containing the errors of the form that do not belong to a specific field
      valid, // A writable() which is either true or false
      validate, // A function which will check if the values in the form are valid
      assign, // A helper function to assign a value to a specific field of the form
      getFieldFromPath, // A helper function to get the field data from the form structure
      toJson, // A helper function to retrieve the data without reactivity or errors
      clearErrors, // A helper function which removes the error state (but `valid` would still be false)
      clear, // A helper function which resets all the values in the form to the initial values
    } = useForm(userSchema);
</script>

In addition to managing a reactive form structure using svelte, @zodactive-form/svelte provides some very basic form component which use that reactivity.

NOTE: The form attribute of ZForm is not the form ref, but the whole object returned by useForm(). The names are a little confusing and will most probably be changed in the future.

<script lang="ts">
  import { z } from 'zod';
  import { useForm, ZForm, ZFormField } from '@zodactive-form/svelte';

  const userSchema = z.object({
    username: z.string().min(3),
    password: z.string().min(3),
    confirmPassword: z.string().min(3),
  }).refine(({ password, confirmPassword }) => password === confirmPassword);
  
  const form = useForm(userSchema);
  
  const onSubmit = () => {
    const data = form.toJson();
    // Form is valid, ready to fetch() with data!
  }
</script>

<ZForm :form="form" on:submit="onSubmit">
  <z-form-field label="Username" path="username" />
  <z-form-field label="Password" path="password" />
  <z-form-field label="Confirm Password" path="confirmPassword" />
</ZForm>

The provided <ZForm> is mostly equivalent to <form><slot /></form>;

The provided <ZFormField> has the following default output:

<template>
  <label>
    <span>{{ label }}</span>
    <input {value} on:input={(ev) => assign(props.path, ev.target.value)} />
    {#each errors as error}
        <p>{{ error }}</p>
    {/each}
  </label>
</template>

Additional Details

  • form.form, the writable(), contains a structure with both values and errors. The nested values are NOT REACTIVE. To update the form programmatically it is necessary to set form.form.value directly. This is what assign() does, and it is the recommended way to update the form.
  • The form is validated on creation, but errors are ignored. This is to set the valid property based on the initial data provided to the form;
  • When calling validate(), all errors are removed. If errors need to be removed at a different time, please use clearErrors();
  • The submit event of ZForm internally calls event.preventDefault(). In addition, it will only be emitted when the form is valid. If there are errors, it will not be emitted.

License

This project is licensed under the MIT License - see the LICENSE file for details.