node-input-validator icon indicating copy to clipboard operation
node-input-validator copied to clipboard

Validator throws Cannot read property 'xxx' of null

Open Valmal2020 opened this issue 4 years ago • 2 comments

Hi,

Please, consider the following rule for a some request input, let's say to update user settings:

{
	"settings": "object|required",
	"settings.enableBlackTheme": "boolean",
	"info": "object",
	"info.displayName": "string"
}

Info is not required to be passed but if a client sends a payload as

{
	"settings": {
		"enableBlackTheme": true
	},
	"info": null
}

Validator throws

'TypeError: Cannot read property 'displayName' of null
    at ...\node_modules\node-input-validator\lib\validator.js:248:22
    at Array.map (<anonymous>)
    at Validator.parseValue (...\node_modules\node-input-validator\lib\validator.js:244:14)
    at Validator.apply (...\node_modules\node-input-validator\lib\validator.js:339:31)
    at ...\node_modules\node-input-validator\lib\validator.js:209:29
    at Array.forEach (<anonymous>)
    at Validator.check (...\node_modules\node-input-validator\lib\validator.js:203:16)
    at ...)'

This requires to add a specific check whether info is null before calling node-input-validator constructor which seems more of a crutch than a solution.

Could null inputs be treated as empty object to avoid the error above? Thanks.

P.S. node-input-validator version is 4.3.3

Valmal2020 avatar Jan 21 '22 11:01 Valmal2020

@bitnbytesio @Valmal2020 Fixed the issue and raised a pr. Kindly review and merge Pr link : https://github.com/bitnbytesio/node-input-validator/pull/82

Kavinkumar07 avatar Nov 26 '22 10:11 Kavinkumar07

Thanks @Kavinkumar07

fix will be released under v4.6

bitnbytesio avatar Dec 02 '22 07:12 bitnbytesio