UNPKG

@apizr-io/class-utils

Version:

Package containing all class-validator function with all custom apizr class validation functions

62 lines 2.92 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.ValidateOnlyOneKeyGroupExists = exports.ValidateAtLeastOneKeyGroupExists = void 0; const class_validator_1 = require("class-validator"); const class_decorator_1 = require("./class.decorator"); let ValidateOneKeyGroupExistsConstraint = class ValidateOneKeyGroupExistsConstraint { // validate the presence of keys in groups validate(value, args) { const [requiredKeysGroups, xor] = args.constraints; // determining the number of valid groups const validGroups = requiredKeysGroups.reduce((total, keys) => { // if you are not in exclusive or and at least one valid group has already been found if (!xor && total > 0) return total; // determinate if keys exist in object return keys.every((key) => key in args.object) ? total + 1 : total; }, 0); return xor ? validGroups === 1 : validGroups >= 1; } defaultMessage(args) { const [requiredKeysGroups, xor] = args.constraints; // error message if validation failed return (`${xor ? 'Only one' : 'One'} of groups of fields must exist together: ` + requiredKeysGroups .map((keys) => `[${keys.join(', ')}]`) .join(' or ')); } }; ValidateOneKeyGroupExistsConstraint = __decorate([ (0, class_validator_1.ValidatorConstraint)() ], ValidateOneKeyGroupExistsConstraint); /** * Allows validating at least one key group exists */ function ValidateAtLeastOneKeyGroupExists(groups) { return (0, class_decorator_1.registerClassValidator)({ name: 'ValidateAtLeastOneKeyGroupExists', validator: ValidateOneKeyGroupExistsConstraint, constraints: [groups, false], }); } exports.ValidateAtLeastOneKeyGroupExists = ValidateAtLeastOneKeyGroupExists; /** * Allows validating only one key group exists */ function ValidateOnlyOneKeyGroupExists(groups) { return (0, class_decorator_1.registerClassValidator)({ name: 'ValidateOnlyOneKeyGroupExists', validator: ValidateOneKeyGroupExistsConstraint, constraints: [groups, true], }); } exports.ValidateOnlyOneKeyGroupExists = ValidateOnlyOneKeyGroupExists; //# sourceMappingURL=uniqueness.decorator.js.map