UNPKG

express-image-validator

Version:

Validator of various image parameters in Express.js applications

65 lines (64 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidationState = void 0; exports.imageValidationResult = imageValidationResult; const bindAll_1 = require("../utils/bindAll"); /** * Copyright (c) 2025 [express-validator](https://github.com/express-validator). * * Licensed under the MIT License. * @see https://github.com/express-validator/express-validator/blob/61d3745e73ac628ac3a58dda02ba64d76d835630/src/validation-result.ts */ /** * The current state of the validation errors in a request. */ class ValidationState { constructor(errors) { this.errors = errors; } /** * Gets the validation errors as an array. * * @param options.onlyFirstError Whether only the first error of each. */ array(options) { return options?.onlyFirstError ? Object.values(this.mapped()) : this.errors; } /** * Gets the validation errors as an object. * If a field has more than one error, only the first one is set in the resulting object. * * @returns An object from field name to error. */ mapped() { return this.errors.reduce((mapping, error) => { const key = error.field; if (!mapping[key]) { // eslint-disable-next-line no-param-reassign mapping[key] = [error]; } return mapping; }, {}); } /** * @returns `true` if there are no errors, `false` otherwise. */ isEmpty() { return this.errors.length === 0; } /** * Throws an error if there are validation errors. */ throw() { if (!this.isEmpty()) { throw Object.assign(new Error(), (0, bindAll_1.bindAll)(this)); } } } exports.ValidationState = ValidationState; /** * Extracts the validation errors of an Express request */ function imageValidationResult(req) { return new ValidationState(req?.IMAGE_VALIDATION_RESULTS || []); }