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.

Require at least one set of data in array to require all fields

See original GitHub issue

Hi There,

Im currently attempting to validate an array of this object shape:

export const exampleDataType = yup.object().shape({
	email: yup.string().email("Email must be valid"),
	phone: yup.string(),
	name: yup.string(),
});

with the only condition being that at least one element of the array has to require all of the fields in the above object, but not all of the elements.

Example:

VALID:

[{
	email: "",
	phone: "1234",
	name: "name"
},
{
	email: "yup@email.com",
	phone: "1234",
	name: "yup"
}] 

INVALID

[{
	email: "",
	phone: "1234",
	name: "name"
},
{
	email: "yup@email.com",
	phone: "",
	name: "yup"
}]

I’ve set up a basic sandbox, but without any real attempt to recreate as all my attemps have been absolute messes and can’t get them to work.

https://codesandbox.io/s/cranky-feistel-ozwuy?file=/src/index.test.js

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
knicolacommented, Nov 3, 2020

right, scratch what i said. .isValidSync() should do

const arrayOfExample = yup.array().compact(value => {
  return yup.object({
    email: yup.string().required(),
    phone: yup.string().required(),
    name: yup.string().required(),
  }).isValidSync(value)
}).of(exampleDataType).min(1)
0reactions
jquensecommented, Nov 3, 2020

the iterator will not receive a promise. async / await should return just the result.

not quite, the return value of the functions is still a promise, async/await just makes async stuff look synchronous, it doesn’t actually make it sync, code receiving the value needs to expect and handle a promise

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to require at least one field from a set of defined ...
If you really don't care which one is required then you could just ensure there is at least one key in the object...
Read more >
How to specify require for at least one of two elements? #180
My Schema has two array elements (recurring_schedule and specific_date) and I want to have at least one of them (both can also exist)...
Read more >
Chapter 7: Arrays - cs.utsa.edu
An array is an object that stores many values of the same type. ... This will require passing a String as a parameter...
Read more >
Data Structures: Objects and Arrays - Eloquent JavaScript
Many types of information require more than one atom, though. Objects allow us to group values—including other objects—to build more complex structures.
Read more >
How to Require One Symfony Form Field and/or Another
The core of the forms component is a set of abstractions for handling form data in general, but actually doesn't do much. Almost...
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