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

formstate-inator

v0.0.60

Published

Create complex forms in a straight forward, testable, and UI framework agnostic manner. This library integrates with [validator-inator](https://github.com/jgreene/validator-inator).

Downloads

17

Readme

Create complex forms in a straight forward, testable, and UI framework agnostic manner. This library integrates with validator-inator.

Examples

Play with the below example here: Edit k0442jj955

    import * as t from "io-ts";
    import * as tdc from "io-ts-derive-class";
    import {
        ValidationRegistry,
        required,
        min,
        max,
        isValid
    } from "validator-inator";
    import { deriveFormState, FormState } from "formstate-inator";

    //This is used so that you can pass dependencies to validation functions
    //e.g. if you want to call a web service you could reference it here
    type MyValidationContext = {};

    const registry = new ValidationRegistry<MyValidationContext>();

    const AddressType = t.type({
        ID: t.number,
        StreetAddress1: t.string,
        City: t.string,
        State: t.string
    });

    class Address extends tdc.DeriveClass(AddressType) {}

    registry.register(Address, {
        StreetAddress1: required()
    });

    const PersonType = t.type({
        ID: t.number,
        FirstName: t.string,
        LastName: t.string,
        Email: t.union([t.string, t.null]),
        Phone: t.union([t.string, t.null]),
        Addresses: t.array(tdc.ref(Address))
    });

    class Person extends tdc.DeriveClass(PersonType) {}

    const mustHaveContactMethodValidator = (p: Person) => {
        const validResult = { Email: null, Phone: null, Addresses: null };
        if (p.Email !== null || p.Phone !== null || p.Addresses.length > 0) {
            return validResult;
        }

        const message =
            "A person must have an Email or a Phone or a Physical Address!";

        return { Email: message, Phone: message, Addresses: message };
    };

    registry.register(Person, {
        FirstName: required(),
        LastName: [required(), min(1)],
        Email: mustHaveContactMethodValidator,
        Phone: mustHaveContactMethodValidator,
        Addresses: mustHaveContactMethodValidator
    });

    class PersonForm {
        state: FormState<Person>;
        constructor(public originalValue: Person, ctx: MyValidationContext) {
            this.state = deriveFormState(originalValue, registry, ctx);
        }
    }

    const app = document.getElementById("app");

    function log(message) {
        if (typeof message == "object") {
            app.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + "<br />";
        } else {
            app.innerHTML += message + "<br />";
        }
    }

    function logJSON(input: any) {
        log(JSON.stringify(input, null, 2));
    }

    function sleep(ms: number) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    app.innerHTML = `validating...`;

    (async function() {
        app.innerHTML = ``;
        const myValidationCtx = {};
        const testPerson1 = new Person();
        const form = new PersonForm(testPerson1, myValidationCtx);

        await form.state.validate();

        logJSON(form.state.value.LastName.errors);

        form.state.value.LastName.onChange("Doofenschmirtz");

        await sleep(1); //validation is always asynchronous so we need to wait for the results after a change
        logJSON(form.state.value.LastName.errors);

        //model is always up to date with the current form state
        //but is in the same shape as the original data passed into deriveFormState
        logJSON(form.state.model);

        form.state.value.Addresses.value.push(new Address());

        logJSON(form.state.model);
        await form.state.validate();

        const address1 = form.state.value.Addresses.value.getItem(0);
        logJSON(address1.value.StreetAddress1.errors);

        address1.value.StreetAddress1.onChange("Evil Inc.");
        logJSON(form.state.model);
        await sleep(1); //wait for validation errors to get updated
        logJSON(address1.value.StreetAddress1.errors);
    })();

Usage can be viewed in src/index.spec.ts

Contributing

yarn install
yarn run test