Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended validation library #78

Closed
bogdansoare opened this issue Dec 21, 2017 · 14 comments
Closed

Recommended validation library #78

bogdansoare opened this issue Dec 21, 2017 · 14 comments

Comments

@bogdansoare
Copy link

Hey there 👋 . What do you recommend for the validation library?
Formik recommends yup, just curious what final-form does recommend.

Keep up the good work ❤️

@erikras
Copy link
Member

erikras commented Dec 21, 2017

🏁Final Form is unopinionated. Yup should work fine.

Here's a list of some that work with Redux-Form and should work fine with 🏁React Final Form.

@erikras erikras closed this as completed Dec 21, 2017
@bogdansoare
Copy link
Author

@erikras I'm struggling implementing validation with yup, could you please provide a simple example ? it would really help, thanks.

@OKNoah
Copy link

OKNoah commented Sep 6, 2018

@bogdansoare

This might help?

<Form
        onSubmit={this.submitHandler}
        validate={async (values) => {
          const validator = validationSchema(values)

          try {
            await validator.validate(values, { abortEarly: false })
          } catch (err) {
            const errors = err.inner.reduce((formError, innerError) => ({
              ...formError,
              [innerError.path]: innerError.message
            }), {})

            return errors
          }
        }}

@Noitidart
Copy link
Contributor

Thanks for asking for help with yup, I was also looking for same.

@lookfirst
Copy link
Contributor

lookfirst commented Aug 29, 2019

Took the above (edit and the below) and turned it into a simple function.

import {Schema} from 'yup';
import {set, get} from 'lodash';

export function makeValidate<T>(validator: Schema<T>) {
	return async (values: T) => {
		try {
			await validator.validate(values, {abortEarly: false})
		} catch (err) {
			return err.inner.reduce((errors: any, {path, message}: any) => {
				if (errors.hasOwnProperty(path)) {
					set(errors, path, get(errors, path) + ' ' + message);
				} else {
					set(errors, path, message);
				}
				return errors;
			}, {});
		}
	};
}
const schema = yup.object().shape<InvoiceInput>(
	{
		name: yup.string().required().label('Invoice name'),
	}
);
const validate = makeValidate<InvoiceInput>(schema);

Hopefully this saves someone 10 minutes. Note that InvoiceInput was auto generated from my graphql schema. It would be nice if I could pass a typescript type into yup and have it magically generate a schema from that, but not the end of the world to write it out twice, at least it is typesafe.

Also, if you're integrating with MUI, you can use your definitions to set the required attribute on your wrapped <Field/>/<TextField/> by using yup.describe(). Actually, that is a lie, right now it is ugly, but I'm hoping the API for that gets better. Right now I'm using schema.fields[name]._exclusive.required

@Noitidart
Copy link
Contributor

Noitidart commented Aug 29, 2019

@lookfirst that reduce doesn't work with nested or field arrays, I used to do that way to, it is setting innerError.path. We have to actually set the shape that the path says. I use lodash set:

import { get, set } from 'lodash';

function convertYupErrorsToFieldErrors(yupErrors) {
  return yupErrors.inner.reduce((errors, { path, message }) => {
    if (get(errors, path)) {
        set(errors, path, get(errors, path) + ' ' + message);
    } else {
        set(errors, path, message);
    }
    return errors;
  }, {});
}

@ziaulrehman40
Copy link

Thanks @Noitidart Working flawlessly!

@lookfirst
Copy link
Contributor

I added this without the lodash dependency to my Material UI + RFF library...

https://github.com/lookfirst/mui-rff#helpers
https://github.com/lookfirst/mui-rff/blob/master/src/Validation.ts

@Noitidart
Copy link
Contributor

Thanks @ziaulrehman40 ! There was a bug in that, the if (errors.hasOwnProperty(path)) I had to change to use lodash get as well.

@lookfirst we should be able to remove the lodash dependency as final-form exports some helpers:

@lookfirst
Copy link
Contributor

Thanks @Noitidart ... that is helpful... I can use those instead of my own versions and save some bytes....

@lookfirst
Copy link
Contributor

@Noitidart I tried them as a drop in replacement and my tests are failing. I'll have to dig into it deeper to see what is going on, but wanted to let you know that they don't seem to be a direct easy replacement.

@Noitidart
Copy link
Contributor

Noitidart commented Nov 26, 2019 via email

@belliAndrea
Copy link

@Noitidart any news with a final solution?

@nfantone
Copy link

nfantone commented Aug 27, 2020

For anyone out there interested in using yup with RFF and landing on this issue, here's my take on a hook that would allow just that.

More than happy to add this to the docs if there's any interest.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants