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.

validation fails if the id of the field is given as json path Ex: groups['Basic'].parameters[0].name.value

See original GitHub issue

Hi

Am using formik as my form library, where am giving the id of the input element is given a json path Ex: “groups[‘Basic’].parameters[0].name.value”

where form values will be generated as below:

  "values": {
    "groups": {
      "basic": {
        "parameters": [
          {
            "domain": {
              "value": "2"
            }
    } ] }

Same id is set to while creating the yup validation schema. But yup always trying get the value from form values assuming id is plain string key in form values object.

https://github.com/jquense/yup/blob/dd474ff79fb34aee5780dc0aebeac42eb6b88e48/src/object.js#L158

I think should support something like below.

if (field.validate) return field.validate(_.get(value, key), innerOptions);

Currently is there any other way or workaround to achieve the same for now?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
jquensecommented, Jul 30, 2018

you need to define schema’s at each level like:

object().shape({
  nested: object().shape({
    arr: array().of(object().shape({ num: number().max(4) })),
  }),
});
5reactions
WNemenchacommented, Apr 30, 2019

Just came into the issue too. This solution works fine except that we don’t use lodash internally. Here’s the plain JS way for future people coming into this issue (found it via Google so…)

Assuming we have such a payload where the key is dynamically created: image

The following schema would allow us to make use of i.e. Formik nested objects support while keepin Yup to validate our inputs.

const dynamicFormSchema = Yup.lazy(obj =>
    Yup.object(
      Object.entries(obj).reduce(
        (a, [key]) => ({
          ...a,
          [key]: Yup.object()
            .shape({
              expected_value: Yup.string().required('Required'),
              operator: Yup.string().required('Required'),
              return_block_name: Yup.string()
                .required('Required')
                .min(3, 'Minimum 3 characters')
                .max(20, 'Maximum 20 characters')
            })
            .required()
        }),
        {}
      )
    )
  )
Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve the "Parameter validation failed" error in AWS ...
1. Open the AWS CloudFormation console. · 2. In the navigation pane, choose Stacks. · 3. Form the Stack name column, choose the...
Read more >
Validate, Query, and Change JSON Data with Built-in Functions
The following query will return the documents where the id JSON field matches the value AndersenFamily , ordered by city and state JSON...
Read more >
Understanding JSON Schema
The names of the basic types in JavaScript and JSON can be ... That means that validation will fail if, for example, a...
Read more >
Working with Json fields (Concepts) - Prisma
Use the Json Prisma field type to read, write, and perform basic filtering on JSON types in the underlying database. In the following...
Read more >
12: 9.15. JSON Functions and Operators - PostgreSQL
text, json or jsonb, Get JSON object field by key ... jsonb, Are the left JSON path/value entries contained at the top level...
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