question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass component props as context for yup validation

See original GitHub issue

Feature

Yup validation can accept the context which can be used for validation purposes, but which is not a part of schema/values. Can we pass props as context?

Current Behavior

I see that validateYupSchema can accept context argument but it’s not used anywhere.

Desired Behavior

Be able to use component’s props and validation rules.

Suggested Solutions

Add component’s props as context for validateYupSchema()

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

14reactions
DaddyWarbuckscommented, Jan 16, 2019

Rather than passing the whole set of component props, it seems simple enough to add another prop validationContext that would ultimately be passed to the yup context options. I know this was closed automatically. But, would you be open to a PR that adds another prop validationContext?

7reactions
DaddyWarbuckscommented, Jun 1, 2019

My PR was closed for a fix to this issue for some valid reasons. Although Formik is somewhat “inspired” by Yup, the two aren’t married. There are no props that are “just for Yup’s sake”. You can pass a Yup schema, or any other schema with a .validate(values) method. Those other schemas however may not accept a second argument options.context. This means something like validationContext would be a prop “just for Yup’s sake”.

Similar to @klis87 solution above and another referenced here I think a good solution is to just use React composability to solve this

import { Formik, yupToFormErrors } from 'formik';

export default props => {
  return (
    <Formik
      validate={(values, props) => {
        return props.validationSchema
          .validate(values, { abortEarly: false, context: props.context })
          .catch(err => {
            throw yupToFormErrors(err);
          });
      }}
      {...props}
    />
  );
};

See comments on PR for some more info: https://github.com/jaredpalmer/formik/pull/1508

I think the real solve for this is some documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Yup context variables in Formik validation
The validation library Yup allows you to pass in values, which aren't validated themselves, to aid in validation using a context. This is...
Read more >
How to access context inside Yup schema - Stack Overflow
I'm trying to create dynamic Yup schema According to my component state. For example: import React, { useContext } from "react"; import { ......
Read more >
Formik - Advanced Yup Validation with Context - CodeSandbox
This is an example of a simple email validation form with React.js and Formik's HoC using a custom synchronous validator using yup and...
Read more >
Formik validations with contexts | DailyCoding
Everything, what we have to do is to pass field name as a props. It will check if form has any error on...
Read more >
Advanced Usage - React Hook Form
With the Form component injecting react-hook-form 's props into the child component, ... Use the custom hook, by passing the validation schema.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found