Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ftonato/nope-validator
A small, simple, and fast JS validator. Like, wow that's fast. 🚀
https://github.com/ftonato/nope-validator
form form-validation forms javascript object schema typescript validation validator
Last synced: 1 day ago
JSON representation
A small, simple, and fast JS validator. Like, wow that's fast. 🚀
- Host: GitHub
- URL: https://github.com/ftonato/nope-validator
- Owner: ftonato
- License: mit
- Created: 2019-06-23T22:27:14.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-06T03:37:55.000Z (5 months ago)
- Last Synced: 2024-09-20T05:51:43.219Z (about 2 months ago)
- Topics: form, form-validation, forms, javascript, object, schema, typescript, validation, validator
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/nope-validator
- Size: 2.83 MB
- Stars: 347
- Watchers: 9
- Forks: 15
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Nope 🙅
[![CircleCI](https://circleci.com/gh/ftonato/nope-validator.svg?style=svg)](https://circleci.com/gh/ftonato/nope-validator)
[![Fast](https://badgen.now.sh/badge/speed/really%20fast/green)](https://npm.im/nope-validator)
[![Version](https://img.shields.io/npm/v/nope-validator.svg)](https://npm.im/nope-validator)
[![size](https://img.shields.io/bundlephobia/min/nope-validator.svg)](https://bundlephobia.com/result?p=nope-validator)
[![gzip](https://img.shields.io/bundlephobia/minzip/nope-validator.svg)](https://bundlephobia.com/result?p=nope-validator)> This project was created by the awesome **[Bruno Vego - @bvego](https://github.com/bvego)**, and is currently maintained by [@ftonato](https://github.com/ftonato) and the community.
---
A small, simple and fast JS validator. Like, wow thats fast. 🚀
Nope's API is ~~heavily inspired~~ stolen from [Yup](https://github.com/jquense/yup) but Nope attempts to be much smaller and much faster. To achieve this Nope only allows for synchronous data validation which should cover most of the use cases.
### Note: Nope is not a plug-and-play replacement for Yup, in some cases at least.
Instead of throwing errors Nope simply returns the error object and if there are no errors it returns undefined.
For more details on what's available in Nope, check out the [documentation](https://github.com/ftonato/nope-validator/wiki).
Typescript definitions included. ✨
- [Getting started](#getting-started)
- [Usage with react-hook-form](#usage-with-react-hook-form)
- [Usage with Formik](#usage-with-formik)## Getting started
To start using Nope simply do
```sh
yarn add nope-validator
```or
```sh
npm install -S nope-validator
```or (even), do you wanna to **[try it online](https://replit.com/@ftonato/nope-validator-with-nodeJS)**?
```js
// import the dependency on your app// const Nope = require('nope-validator'); // or
// const { Nope } = require('nope-validator'); // or
import Nope from 'nope-validator';
``````js
// create a schemaconst UserSchema = Nope.object().shape({
name: Nope.string().atLeast(5, 'Please provide a longer name').atMost(255, 'Name is too long!'),
email: Nope.string().email().required(),
confirmEmail: Nope.string()
.oneOf([Nope.ref('email')])
.required(),
});UserSchema.validate({
name: 'John',
email: '[email protected]',
confirmEmail: '[email protected]',
}); // returns an error object { name: 'Please provide a longer name '};UserSchema.validate({
name: 'Jonathan Livingston',
email: '[email protected]',
confirmEmail: '[email protected]',
}); // returns undefined since there are no errors
```## Usage with [react-hook-form](https://github.com/react-hook-form/react-hook-form)
Huge thanks to the RHF team for making a resolver for nope, enabling you to use nope as a validator in your RHF-controlled forms.
```jsx
import { nopeResolver } from '@hookform/resolvers/nope';
import { useForm } from 'react-hook-form';
import * as Nope from 'nope-validator';const schema = Nope.object().shape({
username: Nope.string().required(),
password: Nope.string().required(),
});function Component({ onSubmit }) {
const {
register,
formState: { errors },
handleSubmit,
} = useForm({
resolver: nopeResolver(schema),
});return (
{errors.username &&{errors.username.message}}
{errors.password &&{errors.password.message}}submit
);
}
```## Usage with [Formik](https://github.com/jaredpalmer/formik)
Instead of passing it through the `validationSchema` prop, you should call Nope's validate on the `validate` prop as shown in the example below.
```jsx
import { Formik } from 'formik';
import * as Nope from 'nope-validator';const schema = Nope.object().shape({
username: Nope.string().required(),
password: Nope.string().required(),
});function Component({ onSubmit }) {
return (
schema.validate(values)}
onSubmit={(values) => console.log('Submitted', values)}
>
{() => (
Submit
)}
);
}
```## Contribute
Information describing how to contribute can be found **[here](https://github.com/ftonato/nope-validator/blob/master/CONTRIBUTING.md)** 🎉
## License
[MIT](LICENSE)