class-validator-extended
Version:
Additional validators for class-validator.
34 lines (33 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAP_MAX_SIZE = void 0;
exports.MapMaxSize = MapMaxSize;
const class_validator_1 = require("class-validator");
const map_max_size_predicate_1 = require("./map-max-size.predicate");
/** @hidden */
exports.MAP_MAX_SIZE = 'mapMaxSize';
/**
* Checks if the given value is a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
* with a size less than or equal to `maximum`.
*
* #### Example
* ```typescript
* // Ensure the map has at most 5 entries.
* @MapMaxSize(5)
* values: Map<string, string>
* ```
*
* @category Map
* @param maximum The maximum allowed size of the map.
* @param options Generic class-validator options.
*/
function MapMaxSize(maximum, options) {
return (0, class_validator_1.ValidateBy)({
name: exports.MAP_MAX_SIZE,
constraints: [maximum],
validator: {
validate: (value, _arguments) => (0, map_max_size_predicate_1.mapMaxSize)(value, maximum),
defaultMessage: (0, class_validator_1.buildMessage)(eachPrefix => `${eachPrefix}$property must contain not more than $constraint1 elements`, options),
},
}, options);
}