New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json-msg-react

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-msg-react

Make form validation easier

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

React json-msg logo

json-msg-react

NPM JavaScript Style Guide

This might be similar to Formik but in a hook form , and this is also use json-msg as a validator. Json-msg is a lightweight alternative for yup and joi or any other schema validator.

Prerequisite

  • Basic knowledge of json-msg for building schema

Check json-msg documentation here

Install

npm install json-msg-react

or

yarn add json-msg-react

Usage

import React from "react";
import jm, { useForm } from "json-msg-react";

const schema = {
  username: jm.str({ min: 5 }),
  password: jm.str({ min: 8, max: 40 }),
  pin: jm.num({ digit: 4 }),
};
const initialData = {
  username: "",
  password: "",
  pin: 0,
};
const Example = () => {
  const { handleChange, handleSubmit, errors } = useForm(initialData, schema);

  function submit(data) {
    console.log(data);
  }
  return (
    <>
      <form onSubmit={handleSubmit(submit)}>
        <input name="username" onChange={handleChange} />
        {errors.username && <p> {errors.username} </p>}
        <input name="password" type="password" onChange={handleChange} />
        {errors.username && <p> {errors.username} </p>}
        <input name="pin" type="number" onChange={handleChange} />
        {errors.password && <p> {errors.password} </p>}
      </form>
    </>
  );
};

API

useForm(initialData,schema, option)

  • initialData:? Object
  • schema:? Object
optiontypedefaultdescription
showAllErrorsbooleanfalseShow all the errors in array form
trimbooleanfalseTrim all the string in the data
validateOnChangebooleantrueValidate on change the data
validateOnMountbooleanfalseValidate the data on mount

Keywords

react

FAQs

Package last updated on 04 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts