UNPKG

class-validator-extended

Version:
35 lines (34 loc) 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MAP_UNIQUE = void 0; exports.MapUnique = MapUnique; const class_validator_1 = require("class-validator"); const map_unique_predicate_1 = require("./map-unique.predicate"); /** @hidden */ exports.MAP_UNIQUE = 'mapUnique'; /** * Checks if the given value is a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) * without duplicates with regard to the given projection. * * #### Example * ```typescript * // Ensure the map does not include duplicate users. * @MapUnique<User>(user => user.id) * usersByEmail: Map<string, User> * ``` * * @category Map * @param projection The function mapping each map entry to the value that is used for the uniqueness check. * @param options Generic class-validator options. * @typeParam Value The type of the map's values. * @typeParam Projection The type returned by `projection`. */ function MapUnique(projection, options) { return (0, class_validator_1.ValidateBy)({ name: exports.MAP_UNIQUE, validator: { validate: (value, _arguments) => (0, map_unique_predicate_1.mapUnique)(value, projection), defaultMessage: (0, class_validator_1.buildMessage)(eachPrefix => `${eachPrefix}$property must have unique values`, options), }, }, options); }