You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.9 KiB
63 lines
1.9 KiB
// @flow
|
|
|
|
import getType from '../util/get_type.js';
|
|
import validate from './validate.js';
|
|
import ValidationError from '../error/validation_error.js';
|
|
|
|
import type {ValidationOptions} from './validate.js';
|
|
|
|
type Options = ValidationOptions & {
|
|
arrayElementValidator: Function;
|
|
};
|
|
|
|
export default function validateArray(options: Options): Array<ValidationError> {
|
|
const array = options.value;
|
|
const arraySpec = options.valueSpec;
|
|
const style = options.style;
|
|
const styleSpec = options.styleSpec;
|
|
const key = options.key;
|
|
const validateArrayElement = options.arrayElementValidator || validate;
|
|
|
|
if (getType(array) !== 'array') {
|
|
return [new ValidationError(key, array, `array expected, ${getType(array)} found`)];
|
|
}
|
|
|
|
if (arraySpec.length && array.length !== arraySpec.length) {
|
|
return [new ValidationError(key, array, `array length ${arraySpec.length} expected, length ${array.length} found`)];
|
|
}
|
|
|
|
if (arraySpec['min-length'] && array.length < arraySpec['min-length']) {
|
|
return [new ValidationError(key, array, `array length at least ${arraySpec['min-length']} expected, length ${array.length} found`)];
|
|
}
|
|
|
|
let arrayElementSpec = {
|
|
"type": arraySpec.value,
|
|
"values": arraySpec.values,
|
|
"minimum": arraySpec.minimum,
|
|
"maximum": arraySpec.maximum,
|
|
function: undefined
|
|
};
|
|
|
|
if (styleSpec.$version < 7) {
|
|
arrayElementSpec.function = arraySpec.function;
|
|
}
|
|
|
|
if (getType(arraySpec.value) === 'object') {
|
|
arrayElementSpec = arraySpec.value;
|
|
}
|
|
|
|
let errors = [];
|
|
for (let i = 0; i < array.length; i++) {
|
|
errors = errors.concat(validateArrayElement({
|
|
array,
|
|
arrayIndex: i,
|
|
value: array[i],
|
|
valueSpec: arrayElementSpec,
|
|
style,
|
|
styleSpec,
|
|
key: `${key}[${i}]`
|
|
}));
|
|
}
|
|
return errors;
|
|
}
|