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.

Example of dynamic object keys

See original GitHub issue

Could you please provide an example of how to validate an object with dynamic keys. I know I should use lazy but I don’t understand how. Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

36reactions
FunkiRcommented, Jan 25, 2020

without lodash

import {lazy, string, object} from 'yup';

const mapRules = (map, rule) => Object.keys(map).reduce((newMap, key) => ({...newMap, [key]: rule}), {});

const schema = lazy(map => object(
	mapRules(map, object({
		name: string().required()
	}))
));
35reactions
bogdansoarecommented, Oct 25, 2017

My solution is something like this:

import mapValues from 'lodash/mapValues';

Yup.lazy(obj =>
  Yup.object(
    mapValues(obj, (value, key) => {
      if (key === 'owner') {
        return Yup.string().required();
      }
      if (key === 'age') {
        return Yup. number();
      }
      if (key.includes('propertyName')) {
        return Yup.string().required();
      }
      if (key.includes('propertyValue')) {
        return Yup.number().required();
      }
    })
  )
)

Basically going through every key, and using str.includes for the dinamic ones.

But I’m curios if @jquense has a better solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Set Dynamic Property Keys with ES6 - Samantha Ming
How to Set Dynamic Property Keys with ES6. Previously, we had to do 2 steps - create the object literal and then use...
Read more >
Dynamic JavaScript Object Keys | Hackmamba
In JavaScript, object keys and values are created in numerous ways either in object literals during initialization, or assignment using dot ...
Read more >
How do I create a dynamic key to be added to a JavaScript ...
2 Answers 2 ; 'key' + i] = 'example' + 1; ; for (var i = 0; i < myArray.length; ++i) { var...
Read more >
How to create an object with dynamic keys in JavaScript
To create an object with dynamic keys in JavaScript, you can use ES6's computed property names feature. The computed property names feature ...
Read more >
Dynamic Object Key in JavaScript - Linux Hint
For adding an object key dynamically, use the bracket notation. First, store the key in a variable and then specify the variable name...
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