How to Validate Members of an Array Field

How can I validate an array of fields in a reactive form?

You are using the same object to all inputs. Try this.

this.myForm = this.fb.group({
names: this.fb.array([
this.fb.group({ element_name: [null, [Validators.required]] }),
this.fb.group({ element_name: [null, [Validators.required]] }),
])
})

How to validate array in Laravel?

Asterisk symbol (*) is used to check values in the array, not the array itself.

$validator = Validator::make($request->all(), [
"names" => "required|array|min:3",
"names.*" => "required|string|distinct|min:3",
]);

In the example above:

  • "names" must be an array with at least 3 elements,
  • values in the "names" array must be distinct (unique) strings, at least 3 characters long.

EDIT: Since Laravel 5.5 you can call validate() method directly on Request object like so:

$data = $request->validate([
"name" => "required|array|min:3",
"name.*" => "required|string|distinct|min:3",
]);

Rails validating values in array

I don't think that default Rails validators will do the trick here, you can do this though:

validate :validate_wdays

def validate_wdays
if !wdays.is_a?(Array) || wdays.any?{|d| !(0..6).include?(d)}
errors.add(:wdays, :invalid)
end
end

How to Validate an array in Laravel

you can use present validation

The field under validation must be present in the input data but can be empty.

 'topics' => 'present|array'

How to validate arrays/objects in express-validator using body(), checking only the specified fields?

to check an object in an array, use: *.key

and then you can just loop your keys and add them dynamically:

const postRequiredFields = () => {

const validators = [];

baseFields.map((key) => {
const validator = body(`*.${key}`)
.exists(existsOptions)
.bail()
.isString();
validators.push(validator);
});

return validators;
};

How to validate array in json response?


pm.expect([1,2,3,4,5]).to.deep.equal([1,2,3,4,5])

or

pm.expect(_.isEqual([1,2,3,4,5],[1,2,3,4,5])).to.be.true

so in your case:

  pm.expect([
{
"kind": {
"code": "COMPANY",
"name": "Company name"
},
"value": "KKO",
"confidence": 0.0
},
{
"kind": {
"code": "AGREEMENT",
"name": "Agreement number"
},
"value": "123",
"confidence": 0.0
},
{
"kind": {
"code": "DATE_OF_ISSUE",
"name": "When it was created"
},
"value": "01/01/2019",
"confidence": 0.0
}]).to.deep.equal([
{
"kind": {
"code": "COMPANY",
"name": "Company name"
},
"value": "KKO",
"confidence": 0.0
},
{
"kind": {
"code": "AGREEMENT",
"name": "Agreement number"
},
"value": "123",
"confidence": 0.0
},
{
"kind": {
"code": "DATE_OF_ISSUE",
"name": "When it was created"
},
"value": "01/01/2019",
"confidence": 0.0
}])

Update

name = ["Company name", "Agreement number", "When it was created"]
value = ["KKO", "123", "01/01/2019"]

pm.test("something", function () {
a.forEach((a, i) => {
pm.expect(a.kind.name).to.be.equal(name[i])

pm.expect(a.value).to.be.equal(value[i])
})
})

How to validate individual element of an array of JSON objects using Yup

you can do like this way:

*I've done an example where at least one registrationNumber in array should be filled
for your vehicles schema:

vehicles: Yup.array(
Yup.object({
registrationNumber: Yup.string(),
make: Yup.string().required("make Required"),
}).test(
"registrationNumber test",
// The error message that should appears if test failed
"at least one registrationNumber should be filled",
// Function that does the custom validation to this array
validateAgainstPrevious
)
)

The function below its just an example. You can do your own logic here.

function validateAgainstPrevious() {
// In this case, parent is the entire array
const { parent } = this;

// filtered array vechicles that doens't have registrationNumber
const filteredArray = parent.filter((e) => !e.registrationNumber);

// If length of vehicles that doesn't have registrationNumber is equals to vehicles array length then return false;
if (filteredArray.length === parent.length) return false;

return true;
}

UPDATED

For the multiple error text issue, you can do a workaround:

Instead you pass TextField component to Field, like this:

<Field
component={TextField}
fullWidth={true}
label="Registration Number"
name={`vehicles[${index}].registrationNumber`}
/>

you can do this:

<Field
// component={TextField}
fullWidth={true}
label="Registration Number"
name={`vehicles[${index}].registrationNumber`}
render={() => (
<TextField
error={Boolean(errors.vehicles)}
helperText= {
errors.vehicles && getVehiclesErrors(errors.vehicles)
}
/>
)}
/>

And created this function:

const getVehiclesErrors = (errors) => {
return Array.isArray(errors)
? errors.filter((email, i, arr) => arr.indexOf(email) === i)
: errors;
};


Related Topics



Leave a reply



Submit