-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isJSON rejects"0"
#2183
Comments
Hello avandekleut, I can confirm that From what I can see, the validator already uses validator.js/src/lib/isJSON.js Line 18 in 43803c0
Only question here is, if that was maybe done intentionally, to only allow strings in "expected JSON" formatting? (i.e. anything in curly brackets?) I guess we should also double-check with the RFC on this as well: |
The RFC states
By this logic, we should be able to delegate validation entirely to |
That would cause more to be accepted than people expect now, so maybe that's for the next major release? And in the meantime we can add a new option to allow non-object values? Like the |
To adhere to semantic versioning, I assume the following behaviour would have to be preserved:
The following behaviour is not implemented, but could be implemented without breaking the default behaviour:
Since the default is Here is my implementation of that change, with comments: import assertString from './util/assertString';
import merge from './util/merge';
const default_json_options = {
allow_primitives: false,
};
export default function isJSON(str, options) {
assertString(str);
/* this doesn't need to be in the try block */
options = merge(options, default_json_options);
try {
/* JSON.parse should succeed if str is a valid stringified JSON object */
const obj = JSON.parse(str);
if (allow_primitives) {
/* by default, primitives are allowed in json */
return true;
}
/* original behaviour is to only validate arrays and objects by default */
/* replaced !!obj with obj !== null which is more readable */
return (obj !== null && typeof obj === 'object');
} catch (e) { /* ignore */ }
return false;
} |
Describe the bug
The function
isJSON
rejects valid json.Examples
You can also use
fast-check
to generate examples for testing:Additional context
Just wondering why we don't do the following?
The text was updated successfully, but these errors were encountered: