# react-schema-validator

> A json schema validator made for react hooks.

Latest version **1.0.1** (published 2019-09-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-schema-validator
pnpm add react-schema-validator
yarn add react-schema-validator
bun add react-schema-validator
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-09-25 |
| First published | 2019-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.9 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Alan Leite |
| Maintainers | alanleite |

## Links

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

## Dependencies (1)

- [validator](https://npm.io/package/validator.md) ^10.11.0

## Recent versions

- 1.0.1 (latest) — 2019-09-25
- 1.0.0 — 2019-09-25
- 0.0.4 — 2019-04-08
- 0.0.3 — 2019-04-05
- 0.0.2 — 2019-04-04
- 0.0.1 — 2019-04-04
- 0.0.0 — 2019-04-04

## README

# react-schema-validator

A json schema validator made for react hooks.

WARNING: this library is in a very beginning and may have changes.

## Usage

```js
import { useStateWithValidator, schema } from 'react-schema-validator'

const Errors = (errors, key) => (
    if(!errors || errors[key]) { return [] }
    return (
        <div>
            {errors[key].map((error, i) => (
                <span key={i}>{error}</span>
            ))}
        </div>
    )
)

const MyForm = () => {
    const [state, setState] = useStateWithValidator({}, {
        name: schema.text().required().min(1).max(10),
        lastname: schema.text().min(1).max(20),
        age: schema.number().min(18).max(80)
    })

    const onChange = (e) => (
        setState({
            ...state,
            [e.name]: e.value
        })
    )

    return (
        <form>
            <input name='name' type='text' onChange={onChange} />
            <Errors errors={state._errors} key='name' />
            <input name='lastname' type='text' onChange={onChange} />
            <Errors errors={state._errors} key='lastname' />
            <input name='age' type='number' onChange={onChange} />
            <Errors errors={state._errors} key='age' />
        </form>
    )
}
```

## Roadmap

- [ ] Add more validation models
- [ ] Tree shaking compatibilities
- [ ] List of supported browsers
- [ ] Create tests and coverage

### Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

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