# react-validation-hook

> React hook for sync and async validation in forms

Latest version **1.0.2** (published 2020-02-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-validation-hook
pnpm add react-validation-hook
yarn add react-validation-hook
bun add react-validation-hook
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-02-03 |
| First published | 2020-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=8 |
| Dependencies | 0 |
| Unpacked size | 50.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | sumedh22 |
| Maintainers | sumedhchakravorty |

## Links

- npm: https://www.npmjs.com/package/react-validation-hook
- Repository: https://github.com/sumedh22/react-validation-hook
- Homepage: https://github.com/sumedh22/react-validation-hook#readme
- Issues: https://github.com/sumedh22/react-validation-hook/issues
- npm.io page: https://npm.io/package/react-validation-hook

## Recent versions

- 1.0.2 (latest) — 2020-02-03
- 1.0.1 — 2020-02-03
- 1.0.0 — 2020-02-03

## README

# react-validation-hook

> React hook for sync and async validation in forms

[![NPM](https://img.shields.io/npm/v/react-validation-hook.svg)](https://www.npmjs.com/package/react-validation-hook) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
# Website

Visit the [website](https://sumedh22.github.io/react-validation-hook/) for demo and docs
# Features
* Nice and Easy hook based interface
* Supports both Synchronous and Asynchronous validation
* Supports validators like range, required, length, regex out of the box
* Specify custom validators!
* Out of the box formatted error messages, which could be overwritten

## Install

```bash
npm install --save react-validation-hook
```

## Usage

```jsx
import React, { useEffect } from "react";

import {
  useValidator,
  requiredValidation,
  lengthValidation,
  asyncValidation
} from "react-validation-hook";

export default () => {
  const asyncFunction = async (value, context) => {
    const response = await fetch("https://jsonplaceholder.typicode.com/photos");
    const res = await response.json();
    if (value.length < 3) {
      throw new Error("Custom error " + value);
    }
  };
  const validators = [
    requiredValidation(),
    lengthValidation(2, 5),
    asyncValidation(asyncFunction)
  ];
  const { error, onChange, pending } = useValidator(validators);

  const changeHandler = e => {
    console.log(e.target.value);
  };

  return (
    <div>
      <ul>
        {error.map((err, idx) => (
          <li key={idx}>{err}</li>
        ))}
      </ul>
      <input onChange={onChange(changeHandler)} />
    </div>
  );
};

```

## License

MIT © [sumedh22](https://github.com/sumedh22)

---
_Source: https://npm.io/package/react-validation-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
