# @coxy/react-validator

> 🚀 Simple validation form for React or NodeJS apps with zod. useValidator are included ;)

Latest version **5.0.2** (published 2026-01-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @coxy/react-validator
pnpm add @coxy/react-validator
yarn add @coxy/react-validator
bun add @coxy/react-validator
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.2 |
| Published | 2026-01-19 |
| First published | 2019-04-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 40.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | dsshard |
| Maintainers | dsshard |
| Keywords | react, react validator, react component, form validation, validation, validator |

## Links

- npm: https://www.npmjs.com/package/@coxy/react-validator
- Repository: https://github.com/dsshard/react-validator
- Homepage: https://github.com/dsshard/react-validator#readme
- Issues: https://github.com/dsshard/react-validator/issues
- npm.io page: https://npm.io/package/@coxy/react-validator

## Dependencies (1)

- [zod](https://npm.io/package/zod.md) ^3.25.0 || ^4.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 5.0.2 (latest) — 2026-01-19
- 5.0.1 — 2025-10-09
- 5.0.0 — 2025-10-09
- 4.0.0 — 2025-08-25
- 3.0.0 — 2025-07-29
- 2.0.7 — 2023-05-12
- 2.0.6 — 2023-05-12
- 2.0.5 — 2023-05-12
- 2.0.4 — 2022-10-08
- 2.0.3 — 2022-10-08
- 2.0.2 — 2022-07-12
- 2.0.1 — 2022-07-12
- 2.0.0 — 2022-07-12
- 1.3.4 — 2021-07-05
- 1.3.3 — 2020-12-23
- … 25 more at https://npm.io/package/@coxy/react-validator/versions

## README

# @Coxy/react-validator

---

# Simple React Form Validation

@Coxy/react-validator provides a simple and flexible way to validate forms in React.

> Supports data validation both **in-browser** and **on Node.js**.

> Requires React **\>=16.3**.

---

# Installation

```bash
npm install @coxy/react-validator
# or
yarn add @coxy/react-validator
```

---

# Quick Start Example

```tsx
import React, { useState } from 'react';
import { ValidatorWrapper, rules, ValidatorField } from '@coxy/react-validator';

const validator = React.createRef();

const handleSubmit = () => {
  const { isValid, message, errors } = validator.current.validate();
  if (!isValid) {
    console.log(isValid, message, errors);
    return;
  }
  alert('Form is valid!');
};

export default function Example() {
  const [email, setEmail] = useState('');

  return (
    <ValidatorWrapper ref={validator}>
      <ValidatorField value={email} rules={rules.email}>
        {({ isValid, message }) => (
          <>
            <input value={email} onChange={({ target: { value } }) => setEmail(value)} />
            {!isValid && <div style={{ color: 'red' }}>{message}</div>}
          </>
        )}
      </ValidatorField>

      <button onClick={handleSubmit} type="button">
        Submit
      </button>
    </ValidatorWrapper>
  );
}
```

More examples available [here](example/example.tsx).

---

# Built-in Validation Rules

Create your own rules easily, or use the built-in ones:

```javascript
const rules = {
  notEmpty: [z.string().min(1, { error: 'Field is required' })],
  isTrue: [z.boolean({ error: 'Value is required' }).and(z.literal(true))],
  email: [z.string().min(1, { error: 'Email is required' }), z.email({ message: 'Email is invalid' })],
}
```

| Name     | Type  | Description                                |
|----------|-------|--------------------------------------------|
| email    | Array | Validate non-empty email with regex check. |
| notEmpty | Array | Check if a string is not empty.            |
| isTrue   | Array | Ensure a boolean value is present.         |

---

# React API

## `<ValidatorWrapper />` Props

| Name             | Default | Required | Description                                        |
|------------------|---------|----------|----------------------------------------------------|
| ref              | null    | No       | React ref or useRef for control.                   |
| stopAtFirstError | false   | No       | Stop validating after the first error.             |

## `<ValidatorField />` Props

| Name     | Default   | Required | Description                           |
|----------|-----------|----------|---------------------------------------|
| value    | undefined | Yes      | Value to validate.                    |
| rules    | []        | Yes      | Validation rules array.               |
| required | true      | No       | Whether the field is required.        |
| id       | null      | No       | ID for the field (for manual access). |

## `useValidator`

Validate a value directly in your component:

```tsx
import { useValidator, rules } from '@coxy/react-validator';

const [isValid, { errors }] = useValidator('test@example.com', rules.email);

console.log(isValid); // true or false
console.log(errors);  // array of error messages
```

---

# Validator Class (Node.js / Manual Usage)

Use it server-side or in custom flows.

## Validator Constructor Options

| Name             | Default | Required | Description                                          |
|------------------|---------|----------|------------------------------------------------------|
| stopAtFirstError | false   | No       | Stop validating after first error across all fields. |

## Methods

### `.addField()`

```javascript
const validator = new Validator({ stopAtFirstError: true });

const field = validator.addField({
  rules: rules.email,
  value: '12345',
});
```

### `.getField(id)`

```javascript
const field = validator.getField('password-field');
console.log(field);
```

### `.removeField(field)`

```javascript
validator.removeField(field);
```

### `.validate()`

```javascript
const { isValid, message, errors } = validator.validate();
console.log(isValid, errors);
```

## Full Server-Side Example

```javascript
import { Validator, rules } from '@coxy/react-validator';

const validator = new Validator();

validator.addField({
  id: 'email',
  rules: rules.email,
  value: 'test@domain.com',
});

validator.addField({
  id: 'password',
  rules: rules.isTrue,
  value: true,
});

const result = validator.validate();

if (result.isValid) {
  console.log('Validation passed!');
} else {
  console.error('Validation failed:', result.errors);
}
```

---

# License

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

---

# Notes

- Lightweight, flexible, easy to integrate.
- Server-side and client-side validation supported.
- Create custom rules for different scenarios.
- Easy to add dynamic validation logic.

> **Fun Fact:** Early validation of user inputs in a form can reduce backend server load by up to **40%** compared to server-only validation!

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