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.

Conditional validation on array/object validation

See original GitHub issue

I send multiple nested forms as an array of objects in my application. here’s an example: [ {"name": "A", "Pre": "", "_destroy": 0}, {"name": "B", "Pre": "", "_destroy": 1} ]

I use the suggested way to check all the values in the array and it works fine, but I wanted to know that is there any way to only check the objects that their “_destroy” is equal to 1? I tried some ways that were suggested for conditional validation but it didn’t work for this case, maybe I did it the wrong way.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
gustavohenkecommented, Jan 3, 2020

At the moment, it isn’t straightforward, but you can write something like this:

check('array.*.name')
	.if((value, { req, path, location }) => {
		// Check the sibling .destroy property, using lodash's _.get
		// https://lodash.com/docs/4.17.11#get
		return _.get(req[location], path.replace('name', 'destroy')) === 1;
	})
	.notEmpty()
1reaction
gustavohenkecommented, Jan 3, 2020

I mean that that parent thing doesn’t exist yet, it’s just a proposal. You should try the other example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

yup conditional validation of array - Stack Overflow
I want to validate the array on the base ...
Read more >
Complex Conditional Validation - Not working :( - Laracasts
I'm trying to use complex conditional validation for my form. In it, I would like for the field Furnished_2 to be required if...
Read more >
Laravel: Conditional Validation of Arrays and Nested Items
Conditional validation of Arrays and Array items with custom validation messages.
Read more >
Validation - Phalcon Documentation
Gets a value to validate in the array/object data source ... By using Phalcon\Validation\Validator\Callback you can execute custom function which must ...
Read more >
17.7.0 API Reference - joi.dev
If the input is valid, then the error will be undefined . If the input is invalid, error is assigned a ValidationError object...
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