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.

Accessing the "final" schema and extracting it's rules

See original GitHub issue

Let’s say I have the following schema (one of the examples in Yup’s documentation):

let schema = object({
  isSpecial: boolean(),
  isBig: boolean(),
  count: number().when(['isBig', 'isSpecial'], {
    is: true, // alternatively: (isBig, isSpecial) => isBig && isSpecial
    then: yup.number().min(5),
    otherwise: yup.number().min(0),
  }),
});

const result = schema.validateSync({
  isBig: true,
  isSpecial: true,
  count: 10,
});

The count min value depends on the value of isBig. That value is decided at runtime, when calling validate(). Is there a way I can somehow access the “final” schema and programatically check what Yup decided to enforce?

Something like:

const { result, finalSchema } = schema.validateSync({
  isBig: true,
  isSpecial: true,
  count: 10,
});

console.log(finalSchema.count.min) // 5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jakobsandbergcommented, Mar 13, 2021

@alexandernst as mentioned above, schema.resolve() won’t resolve nested schemas. We must instead resolve the nested schema, count in this case, via yup.reach().

Now that we’re resolving at the nested schema, we’ll need to provide a parent object to schema.resolve() in order for this nested schema to access the values of its siblings.

const resolved = yup.reach(schema, 'count').resolve({
  parent: {
    isBig: true,
    isSpecial: true,
    count: 10
  }
});
1reaction
jakobsandbergcommented, Mar 13, 2021

It would be nice if Yup could resolve nested schemas by default though.

Yes, that would be handy. Fortunately, Yup provides a good API for extending functionality like this.

For example, you can add a resolveDeep method to your schemas that traverses the schema and resolves each node in a similar manner to the workaround I posted above.

yup.addMethod(yup.mixed, 'resolveDeep', function(values) {
  // traverse schema and resolve each node 
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Extracting schemas from a rule application archive - IBM
Extracting schemas from a rule application archive. To access the schemas for an imported rule set, extract them from the decision service.
Read more >
Creating and running an Extract rule - Pega
Create and open an Extract rule to run a manual BIX extraction process. To limit the properties included in your extract, ...
Read more >
Finalizing the end schema JSON of your output file - Cloud - 8.0
About this task The last step of the wizard shows the end schema generated and allows you to customize the schema according to...
Read more >
Create a Field Extraction Rule | Sumo Logic Docs
Extracted Fields (applicable to Ingest Time rules) shows the field names the rule will parse. Any fields that do not exist in the...
Read more >
Learn the structure of an Access database - Microsoft Support
Tables to store your data. Queries to find and retrieve just the data that you want. Forms to view, add, and update data...
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