# react-form-reducer-validator

> The validator resolver for react-from-reducer, this package uses validatorjs, and wraps it to be used with react-form-reducer

Latest version **1.0.2** (published 2023-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-form-reducer-validator
pnpm add react-form-reducer-validator
yarn add react-form-reducer-validator
bun add react-form-reducer-validator
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-04-03 |
| First published | 2023-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 24 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Majid Kuhail |
| Maintainers | sweetscript |
| Keywords | react-from-reducer, validatorjs |

## Links

- npm: https://www.npmjs.com/package/react-form-reducer-validator
- Repository: https://github.com/sweetscript/react-form-reducer-validator
- Homepage: https://github.com/sweetscript/react-form-reducer-validator#readme
- Issues: https://github.com/sweetscript/react-form-reducer-validator/issues
- npm.io page: https://npm.io/package/react-form-reducer-validator

## Dependencies (1)

- [validatorjs](https://npm.io/package/validatorjs.md) ^3.22.1

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-04-03
- 1.0.1 — 2023-03-17
- 1.0.0 — 2023-02-24

## README

# Validator for React-Form-Reducer

An extension package for [`react-form-reducer`](https://github.com/sweetscript/react-form-reducer).

The resolver wraps and uses the [`validatorjs`](https://www.npmjs.com/package/validatorjs) library, the library allows for a neat & simple way of adding validation rules. and the resolver allows for partial validation which is useful for stepped forms. You can also build and use your own resolver library if you like!


## Install

```bash
npm install react-form-reducer-validator
```

## Usage

```tsx
import { Resolver } from 'react-form-reducer-validator';

type MyValidatedFormType = {
  name: string;
  email: string;
  password: string;
  password_confirmation: string;
  terms: boolean;
};

export function ValidatedForm() {
  const {
    fields,
    handleInputChange,
    validate,
    errors
  } = useForm<MyValidatedFormType>(
    {
      name: '',
      email: '',
      password: '',
      password_confirmation: '',
      terms: false
    },
    {
      validation: new Resolver(
      	{
	      // Validation rules
          name: 'required',
          email: 'required|email',
          password: 'required',
          password_confirmation: 'required|same:password',
          terms: 'accepted'
        },
        {
	      // Custom messages
          'accepted.terms': 'Please accept the :attribute',
          'same.password_confirmation': "Passwords don't match"
        },
        [
          // Custom Validation Rules
          {
            name: 'telephone',
            callback: (fieldValue, args, attribute, data) => {
              return fieldValue.match(/^\d{3}-\d{3}-\d{4}$/);
            },
            message: 'The :attribute phone number is not in the format XXX-XXX-XXXX.'
          }
        ]
      )
    }
  );


  const handleSubmit = async ()=>{
    const { passed } = await validate()

    if(!passed){
      // throw error
    }
  }
}

```

## Contribution

![GitHub issues](https://img.shields.io/github/issues/sweetscript/react-form-reducer-validator)

Feedback, Issue reports, suggestions and contributions are appreciated and welcome.

[majid@sweetscript.com](mailto:majid@sweetscript.com)

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