class-validator-formik-lkc
TypeScript icon, indicating that this package has built-in type declarations

1.0.31 • Public • Published

class-validator-formik

Actions Status npm version

This fork updates the class-validator version because the old version is not tree-shakable

An easy and small library that allows you to validate Formik forms with class-validator.

Example

First, define your validation schema:

import { IsEmail } from 'class-validator';

class Schema {
  @IsEmail()
  email: string;
}

Then, provide the schema wrapped in the createValidator function from the library to the validate prop in Formik. For example, with hooks it looks like this:

import { useFormik } from 'formik';
import { createValidator } from 'class-validator-formik';

...
const { ... } = useFormik({ ..., validate: createValidator(Schema) });

Or, without hooks:

import { createValidator } from 'class-validator-formik';

<Formik ... validate={createValidator(Schema)}>
  ...
</Formik>

Note: you might want to memoize this the createValidator function.

Motivation

I built this because I was looking to share validation schemas between my frontend and type-graphql backend. type-graphql works very well with class-validator, and because I like to manage my forms with Formik I decided I should make this, so I do not have to duplicate validation code (by default, Formik has very good support for yup)

Contributing

The library is pretty much done, but if you want to contribute please note that we use tsdx.

Readme

Keywords

none

Package Sidebar

Install

npm i class-validator-formik-lkc

Weekly Downloads

22

Version

1.0.31

License

MIT

Unpacked Size

15.2 kB

Total Files

23

Last publish

Collaborators

  • watchdict