UNPKG

@apizr-io/class-utils

Version:

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

43 lines 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidateSort = void 0; const class_validator_1 = require("class-validator"); function ValidateSort(allowedKeys, validationOptions) { return (object, propertyName) => { const allowedKeysArray = Array.isArray(allowedKeys) ? allowedKeys : [allowedKeys]; (0, class_validator_1.registerDecorator)({ name: 'validateSort', target: object.constructor, propertyName, constraints: allowedKeysArray, options: { message: () => { return `Allowed sort values are [${allowedKeysArray.join(',')}] with suffix :asc or :desc (default :asc if not specified)`; }, ...validationOptions, }, validator: { validate(value) { if (!Array.isArray(value)) { return false; } return value.reduce((acc, el) => { if (typeof el !== 'string') { return false; } const [key, direction] = el.split(':'); const lowerCasedDirection = (direction || 'asc').toLocaleLowerCase(); if (!['asc', 'desc'].includes(lowerCasedDirection)) { return false; } return acc || allowedKeysArray.includes(key); }, false); }, }, }); }; } exports.ValidateSort = ValidateSort; //# sourceMappingURL=sort.decorator.js.map