@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
58 lines • 1.71 kB
JavaScript
import { getTypeName } from '@backland/utils';
import { parseValidationError } from '../applyValidator';
import { createFieldTypeError } from './FieldTypeErrors';
export function arrayFieldParse(config) {
var parser = config.parser,
parserOptions = config.parserOptions,
input = config.input,
arrayOptions = config.arrayOptions;
if (!input || !Array.isArray(input)) {
throw createFieldTypeError('unexpectedType', "expected Array, found ".concat(getTypeName(input)));
}
var excludeInvalidListItems = parserOptions.excludeInvalidListItems,
customMessage = parserOptions.customMessage;
var min = arrayOptions.min,
length = arrayOptions.length,
max = arrayOptions.max;
var found = input.length;
if (min !== undefined && found < min) {
throw createFieldTypeError('minSize', {
expected: {
min: min
},
found: found
});
}
if (max !== undefined && found > max) {
throw createFieldTypeError('maxSize', {
expected: {
max: max
},
found: found
});
}
if (length !== undefined && found !== length) {
throw createFieldTypeError('sizeMismatch', {
expected: {
length: length
},
found: found
});
}
var values = [];
input.forEach(function (item, key) {
try {
var parsed = parser(item, parserOptions);
values.push(parsed);
} catch (originalError) {
if (excludeInvalidListItems) {
return;
}
var error = parseValidationError(item, customMessage, originalError);
error.message = "".concat(error.message, " at position ").concat(key);
throw error;
}
});
return values;
}
//# sourceMappingURL=ArrayFieldParse.js.map