Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nope-validator

ftonato8.9kMIT1.0.4TypeScript support: included

Fast and simple JS validator

nope, js, object, validation, validator, scheme

readme

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: 'me@gmail.com',
  confirmEmail: 'me@gmail.com',
}); // returns an error object { name: 'Please provide a longer name '};

UserSchema.validate({
  name: 'Jonathan Livingston',
  email: 'me@gmail.com',
  confirmEmail: 'me@gmail.com',
}); // 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>
  );
}

License

MIT

changelog

1.0.4

Fixes 🔨

  • String
    • .trim()

1.0.3

Added 🚀

  • String
    • .trim() (thank you @TolyaDeveloper, for the suggestion!)

Internal 🏡

  • A lot of dev dependencies upgraded

1.0.2

Added 🚀

  • Number
    • between (thank you @ftonato for the contribution!)

1.0.1

Added 🚀

  • String
    • between (thank you @ftonato for the contribution!)

Internal 🏡

  • A lot of dev dependencies upgraded

1.0.0 🎉

Improvements

  • Drastically improved performance for synchronous validation
  • Better API docs

Added 🚀

  • Async validation

Internal 🏡

  • A lot of dev dependencies upgraded
  • Removed default exports within code

0.12.2

Improvements 🔨

  • Speed up validation a bit

Internal 🏡

  • Changes to UMD build process

0.12.1

Fixes 🔨

  • URL and e-mail regex security issues

0.12.0

Added 🚀

  • Primitive
    • notAllowed
  • Ref
    • Access parent values

Fixes 🔨

  • Object
    • Deep schema validation

Internal 🏡

  • Bump @babel/types to 7.13.14
  • Bump @types/jest to 26.0.22
  • Bump @types/node to 14.14.37
  • Bump @typescript-eslint/eslint-plugin to 4.21.0
  • Bump @typescript-eslint/parser to 4.21.0
  • Bump eslint to 7.23.0
  • Bump eslint-config-prettier to 8.1.0
  • Bump eslint-plugin-prettier to 3.3.1
  • Bump husky to 6.0.0
  • Bump jest to 26.6.3
  • Bump lint-staged to 10.5.4
  • Bump prettier to 2.2.1
  • Bump typescript to 4.2.4
  • Replace ts-jest with @swc/jest
  • Remove ts-node
  • Remove nodemon

0.11.3

Fixes 🔨

  • Date
    • before (reference type argument)
    • after (reference type argument)

Internal 🏡

  • Bump ts-jest to 25.5.1
  • Bump @typescript-eslint/eslint-plugin to 2.32.0
  • Bump @typescript-eslint/parser to 2.32.0

0.11.2

Added 🚀

  • String
    • exactLength

Fixes

  • Primitive
    • oneOf (undefined throws error)

0.11.1

Added 🚀

  • Object
    • validate
      • abortEarly

0.11.0

Added 🚀

  • Object
    • validateAt (#232)

0.10.0

Added 🚀

  • Array (#50)
    • required
    • of
    • minLength
    • maxLength
    • mustContain
    • hasOnly
    • every
    • some
    • test

Special thanks to @AAmanzi for adding the array type

Fixes 🔨

  • .required property (#141)

Special thanks to @kuzmycz taking his time to open up an issue and a pull request to fix the .required

Internal 🏡

  • Bump @babel/types to 7.96
  • Bump @types/jest to 25.2.1
  • Bump @types/node to 13.13.4
  • Bump @typescript-eslint/eslint-plugin to 2.30.0
  • Bump @typescript-eslint/parser to 2.30.0
  • Bump eslint to 6.8.0
  • Bump eslint-plugin-prettier to 3.1.3
  • Bump husky to 4.2.5
  • Bump jest to 25.5.4
  • Bump nodemon to 2.0.3
  • Bump prettier to 2.0.5
  • Bump ts-jest to 25.4.0
  • Bump ts-node 8.10.1
  • Bump typescript to 3.8.3

0.9.0 Added 🚀

  • Number
    • atLeast
    • atMost
    • greaterThan
    • lessThan

Internal 🏡

  • Fixed husky precommit hook
  • Replaced TSLint with ESLint
  • Bump @types/node to 12.6.6
  • Bump lodash to 4.17.4 in /benchmark
  • Bump lint-staged to 9.2.0

0.8.1 Fixes 🔨

  • Email validation flag

0.8.0 Added 🚀

  • Object
    • noUnkown

Internal 🏡

  • Bumped typescript from 3.5.2 too 3.5.3
  • Bumped lint-staged from 9.0.2 to 9.1.0
  • Bumped @babel/types from 7.4.4 to 7.5.0
  • Bumped @types/node from 12.0.12 to 12.6.1
  • Added code of conduct
  • Added small PR template
  • Readme update

0.7.3 Support build for: ES2015 ESM CJS UMD AMD

0.7.2 Fixed when conditional not resolving properly in some cases

Dev: Bumps @types/node from 12.0.10 to 12.0.12

0.7.0 Added

  • Primitive
    • notOneOf
  • Number
    • integer

0.6.0 Added

  • Primitive
    • When

0.5.2 Complexity fixes Fixed email regex taking too long on certain entries Added

  • String
    • regex

0.5.1 Added CircleCI

0.5.0 Added

  • Object
    • extend

Tweaked lint rules and fixed some docs problems

0.4.0 Added

  • Primitives (string, number, date, boolean)
    • oneOf
    • required
    • validate
  • String
    • url
    • email
    • min
    • max
  • Number
    • min
    • max
    • positive
    • negative
  • Date
    • before
    • after
  • Boolean
    • true
    • false