class-validator-extended
Version:
Additional validators for class-validator.
27 lines (26 loc) • 908 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Optional = Optional;
const class_validator_1 = require("class-validator");
/**
* Only validates the given value if it is not `undefined`.
*
* This is similar to the built-in `@IsOptional` except that it does not allow `null`. Note that this validator does not
* work as expected with `{ each: true }` (because it is based on `@ValidateIf` which does not either).
*
* #### Example
* ```typescript
* // Ensure the value is a string or undefined (but not null).
* @Optional()
* @IsString()
* value?: string
* ```
*
* @category Common
* @param options Generic class-validator options.
*/
function Optional(options) {
return function optionalDecorator(prototype, propertyKey) {
(0, class_validator_1.ValidateIf)(object => object[propertyKey] !== undefined, options)(prototype, propertyKey);
};
}