1.0.31 • Published 1 year ago

class-validator-formik-lkc v1.0.31

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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.