class-validator-extended
Version:
Additional validators for class-validator.
33 lines (32 loc) • 1.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ARRAY_SIZE = void 0;
exports.ArraySize = ArraySize;
const class_validator_1 = require("class-validator");
const array_size_predicate_1 = require("./array-size.predicate");
/** @hidden */
exports.ARRAY_SIZE = 'arraySize';
/**
* Checks if the given value is an array with a size of exactly `size`.
*
* #### Example
* ```typescript
* // Ensure the array has exactly 5 entries.
* @ArraySize(5)
* values: string[]
* ```
*
* @category Array
* @param size The allowed size of the array.
* @param options Generic class-validator options.
*/
function ArraySize(size, options) {
return (0, class_validator_1.ValidateBy)({
name: exports.ARRAY_SIZE,
constraints: [size],
validator: {
validate: (value, _arguments) => (0, array_size_predicate_1.arraySize)(value, size),
defaultMessage: (0, class_validator_1.buildMessage)(eachPrefix => `${eachPrefix}$property must contain exactly $constraint1 element(s)`, options),
},
}, options);
}