Unable to access nested objects using `pick`
See original GitHub issueIt 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:
- Created 3 years ago
- Comments:11 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
Then I can have a greater schema, something like
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
@jquense i see this marked as completed, awesome! how can we use this new feature? super appreciate your work on this package