feat(isJson): allow any valid JSON value to pass#2690
feat(isJson): allow any valid JSON value to pass#2690WikiRik merged 4 commits intovalidatorjs:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2690 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2595 2597 +2
Branches 659 660 +1
=========================================
+ Hits 2595 2597 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds an opt-in isJSON mode that treats any value accepted by JSON.parse as valid JSON (per RFC 8259), while preserving the existing default behavior for backwards compatibility.
Changes:
- Add
allow_any_valueoption (defaultfalse) toisJSONto accept any valid JSON root value (e.g., strings/numbers) when enabled. - Add unit tests covering
allow_any_valuebehavior for primitives, strings, and numbers. - Update README to document the new option and updated defaults.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/validators.test.js | Adds a new test case for isJSON with { allow_any_value: true }. |
| src/lib/isJSON.js | Introduces allow_any_value option and updates validation flow to accept any parsed JSON value when enabled. |
| README.md | Documents the new allow_any_value option and updated defaults for isJSON. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Copilot comments made sense, I should have fixed them. |
WikiRik
left a comment
There was a problem hiding this comment.
LGTM, need to think about the follow up.
The current
isJSONimplementation incorrectly restricts valid JSON values. According to RFC 8259, a standalone string (e.g.,'"123-abc"') or a number is a valid JSON value, but the current logic only permits objects, arrays, or a subset of "primitives" (null/boolean).This creates a mismatch between JSON.parse() behavior and this validator, creating false expectations in modern developers. Probably the function was designed when JSON didn't allow primitive values as root elements (see RFC 4627 and some signs of this in the RFC 7158, where it mentions that "certain previous specifications of JSON constrain a JSON text to be an object or an array").
My approach was not to change the default behaviour and create a new option to enable compliance to RFC 8259, which is simply: JSON.parse don't throw.
Discussion points
As a futher follow up (or it can be done in this PR if agreed), it would be to deprecate allow_primitives. It is again deceiving as primitives are also strings and numbers, but they do not pass the validation. It is a pity that we can't easily change the default behaviour of this function, too. I believe that probably the most ergonomic implementation of the
isJsonvalidator is that:Maybe it is worth introducing a new function and deprecating IsJSON?
Checklist
fix #2183 - credits to @avandekleut for mentioning an initial proposal.