@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
64 lines (63 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.arrayFieldParse = arrayFieldParse;
var _utils = require("@backland/utils");
var _applyValidator = require("../applyValidator");
var _FieldTypeErrors = require("./FieldTypeErrors");
function arrayFieldParse(config) {
var parser = config.parser,
parserOptions = config.parserOptions,
input = config.input,
arrayOptions = config.arrayOptions;
if (!input || !Array.isArray(input)) {
throw (0, _FieldTypeErrors.createFieldTypeError)('unexpectedType', "expected Array, found ".concat((0, _utils.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 (0, _FieldTypeErrors.createFieldTypeError)('minSize', {
expected: {
min: min
},
found: found
});
}
if (max !== undefined && found > max) {
throw (0, _FieldTypeErrors.createFieldTypeError)('maxSize', {
expected: {
max: max
},
found: found
});
}
if (length !== undefined && found !== length) {
throw (0, _FieldTypeErrors.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 = (0, _applyValidator.parseValidationError)(item, customMessage, originalError);
error.message = "".concat(error.message, " at position ").concat(key);
throw error;
}
});
return values;
}
//# sourceMappingURL=ArrayFieldParse.js.map