class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

isJSON doesn't validate json-encoded strings

Open alust opened this issue 6 years ago • 3 comments

> import {Validator} from "class-validator";
{}
> const validator = new Validator();
undefined
> let json_str = JSON.stringify('abc');
undefined
> validator.isJSON(json_str);
false

However json-encoded objects and arrays are validated correctly:

> json_str = JSON.stringify({});
'{}'
> validator.isJSON(json_str);
true
> json_str = JSON.stringify([]);
'[]'
> validator.isJSON(json_str);
true

alust avatar Dec 18 '18 05:12 alust

https://stackoverflow.com/questions/7487869/is-this-simple-string-considered-valid-json

Is string really valid JSON?

vlapo avatar Jun 14 '19 21:06 vlapo

Any update on this?

harsh-arora-by avatar Sep 23 '21 14:09 harsh-arora-by

@vlapo yes it is, the JSON specification states that a string is a valid Json https://datatracker.ietf.org/doc/html/rfc7159#section-3

overbit avatar Apr 26 '23 07:04 overbit