Skip to content

Commit 5ac9fa0

Browse files
committed
fork: Make isOptional behaviour more strict
1 parent d745181 commit 5ac9fa0

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
[![Build Status](https://travis-ci.org/typestack/class-validator.svg?branch=master)](https://travis-ci.org/typestack/class-validator)
44
[![npm version](https://badge.fury.io/js/class-validator.svg)](https://badge.fury.io/js/class-validator)
55
[![install size](https://packagephobia.now.sh/badge?p=class-validator)](https://packagephobia.now.sh/result?p=class-validator)
6-
[![Join the chat at https://gitter.im/typestack/class-validator](https://badges.gitter.im/typestack/class-validator.svg)](https://gitter.im/typestack/class-validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
6+
[![Join the chat at https://gitter.im/typestack/class-validator](https://badges.gitter.im/typestack/class-validator.svg)](https://gitter.im/typestack/class-validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7+
8+
> ⚠️ This is a fork of the original `class-validator` package. It adds a validation property specifically to the `isOptional()` decorator called `nullable`, defaulting to `true` if not supplied (the original behaviour), and if set to `false` will not allow `null` values to be treated as empty.
79
810
Allows use of decorator and non-decorator based validation.
911
Internally uses [validator.js][1] to perform validation.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "class-validator",
2+
"name": "@socialenergy/class-validator",
33
"private": true,
44
"version": "0.12.2",
55
"description": "Class-based validation with Typescript / ES6 / ES5 using decorators or validation schemas. Supports both node.js and browser",

src/decorator/common/IsOptional.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,38 @@ import { ValidationTypes } from "../../validation/ValidationTypes";
44
import { ValidationMetadata } from "../../metadata/ValidationMetadata";
55
import { getMetadataStorage } from "../../metadata/MetadataStorage";
66

7+
interface IsOptionalValidationOptions extends ValidationOptions {
8+
/**
9+
* If true, both `undefined` and `null` will be allowed inputs. If `false`,
10+
* only `undefined` will be an allowed input. Defaults to true.
11+
*/
12+
nullable?: boolean;
13+
}
14+
715
/**
816
* Checks if value is missing and if so, ignores all validators.
917
*/
10-
export function IsOptional(validationOptions?: ValidationOptions): PropertyDecorator {
18+
export function IsOptional(
19+
validationOptions?: IsOptionalValidationOptions
20+
): PropertyDecorator {
21+
validationOptions = { nullable: true, ...validationOptions };
22+
1123
return function (object: Object, propertyName: string) {
1224
const args: ValidationMetadataArgs = {
1325
type: ValidationTypes.CONDITIONAL_VALIDATION,
1426
target: object.constructor,
1527
propertyName: propertyName,
16-
constraints: [(object: any, value: any) => {
17-
return object[propertyName] !== null && object[propertyName] !== undefined;
18-
}],
19-
validationOptions: validationOptions
28+
constraints: [
29+
(object: any, value: any) =>
30+
validationOptions.nullable
31+
? value !== undefined && value !== null
32+
: object.hasOwnProperty(propertyName),
33+
],
34+
validationOptions: validationOptions,
2035
};
21-
getMetadataStorage().addValidationMetadata(new ValidationMetadata(args));
36+
37+
getMetadataStorage().addValidationMetadata(
38+
new ValidationMetadata(args)
39+
);
2240
};
2341
}

0 commit comments

Comments
 (0)