diff --git a/README.md b/README.md index de7d9f469b..51c3491318 100644 --- a/README.md +++ b/README.md @@ -836,6 +836,8 @@ validator.isISBN(str, version); // Checks if the string is an ISBN (version 10 o validator.isISIN(str); // Checks if the string is an ISIN (stock/security identifier). validator.isISO8601(str); // Checks if the string is a valid ISO 8601 date. validator.isJSON(str); // Checks if the string is valid JSON (note: uses JSON.parse). +validator.isObject(object); // Checks if the object is valid Object (null, functions, arrays will return false) +validator.isNotEmptyObject(object); // Checks if the object is not empty validator.isLowercase(str); // Checks if the string is lowercase. validator.isMobilePhone(str, locale); // Checks if the string is a mobile phone number. validator.isISO31661Alpha2(str); // Check if the string is a valid ISO 3166-1 alpha-2 @@ -922,6 +924,8 @@ validator.isInstance(value, target); // Checks value is an instance of the targe | `@IsISIN()` | Checks if the string is an ISIN (stock/security identifier). | | `@IsISO8601()` | Checks if the string is a valid ISO 8601 date. | | `@IsJSON()` | Checks if the string is valid JSON. | +| `@IsObject()` | Checks if the object is valid Object (null, functions, arrays will return false). | +| `@IsNotEmptyObject()` | Checks if the object is not empty. | | `@IsLowercase()` | Checks if the string is lowercase. | | `@IsMobilePhone(locale: string)` | Checks if the string is a mobile phone number. | | `@IsISO31661Alpha2()` | Check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. | diff --git a/src/decorator/decorators.ts b/src/decorator/decorators.ts index 7e2bacba05..415742b4c8 100644 --- a/src/decorator/decorators.ts +++ b/src/decorator/decorators.ts @@ -875,6 +875,36 @@ export function IsJSON(validationOptions?: ValidationOptions) { }; } +/** + * Checks if the value is a valid object. + */ +export function IsObject(validationOptions?: ValidationOptions) { + return function (object: Object, propertyName: string) { + const args: ValidationMetadataArgs = { + type: ValidationTypes.IS_OBJECT, + target: object.constructor, + propertyName: propertyName, + validationOptions: validationOptions + }; + getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args)); + }; +} + +/** + * Checks if the value is a valid object & not empty. + */ +export function IsNotEmptyObject(validationOptions?: ValidationOptions) { + return function (object: Object, propertyName: string) { + const args: ValidationMetadataArgs = { + type: ValidationTypes.IS_NOT_EMPTY_OBJECT, + target: object.constructor, + propertyName: propertyName, + validationOptions: validationOptions + }; + getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args)); + }; +} + /** * Checks if the string is lowercase. */ diff --git a/src/validation/ValidationTypes.ts b/src/validation/ValidationTypes.ts index e59bf404d3..e4e7960a4e 100644 --- a/src/validation/ValidationTypes.ts +++ b/src/validation/ValidationTypes.ts @@ -70,6 +70,8 @@ export class ValidationTypes { static IS_ISIN = "isIsin"; static IS_ISO8601 = "isIso8601"; static IS_JSON = "isJson"; + static IS_OBJECT = "isObject"; + static IS_NOT_EMPTY_OBJECT = "isNotEmptyObject"; static IS_LOWERCASE = "isLowercase"; static IS_MOBILE_PHONE = "isMobilePhone"; static IS_PHONE_NUMBER = "isPhoneNumber"; @@ -220,6 +222,10 @@ export class ValidationTypes { return eachPrefix + "$property must be a valid ISO 8601 date string"; case this.IS_JSON: return eachPrefix + "$property must be a json string"; + case this.IS_OBJECT: + return eachPrefix + "$property must be an object"; + case this.IS_NOT_EMPTY_OBJECT: + return eachPrefix + "$property must be a non-empty object"; case this.IS_LOWERCASE: return eachPrefix + "$property must be a lowercase string"; case this.IS_MOBILE_PHONE: diff --git a/src/validation/Validator.ts b/src/validation/Validator.ts index ceeac049a1..0d66fce36a 100644 --- a/src/validation/Validator.ts +++ b/src/validation/Validator.ts @@ -20,6 +20,15 @@ export class Validator { private libPhoneNumber = { phoneUtil: require("google-libphonenumber").PhoneNumberUtil.getInstance(), }; + private _isEmptyObject = function(object: object) { + for (const key in object) { + if (object.hasOwnProperty(key)) { + return false; + } + } + + return true; + }; /** * Performs validation of the given object based on decorators or validation schema. @@ -215,6 +224,10 @@ export class Validator { return this.isISO8601(value); case ValidationTypes.IS_JSON: return this.isJSON(value); + case ValidationTypes.IS_OBJECT: + return this.isObject(value); + case ValidationTypes.IS_NOT_EMPTY_OBJECT: + return this.isNotEmptyObject(value); case ValidationTypes.IS_LOWERCASE: return this.isLowercase(value); case ValidationTypes.IS_MOBILE_PHONE: @@ -659,6 +672,22 @@ export class Validator { return typeof value === "string" && this.validatorJs.isJSON(value); } + /** + * Checks if the value is valid Object. + * Returns false if the value is not an object. + */ + isObject(value: any): boolean { + return value != null && (typeof value === "object" || typeof value === "function") && !Array.isArray(value); + } + + /** + * Checks if the value is valid Object & not empty. + * Returns false if the value is not an object or an empty valid object. + */ + isNotEmptyObject(value: any): boolean { + return this.isObject(value) && !this._isEmptyObject(value); + } + /** * Checks if the string is lowercase. * If given value is not a string, then it returns false. diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index d6f86d5ca3..0ba7b024e8 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -33,6 +33,8 @@ import { IsIn, IsInt, IsJSON, + IsObject, + IsNotEmptyObject, Length, IsLowercase, IsMongoId, @@ -2231,6 +2233,74 @@ describe("IsJSON", function() { }); +describe("IsObject", function() { + + const validValues = [{ "key": "value" }, { key: "value" }, {}]; + const invalidValues: any[] = [null, undefined, "{ key: \"value\" }", "{ 'key': 'value' }", "string", 1234, false, "[]", [], [{ key: "value" }]]; + + class MyClass { + @IsObject() + someProperty: object; + } + + it("should not fail if validator.validate said that its valid", function(done) { + checkValidValues(new MyClass(), validValues, done); + }); + + it("should fail if validator.validate said that its invalid", function(done) { + checkInvalidValues(new MyClass(), invalidValues, done); + }); + + it("should not fail if method in validator said that its valid", function() { + validValues.forEach(value => validator.isObject(value).should.be.true); + }); + + it("should fail if method in validator said that its invalid", function() { + invalidValues.forEach(value => validator.isObject(value).should.be.false); + }); + + it("should return error object with proper data", function(done) { + const validationType = "isObject"; + const message = "someProperty must be an object"; + checkReturnedError(new MyClass(), invalidValues, validationType, message, done); + }); + +}); + +describe("IsNotEmptyObject", function() { + + const validValues = [{ "key": "value" }, { key: "value" }]; + const invalidValues = [null, undefined, "{ key: \"value\" }", "{ 'key': 'value' }", "string", 1234, false, {}, [], [{ key: "value" }]]; + + class MyClass { + @IsNotEmptyObject() + someProperty: object; + } + + it("should not fail if validator.validate said that its valid", function(done) { + checkValidValues(new MyClass(), validValues, done); + }); + + it("should fail if validator.validate said that its invalid", function(done) { + checkInvalidValues(new MyClass(), invalidValues, done); + }); + + it("should not fail if method in validator said that its valid", function() { + validValues.forEach(value => validator.isNotEmptyObject(value).should.be.true); + }); + + it("should fail if method in validator said that its invalid", function() { + invalidValues.forEach(value => validator.isNotEmptyObject(value).should.be.false); + }); + + it("should return error object with proper data", function(done) { + const validationType = "isNotEmptyObject"; + const message = "someProperty must be a non-empty object"; + checkReturnedError(new MyClass(), invalidValues, validationType, message, done); + }); + +}); + describe("IsLowercase", function() { const validValues = [