UNPKG

@team-supercharge/nest-amqp

Version:
88 lines (87 loc) 4.64 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectValidatorService = void 0; const common_1 = require("@nestjs/common"); const class_transformer_1 = require("class-transformer"); const class_validator_1 = require("class-validator"); const exceptions_1 = require("../../util/exceptions"); /** * Class to validate an object or an array of objects. */ let ObjectValidatorService = class ObjectValidatorService { /** * Transforme and validate a source object by a decorated class. It works with * the `class-validator` and the `class-transformer` packages. * * By default, the validator will strip every property that is not explicitly exposed * * @param {new (...params: unknown[]) => T} type Class with validation and transformation decorators. * @param {unknown} plain Source object which will be transformed and validated. * @param {ObjectValidationOptions} options Transformation and validations options. * * @return {Promise<T>} The transformed and validated object. * * {@link https://www.npmjs.com/package/class-transformer class-transformer} * {@link https://www.npmjs.com/package/class-validator class-validator} * * @public */ async validate(type, plain, options) { var _a, _b; if (plain === null || plain === undefined) { throw new exceptions_1.ValidationNullObjectException(type.name); } const transformerOptions = (_a = options === null || options === void 0 ? void 0 : options.transformerOptions) !== null && _a !== void 0 ? _a : {}; const validatorOptions = (_b = options === null || options === void 0 ? void 0 : options.validatorOptions) !== null && _b !== void 0 ? _b : null; const object = class_transformer_1.plainToInstance(type, plain, Object.assign({ strategy: 'excludeAll' }, transformerOptions)); const errors = await class_validator_1.validate(object, validatorOptions); if (errors.length !== 0) { throw new exceptions_1.ValidationException(errors); } return object; } /** * Validate and transform a list of objects by a decorated class. It works * with the `class-validator` and the `class-transformer` packages. * * By default, the validator will strip every property that is not explicitly exposed * * @param {new (...params: unknown[]) => T} type Class with validation and transformation decorators. * @param {unknown[]} plains Source array of object which will be transformed and validated. * @param {ObjectValidationOptions} options Transformation and validations options. * * @return {Promise<T[]>} Validated and transformed array. * * {@link https://www.npmjs.com/package/class-transformer class-transformer} * {@link https://www.npmjs.com/package/class-validator class-validator} * * @public */ async validateArray(type, plains, options) { var _a, _b; if (plains === null || plains === undefined) { throw new exceptions_1.ValidationNullObjectException(type.name); } const transformerOptions = (_a = options === null || options === void 0 ? void 0 : options.transformerOptions) !== null && _a !== void 0 ? _a : {}; const validatorOptions = (_b = options === null || options === void 0 ? void 0 : options.validatorOptions) !== null && _b !== void 0 ? _b : {}; const objects = class_transformer_1.plainToInstance(type, plains, Object.assign({ strategy: 'excludeAll' }, transformerOptions)); const errors = []; for (const object of objects) { errors.push(...(await class_validator_1.validate(object, validatorOptions))); } if (errors.length !== 0) { throw new exceptions_1.ValidationException(errors); } return objects; } }; ObjectValidatorService = __decorate([ common_1.Injectable() ], ObjectValidatorService); exports.ObjectValidatorService = ObjectValidatorService;