• Stars
    star
    347
  • Rank 122,141 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small, simple, and fast JS validator. Like, wow that's fast. 🚀

Nope 🙅

CircleCI Fast Version size gzip

This project was created by the awesome Bruno Vego - @bvego, and is currently maintained by @ftonato and the community.


A small, simple and fast JS validator. Like, wow thats fast. 🚀

Nope's API is heavily inspired stolen from 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.

Typescript definitions included. ✨

Getting started

To start using Nope simply do

yarn add nope-validator

or

npm install -S nope-validator

or (even), do you wanna to try it online?

// import the dependency on your app

// const Nope = require('nope-validator'); // or
// const { Nope } = require('nope-validator'); // or
import Nope from 'nope-validator';
// create a schema

const 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

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.

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 (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register('username')} />
      {errors.username && <div>{errors.username.message}</div>}

      <input {...register('password')} />
      {errors.password && <div>{errors.password.message}</div>}

      <button type="submit">submit</button>
    </form>
  );
}

Usage with 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.

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 (
    <Formik
      initialValues={{ username: '', password: '' }}
      validate={(values) => schema.validate(values)}
      onSubmit={(values) => console.log('Submitted', values)}
    >
      {() => (
        <Form>
          <Field type="username" name="username" />
          <ErrorMessage name="username" component="div" />

          <Field type="password" name="password" />
          <ErrorMessage name="password" component="div" />

          <button type="submit">Submit</button>
        </Form>
      )}
    </Formik>
  );
}

Contribute

Information describing how to contribute can be found here 🎉

License

MIT

More Repositories

1

registrobr

Pesquise a disponibilidade de domínios desejados no Registro.br
JavaScript
25
star
2

populatedom.js

Populate form fields from a JSON object.
HTML
16
star
3

pomodoro-app

A minimal Pomodore technique application to help you control your focus.
JavaScript
14
star
4

svelte-debugger

A JSON <Debugger> component for Svelte apps
Svelte
13
star
5

DEV-SL-meetup

Página do evento DEV-SL - São Leopoldo
8
star
6

vscode-password-generator

Create a secure password using our generator tool. Help prevent a security threat by getting a strong password today.
TypeScript
7
star
7

set-timeout-manager

A simple utility for managing timeouts without having to manually track timeout IDs. Easily start and stop timeouts using a unique identifier.
TypeScript
6
star
8

vue-supabase-microblog

This is a repository created to provide an example of how to create a microblog using Vue + Supabase
Vue
5
star
9

mattermost-plugin-gender-inclusive-language

A Mattermost Plugin which actively listens (and prohibits) for language with racist, ableist, sexist, or other exclusionary histories
JavaScript
5
star
10

metic

Easy math decimals for JS
TypeScript
4
star
11

visibility-listener

Seamlessly track the visibility state of your document across different browsers. Know when your users switch to another window or tab.
TypeScript
4
star
12

bower-course

Um simples repositório utilizado durante o estudo do Bower.
3
star
13

plain-object

Convert nested JSON to simple plain JSON object
TypeScript
2
star
14

http2-server-push-example

HTTP/2 Server Push with Node.js
JavaScript
2
star
15

set-interval-manager

A simple utility for managing intervals without having to manually track interval IDs. Easily start and stop intervals using a unique identifier.
TypeScript
2
star
16

ftonato

1
star
17

donex

Workshop : Remix + Supabase + Tailwind
TypeScript
1
star
18

poc-i18n

1
star
19

vodafone-iot

Vodafone IoT Portal Dev Test
JavaScript
1
star
20

be-mean-modulo-mongodb

Exercicios do curso de be-mean, módulo de Mongodb
1
star
21

intercambio-page

Organizando o intercâmbio de forma simples e objetiva.
JavaScript
1
star
22

V13-SL

Um repositório simples p/ organização do evento (acampamento).
CSS
1
star
23

fuck-feelings

Um blog com intuito de armazenar experiências e desejos.
CSS
1
star
24

python-para-zumbis

Exercícios do curso Python para Zumbis
Python
1
star
25

introducao-react-course

Aprendendo sobre a estrutura básica do React
JavaScript
1
star
26

vue-i18n

Vue
1
star