faloi / class-validator-formik

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

class-validator-formik

Actions Status npm version

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.

About

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

License:MIT License


Languages

Language:TypeScript 100.0%