On attribute validation #351
-
I'm looking at this code below it says return this array. I don't want to do that I'd prefer to just return the object. If there are multiple errors then I'd change this into an array. // ./attribute-types/DateTime.js
export class DateTime {
validate(value, config) {
if (typeof value !== 'string' || isNaN(Date.parse(value)))
return [
{
id: 'invalid-datetime-type',
level: 'critical',
message: 'Must be a string with a valid date format'
}
];
return [];
}
transform(value, config) {
return Date.parse(value);
}
} Can't I just write an object? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The logic in the validator assumes that these are all arrays, it does something like: |
Beta Was this translation helpful? Give feedback.
The logic in the validator assumes that these are all arrays, it does something like:
errors.push(...errorsFromValidationFunction)
so if you return an object rather than an array, it will throw an error. In order to support returning a single object, we would have to add explicit logic in the validator in several places to check and see if the output is an array, and we'd prefer not to do that. Is there a particular reason why you don't want to return an array?