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

svelte-formify

v1.1.16

Published

A library to manage and validate the forms. This library uses decorators to define validations.

Downloads

19

Readme

svelte-formify

A library to manage and validate the forms. This library uses decorators to define validations.

This project is still under development, be careful if you decide to use on a production environment

Installation

npm i svelte-formify

Components

This library provides 3 main core components:

  • @Field to define properties and validations
  • SvelteForm to manage form state, get errors and the validated input values
  • <FormField> a simple input field to use and prevents boilerplate codes while create input fields

@Field

Field decorator expects the declarations from yup library, you can nest any yup validation function while creating a Field.

Important: We must pass the Type explicitly for nested types. ESBuild does not support emitDecoratorMetadata yet.

Usage example:

import { number, object, string } from 'yup'
import { Field }                  from 'svelte-formify'

export class Role {
    @Field(string().required().oneOf(['admin', 'test'])) name: string
}

export class User {
    @Field(string().required()) username: string
    @Field(string().required()) password: string
    @Field(string().required()) firstName: string
    @Field(string().required()) lastName: string
    @Field(object(), Role) role: Role // due to a restriction on ESBuild we can not emit decorator metadata for now, therefore we must pass the type for nested values explicitly
}

SvelteForm

SvelteForm class encapsulates the validations, listens for input changes and updates the errors so you can show validates states interactively to the user.

SvelteForm expect 2 parameters while constructing it.

  1. target: class defines the validation properties
  2. initialValues: initial values for this class

SvelteForm stores all data in a Context class. A context contains the properties described as below:

  • error : ValidationError thrown by yup validation
  • touched true on blur otherwise false
  • dirty if user is typing
  • value the value typed by user

How get raw values

You can call getRawValues function if you need the raw values (e.g. : sending the form)

const values = form.getRawValues()

Listening validation result

You can use isValid property which is a Writable to get validation status each time after user changes something.

Example: you want to enable/disable a button depends on validation status:

<script lang="ts">
    import { SvelteForm } from 'svelte-formify'
    import { User }       from './models/user' // your model classes

    const {isValid,...form} = new SvelteForm<User>(User, {
        firstName: '',
        lastName: '',
        password: '',
        role: {
            name: 'test'
        },
        username: 'hasan'
    })
</script>

<button disabled={!$isValid}>Login</button>

General usage example

<script lang="ts">
    import { SvelteForm } from 'svelte-formify'
    import { User }       from './models/user' // your model classes

    const {values,...form} = new SvelteForm<User>(User, {
        firstName: '',
        lastName: '',
        password: '',
        role: {
            name: 'test'
        },
        username: 'hasan'
    })
    
    // calling validate manually
    form.validate().then(rawValue => {}).catch(validationError => {})

    // e.g. usage of listener to get username while user types 
    $: console.log($values.username.value) // returns the typed value
    $: console.log($values.username.error) // returns the error
    $: console.log($values.username.touched) // true after onBlur
    $: console.log($values.username.dirty) // true while user is typing
</script>

{#if $values.username.error }
    <small>show some error</small>
{/if}
<FormField form={form} property={$values.username} classes="w-full p-2" placeholder="Username *" />
<!-- e.g. nested object usage -->
<FormField form={form} property={$values.role.name} classes="w-full p-2" placeholder="Role *" />