Here is a sample validation function I created for handling display delay properties in milliseconds for an item that toggles visibility on and off the screen. The property can be either a number representing both the show and hide delays, or it can be an object defining separate delays for each case.
To ensure the correct data type, I validate each expected key to match the type 'number'. If a key is missing, its type will default to 'undefined'. Additionally, negative values are not permissible in this scenario.
props: {
delay: {
type: [Number, Object],
default: 0,
validator(value) {
if (typeof value === 'number') {
return value >= 0;
} else if (value !== null && typeof value === 'object') {
return typeof value.show === 'number' &&
typeof value.hide === 'number' &&
value.show >= 0 &&
value.hide >= 0;
}
return false;
}
},
}