UNPKG

class-validator-extended

Version:
34 lines (33 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MAP_CONTAINS = void 0; exports.MapContains = MapContains; const class_validator_1 = require("class-validator"); const map_contains_predicate_1 = require("./map-contains.predicate"); /** @hidden */ exports.MAP_CONTAINS = 'mapContains'; /** * Checks if the given value is a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) * and contains all required values. * * #### Example * ```typescript * // Ensure the map contains (at least) 'foo' and 'bar'. * @MapContains(['foo', 'bar']) * values: Map<number, string> * ``` * * @category Map * @param required The values required in the given map. * @param options Generic class-validator options. * @typeParam Value The type of values to check for. */ function MapContains(required, options) { return (0, class_validator_1.ValidateBy)({ name: exports.MAP_CONTAINS, validator: { validate: (value, _arguments) => (0, map_contains_predicate_1.mapContains)(value, required), defaultMessage: (0, class_validator_1.buildMessage)(eachPrefix => `${eachPrefix}$property must contain all of the following values: ${[...required].join(', ')}`, options), }, }, options); }