feat(isJson): allow any valid JSON value to pass#2690
Open
relu91 wants to merge 1 commit intovalidatorjs:masterfrom
Open
feat(isJson): allow any valid JSON value to pass#2690relu91 wants to merge 1 commit intovalidatorjs:masterfrom
relu91 wants to merge 1 commit 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:
|
rubiin
approved these changes
Mar 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.