An easy and small library that allows you to validate Formik forms with class-validator.
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.
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)
The library is pretty much done, but if you want to contribute please note that we use tsdx.