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

rx-store-form-plugin

v2.6.1

Published

a type safe form validation plugin based on Rx-Store-core

Downloads

179

Readme

RxStoreFormPlugin

A form Validation JavaScript library based on rx-store-core

Install

npm i rx-store-form-plugin

Validation for normal JavaScript object validation

import library:

import { NRFormBuilder } from "rx-store-form-plugin";

Define field types and meta types

fields is an array contains information to describe each field state

type define:

{
  field: string; // field name, should be unique
  value: any; // field value
  touched: boolean; // the field is touched or not
  changed: boolean; // the value in this field is changed or not
  focused: boolean; // this field is focused or not
  hovered: boolean; // this field is hovered by mouse or not
  type: DatumType; // field is SYNC(sync validated) or ASYNC(bulk async validated) or EXCLUDED(excluded async validated)
  lazy?: boolean; // if field type is EXCLUDED, whether wait for previous resolve
}[]

each element inside this array is a field data

metadata stands for the validation result for each field

type define

{
  errors: object; // required error message
  warn?: any; // optional warning message
  info?: any; // optional information
}

can be shortly written by:

FormControlMetadata<Error extends object, Warn = any, Info = any>
import { FormControlStrDatum, FormControlDatum, FormControlMetadata } from "rx-store-form-plugin"; // handy type definitions for define form types
// NRFormBuilder<Fields, Metadata>(...)
const sampleFormBuilder = new NRFormBuilder<
  [
    FormControlStrDatum<"uid">, // define a field with the name of 'uid' and value type is string
    FormControlStrDatum<"username">, // define a field with the name of 'username' and value type is string
    FormControlDatum<"subscribed", boolean> // define a field with the name of 'subscribed' and value type is boolean
  ],
  {
    uid: {
      errors: {
        invalidLength?: "length should be greater than 5" | null
      }
    }, // define metadata for 'uid'
    username: FormControlMetadata<{
      invalidSymbol?: "user's name should not contains symbol of '$'" | null
    }>,// define metadata for 'username'
  }
>(...)

Form ID and Sync validator

formSelector stands for id of a form validator is for Sync validate and return a metadata object validator will be called once the entire form data change

const sampleFormBuilder = new NRFormBuilder<...>(
  {
    formSelector: "sample" as const,
      validator: (form, meta) => {
        // validate uid field
      if (form[0].value.length < 5)) {
        if (meta.uid) {
          meta.uid.errors = {
            invalidLength: "length should be greater than 5",
          };
        }
      }
      // validate username field
      if (
        form[1].value.includes("$")
      ) {
        if (meta.username) {
          meta.username.errors = {
            invalidSymbol: "user's name should not contains symbol of '$'",
          };
        }
      }
      return meta;
    }
  }
)

Init typed form fields

const sampleFormBuilder = new NRFormBuilder<...>({...})
  .setFields([
    {
      field: "uid", // field name
      defaultValue: "", // default value for this field
        type?: field is SYNC(sync validated) or ASYNC(bulk async validated) or EXCLUDED(excluded async validated),
        $validator?: function description: (
          arg1 is field value, 
          arg2 is metadata for this field, 
          arg3 is entire form data
          ) => Promise or Observable resolve metadata, 
          // triggered by current field data changed, just for NRF
        $immutableValidator?: function description: (
          arg1 is field value, 
          arg2 is metadata for this field, 
          arg3 is entire immutable form data
          ) => Promise or Observable resolve immutable metadata, 
          // triggered by current field data changed, just for IRF
        lazy?: if field type is EXCLUDED, whether wait for previous pending get resolved,
        debounceDuration?: if field type is EXCLUDED, the debounce time
        datumKeys?: if field type is EXCLUDED, only the selected fields for comparing,
        comparator?: if the form type is NRF and field type is EXCLUDED, a comparator determine whether to call $validator or not, type def: (v1: any, v2: any) => boolean
      },
    {
      field: "username",
      defaultValue: "",
    },
    {
      field: "subscribed",
      defaultValue: false,
    },
  ])  
  // default metadata to start
  .setDefaultMeta({
    uid: { 
      errors: {
        invalidLength: "length should be greater than 5"
      } 
    },
    username: { errors: {} },
  });

** Code Example **

const {
  selector,
  initiator,
  observeMeta,
  observeMetaByField,
  observeFormData,
  observeFormDatum,
  observeFormValue,
  changeFormValue,
  getDatum,
} = sampleFormBuilder.getInstance();

const store = NRS({
  [selector()]: initiator,
  other: () => ({
    name: "",
    ph: "",
  }),
});

const stopValidation = sampleFormBuilder.getInstance().startValidation();

observeFormValue("uid", (result) => {
  console.log("uid", result);
});

observeMeta((meta) => {
  console.log("meta observed", meta);
});

setTimeout(() => {
  changeFormValue("uid", "xxx");
}, 1000);

setTimeout(() => {
  changeFormValue("uid", "111");
  changeFormValue("username", "jQuery$");
}, 5000);

setTimeout(() => {
  stopValidation?.();
}, 7000);

setTimeout(() => {
  changeFormValue("uid", "");
  changeFormValue("username", "");
}, 8000);