UNPKG

@nodeboot/starter-validation

Version:

Node-Boot starter package for Beans Validations

69 lines 4.22 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidationsConfiguration = void 0; const core_1 = require("@nodeboot/core"); const context_1 = require("@nodeboot/context"); /** * This starter package provides an auto-configuration mechanism for API request validation using `class-validator`. * The configuration options are loaded from the application configuration file (`app-config.yaml`) * under the `api.validations` section. If no configuration is found, default settings are applied. */ let ValidationsConfiguration = class ValidationsConfiguration { /** * Configures API request validations based on `class-validator` options. * * These options are configurable in the `app-config.yaml` file under `api.validations` config path. * * @typedef {Object} ValidatorOptions * @description * Options passed to `class-validator` to configure validation behavior. * @property {boolean} [enableDebugMessages] - Enables debug messages. * @property {boolean} [skipUndefinedProperties] - Skips validation for undefined properties. * @property {boolean} [skipNullProperties] - Skips validation for null properties. * @property {boolean} [skipMissingProperties] - Skips validation for missing properties. * @property {boolean} [whitelist] - Removes non-decorated properties. * @property {boolean} [forbidNonWhitelisted] - Throws an error if non-whitelisted properties exist. * @property {string[]} [groups] - Specifies validation groups. * @property {boolean} [always] - Applies validation to all properties by default. * @property {boolean} [strictGroups] - Ignores decorators with at least one group if `groups` is empty. * @property {boolean} [dismissDefaultMessages] - Disables default error messages. * @property {Object} [validationError] - Configures error responses. * @property {boolean} [validationError.target] - Includes the target object in validation errors. * @property {boolean} [validationError.value] - Includes validated values in errors. * @property {boolean} [forbidUnknownValues] - Fails validation for unknown objects. * @property {boolean} [stopAtFirstError] - Stops validation after the first error. * @param {BeansContext} context - The application context providing logger and configuration. */ validationConfig({ logger, config }) { logger.info("Configuring API validations"); const validationProperties = config.getOptional("api.validations"); if (validationProperties) { context_1.ApplicationContext.get().validation = validationProperties; logger.info("API validations successfully configured with configs from app-config.yaml file"); } else { context_1.ApplicationContext.get().validation = true; logger.info("API validations successfully configured with default configurations"); } } }; exports.ValidationsConfiguration = ValidationsConfiguration; __decorate([ (0, core_1.Bean)(), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], ValidationsConfiguration.prototype, "validationConfig", null); exports.ValidationsConfiguration = ValidationsConfiguration = __decorate([ (0, core_1.Configuration)() ], ValidationsConfiguration); //# sourceMappingURL=ValidationsConfiguration.js.map