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.

How to create optional object with required fields?

See original GitHub issue

Hi, is it possible to create the following schema:

yup.object({
  foo: yup.object({
    bar: yup.string().required()
  })
})

where you can either provide foo or not, but if you do, you must provide bar? Running the above schema on {}, I’m getting foo.bar is required

Issue Analytics

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

github_iconTop GitHub Comments

14reactions
jquensecommented, Dec 11, 2020

const parent = object({ child: object({ name: string.required() }).default(undefined) })

9reactions
jquensecommented, Feb 6, 2020

Objects automatically default there fields which is why its complaining. If you want the object to be optional and empty by default set .default() to null or undefined on the foo object schema

Read more comments on GitHub >

github_iconTop Results From Across the Web

Guide To Java 8 Optional | Baeldung
There are several ways of creating Optional objects. To create an empty Optional object, we simply need to use its empty() static method:....
Read more >
Java Optional Tutorial with Examples - CalliCoder
1. Create an empty Optional. An empty Optional Object describes the absence of a value. · 2. Create an Optional with a non-null...
Read more >
3 Different ways to create Java 8 Optional object with examples
This blog explains 3 different ways to create java 8 optional object with real life examples. Most common ways are using empty, ...
Read more >
How to use Optional in Java - Developer.com
Once you have created an Optional object, you can use the isPresent() method to check if it contains a non-null value. If it...
Read more >
How to make a field optional? - java - Stack Overflow
5. There is no concept for this in Java. Most people would just assign null to it and return Optional<String> on methods that...
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