UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

70 lines (69 loc) 1.74 kB
"use strict"; 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) { const { parser, parserOptions, input, arrayOptions } = config; if (!input || !Array.isArray(input)) { throw (0, _FieldTypeErrors.createFieldTypeError)('unexpectedType', `expected Array, found ${(0, _utils.getTypeName)(input)}`); } const { excludeInvalidListItems, customMessage } = parserOptions; const { min, length, max } = arrayOptions; const found = input.length; if (min !== undefined && found < min) { throw (0, _FieldTypeErrors.createFieldTypeError)('minSize', { expected: { min }, found }); } if (max !== undefined && found > max) { throw (0, _FieldTypeErrors.createFieldTypeError)('maxSize', { expected: { max }, found }); } if (length !== undefined && found !== length) { throw (0, _FieldTypeErrors.createFieldTypeError)('sizeMismatch', { expected: { length }, found }); } const values = []; input.forEach((item, key) => { try { const parsed = parser(item, parserOptions); values.push(parsed); } catch (originalError) { if (excludeInvalidListItems) { return; } const error = (0, _applyValidator.parseValidationError)(item, customMessage, originalError); error.message = `${error.message} at position ${key}`; throw error; } }); return values; } //# sourceMappingURL=ArrayFieldParse.js.map