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