UNPKG

@apizr-io/class-utils

Version:

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

56 lines 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MustNotExistIf = void 0; const class_validator_1 = require("class-validator"); const jsonpath_plus_1 = require("jsonpath-plus"); /** * @param {function} conditionFunc - The condition to check * @param {object} options - The options to pass to the decorator * @param {string} options.jsonpath - The jsonpath to use to get the object to check * @param {ValidationOptions} options.validationOptions - The validation options to pass to the decorator * * @description Decorator that checks if a property must not exist if a condition is true. * Ignores other decorators on the property if it does not exist and the condition is true. */ function MustNotExistIf(conditionFunc, options) { return (object, propertyName) => { (0, class_validator_1.ValidateIf)((obj, value) => { const checkedObject = (options === null || options === void 0 ? void 0 : options.jsonpath) ? (0, jsonpath_plus_1.JSONPath)({ path: options === null || options === void 0 ? void 0 : options.jsonpath, json: obj }) : obj; if (conditionFunc(checkedObject) && value !== undefined && value !== null) { return true; } return !conditionFunc(checkedObject); })(object, propertyName); (0, class_validator_1.registerDecorator)({ name: 'mustNotExistIf', target: object.constructor, propertyName, constraints: [conditionFunc, options === null || options === void 0 ? void 0 : options.jsonpath], options: { message: (args) => `Property ${args.property} must not exist if ${conditionFunc} is true`, ...options === null || options === void 0 ? void 0 : options.validationOptions, }, validator: { validate(value, args) { const [condition, jsonpath] = args.constraints; const checkedObject = jsonpath ? (0, jsonpath_plus_1.JSONPath)({ path: jsonpath, json: args.object }) : args.object; if (condition(checkedObject)) { return value === undefined || value === null; } return true; }, defaultMessage(args) { return `Property ${args.property} must not exist if ${conditionFunc} is true`; }, }, }); }; } exports.MustNotExistIf = MustNotExistIf; //# sourceMappingURL=exist.decorator.js.map