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.

Unable to access nested objects using `pick`

See original GitHub issue

It seems as though we should be able to access nested objects using object().pick.

example:

const schema = yup.object({
  name: yup.string().required(),
  address: yup.object({
    line1: yup.string.required(),
    ...
  })
})
schema.pick(['name']) // returns schema with only `name`
schema.pick(['address.line1') // returns no schema =(
})

Is this something that should be supported? I have a use case where i’d like to define a full schema, then need to validate 1 field at a time (multi step form). then – validate the full object at the end via the full schema.

I have attempted some stuff “building” a schema on the fly based on lodash.pick but first party access would be a life saver.

Thanks for your great work!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
rkingoncommented, Mar 13, 2021

thanks @jakobsandberg - i am well aware of yup.reach but in this case I need to actually grab multiple keys at a time.

What I ended up doing was building a recursive function that converts POJO to yup.object

function toYupSchema (pojo = {}) {
	return yup.object(Object.entries(pojo).reduce((acc, [ key, value ]) => {
		if(typeof value === 'object' && !('__isYupSchema__' in value)) {
			acc[key] = toYupSchema(value)
		} else {
			acc[key] = value
		}
		return acc
	}, {}))
}

Then I can have a greater schema, something like

const userSchema = {
  firstName: yup.string(),
  lastName: yup.string(),
  address: {
    line1: yup.string(),
    ...
  }
}

then a combination of the wrapper function and lodash.pick

toYupSchema(_.pick(userScema, ['firstName, 'lastName'])) toYupSchema(_.pick(userScema, ['address.line1, 'address.line2'])) toYupSchema(_.pick(userScema, ['address.city, 'address.state', 'address.postalCode]))

The intent here is because we’re building a multi step form (wizard) that may ask 1 to many sub set of schema

0reactions
rkingoncommented, Aug 23, 2022

@jquense i see this marked as completed, awesome! how can we use this new feature? super appreciate your work on this package

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing Nested Objects in JavaScript
One of those things is the confrontation with this error when you try to access a nested object,. Cannot read property 'foo' of...
Read more >
How can I access and process nested objects, arrays, or ...
Use console.log or console.dir and inspect the structure of object / array. The property you are trying to access might be actually defined...
Read more >
Accessing Nested Objects in JavaScript
One of those things is the confrontation with this error when you try to access a nested object,. Cannot read property 'foo' of...
Read more >
4 Ways to Safely Access Nested Objects in Vanilla Javascript
The most straightforward way to deal with that is to check if an object is null/undefined before attempting to access the data within....
Read more >
TypeScript Pick nested object
But is it possible to get a type from a nested object Yes! It's possible to get a type by using brackets with...
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