@onlydann/validator-form
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Tool for creating text validation forms and use in both sides (frotend, backend)

Use this package in react with @onlydann/react-validator, and in react native with @onlydann/native-validator

Install

npm install @onlydann/validator-form

Or

yarn add @onlydann/validator-form

Usage

import { ValidatorForm, Validator } from "react-validator-form";

const form = new ValidatorForm({
  name: new Validator("", [Validator.minLength(4), Validator.maxLength(16)]),

  password: new Validator("", [
    Validator.password(8, { maxLength: 10, bothCases: true }),
    Validator.required,
  ]),
});

Change values and get errors

form.fields.password.change("1234asd");
// or
form.change("password", "1234asd");

// errors
if (!form.valid) {
  if (form.errors?.password?.required) {
    console.log("Password is required");
  }

  if (form.errors?.name?.minLength) {
    console.log(
      `Name's minimum length is ${form.errors.name.minLength.allowedLength}, current length is ${form.errors.name.minLength.currentLength}.`
    );
  }
}

Sending and getting forms

// Client Side
const json = form.toJSON();

anyFetch("api/loging", { body: json });

// Server Side
const newForm = ValidatorForm.parse(req.body);
if (newForm.valid) {
  return res.json({ msg: "Form is valid" });
} else {
  return res.json({ msg: "Invalid form" });
}

Package Sidebar

Install

npm i @onlydann/validator-form

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

28 kB

Total Files

15

Last publish

Collaborators

  • seadann