2.6.1 • Published 6 months ago

rx-store-form-plugin v2.6.1

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

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);
2.5.9

6 months ago

2.6.1

6 months ago

2.6.0

6 months ago

1.6.4

11 months ago

1.2.8

11 months ago

1.6.3

11 months ago

1.2.7

11 months ago

1.6.2

11 months ago

1.2.6

11 months ago

1.6.1

11 months ago

1.2.5

12 months ago

1.6.0

11 months ago

1.2.4

12 months ago

1.2.3

12 months ago

2.0.3

11 months ago

2.0.2

11 months ago

2.4.1

11 months ago

2.0.5

11 months ago

2.4.0

11 months ago

2.0.4

11 months ago

2.4.3

11 months ago

2.0.7

11 months ago

2.4.2

11 months ago

2.0.6

11 months ago

2.4.5

11 months ago

2.0.9

11 months ago

2.4.4

11 months ago

2.0.8

11 months ago

2.0.0

11 months ago

2.5.6

10 months ago

2.5.5

10 months ago

2.1.9

11 months ago

2.5.8

8 months ago

2.5.7

10 months ago

1.7.9

11 months ago

1.7.8

11 months ago

1.7.7

11 months ago

1.7.6

11 months ago

1.7.5

11 months ago

1.3.9

11 months ago

1.7.4

11 months ago

1.3.8

11 months ago

1.9.1

11 months ago

1.5.5

11 months ago

1.9.0

11 months ago

1.5.4

11 months ago

1.5.3

11 months ago

1.5.2

11 months ago

1.5.1

11 months ago

1.5.0

11 months ago

2.3.0

11 months ago

2.3.2

11 months ago

2.3.1

11 months ago

2.3.4

11 months ago

2.3.3

11 months ago

2.3.6

11 months ago

2.3.5

11 months ago

2.4.7

11 months ago

2.4.6

11 months ago

2.4.9

11 months ago

2.4.8

11 months ago

1.6.9

11 months ago

1.6.8

11 months ago

1.6.7

11 months ago

1.6.6

11 months ago

1.6.5

11 months ago

1.2.9

11 months ago

1.8.2

11 months ago

1.4.6

11 months ago

1.8.1

11 months ago

1.4.5

11 months ago

1.8.0

11 months ago

1.4.3

11 months ago

1.4.2

11 months ago

1.4.1

11 months ago

1.4.0

11 months ago

2.2.1

11 months ago

2.2.0

11 months ago

2.2.3

11 months ago

2.2.2

11 months ago

2.2.5

11 months ago

2.2.4

11 months ago

2.2.7

11 months ago

2.2.6

11 months ago

2.3.8

11 months ago

2.3.7

11 months ago

2.3.9

11 months ago

1.9.9

11 months ago

1.9.8

11 months ago

1.9.7

11 months ago

1.9.6

11 months ago

1.9.5

11 months ago

1.5.9

11 months ago

1.9.4

11 months ago

1.5.8

11 months ago

1.9.3

11 months ago

1.5.7

11 months ago

1.9.2

11 months ago

1.5.6

11 months ago

1.7.3

11 months ago

1.3.7

11 months ago

1.7.2

11 months ago

1.3.6

11 months ago

1.7.1

11 months ago

1.3.5

11 months ago

1.7.0

11 months ago

1.3.4

11 months ago

1.3.3

11 months ago

1.3.2

11 months ago

1.3.1

11 months ago

1.3.0

11 months ago

2.1.2

11 months ago

2.1.1

11 months ago

2.5.0

11 months ago

2.1.4

11 months ago

2.1.3

11 months ago

2.5.2

10 months ago

2.1.6

11 months ago

2.1.5

11 months ago

2.5.4

10 months ago

2.1.8

11 months ago

2.5.3

10 months ago

2.1.7

11 months ago

2.1.0

11 months ago

2.2.9

11 months ago

2.2.8

11 months ago

1.8.9

11 months ago

1.8.8

11 months ago

1.8.7

11 months ago

1.8.6

11 months ago

1.8.5

11 months ago

1.4.9

11 months ago

1.8.4

11 months ago

1.4.8

11 months ago

1.8.3

11 months ago

1.4.7

11 months ago

1.2.2

12 months ago

1.2.1

12 months ago

1.2.0

12 months ago

1.1.9

12 months ago

1.1.7

12 months ago

1.1.6

12 months ago

1.1.5

12 months ago

1.1.4

12 months ago

1.1.3

12 months ago

1.1.2

12 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.9

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

1 year ago

1.0.0

1 year ago