UNPKG

class-validator-extended

Version:
35 lines (34 loc) 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MAP_UNIQUE_KEYS = void 0; exports.MapUniqueKeys = MapUniqueKeys; const class_validator_1 = require("class-validator"); const map_unique_keys_predicate_1 = require("./map-unique-keys.predicate"); /** @hidden */ exports.MAP_UNIQUE_KEYS = 'mapUniqueKeys'; /** * Checks if the given value is a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) * whose keys are all unique with regard to the given projection. * * #### Example * ```typescript * // Ensure there are no duplicate user ids in the map. * @MapUniqueKeys<User>(user => user.id) * postsByUser: Map<User, Post[]> * ``` * * @category Map * @param projection The function mapping each key to the value that is used for the uniqueness check. * @param options Generic class-validator options. * @typeParam Key The type of the map's keys. * @typeParam Projection The type returned by `projection`. */ function MapUniqueKeys(projection, options) { return (0, class_validator_1.ValidateBy)({ name: exports.MAP_UNIQUE_KEYS, validator: { validate: (value, _arguments) => (0, map_unique_keys_predicate_1.mapUniqueKeys)(value, projection), defaultMessage: (0, class_validator_1.buildMessage)(eachPrefix => `${eachPrefix}$property must have unique keys`, options), }, }, options); }