@apizr-io/class-utils
Version:
Package containing all class-validator function with all custom apizr class validation functions
40 lines • 1.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OneOfKeysIsDefined = void 0;
const class_validator_1 = require("class-validator");
function exists(val) {
return typeof val !== 'undefined' && val !== null;
}
function keysExistInObject(keys, object) {
return keys.reduce((acc, val) => {
return acc || exists(object[val]);
}, false);
}
function OneOfKeysIsDefined(keysToValidate, validationOptions) {
return (object, propertyName) => {
(0, class_validator_1.registerDecorator)({
name: 'oneOfKeysIsDefined',
target: object.constructor,
propertyName,
constraints: [],
options: {
message: () => {
return `One of the following keys [${keysToValidate.join(',')}] must be defined in ${propertyName}`;
},
...validationOptions,
},
validator: {
validate(value) {
if (validationOptions && validationOptions.each) {
return value.reduce((acc, obj) => {
return acc && keysExistInObject(keysToValidate, obj);
}, true);
}
return keysExistInObject(keysToValidate, value);
},
},
});
};
}
exports.OneOfKeysIsDefined = OneOfKeysIsDefined;
//# sourceMappingURL=object.decorator.js.map