Skip to content

feat(isJson): allow any valid JSON value to pass#2690

Open
relu91 wants to merge 1 commit intovalidatorjs:masterfrom
relu91:master
Open

feat(isJson): allow any valid JSON value to pass#2690
relu91 wants to merge 1 commit intovalidatorjs:masterfrom
relu91:master

Conversation

@relu91
Copy link

@relu91 relu91 commented Mar 18, 2026

The current isJSON implementation 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 isJson validator is that:

  • by default, pass all JSON values
  • With options, I can constrain the allowed values.

Maybe it is worth introducing a new function and deprecating IsJSON?

Checklist

  • PR contains only changes related; no stray files, etc.
  • README updated (where applicable)
  • Tests written (where applicable)
  • References provided in PR (where applicable)

fix #2183 - credits to @avandekleut for mentioning an initial proposal.

@codecov
Copy link

codecov bot commented Mar 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9fa1e3a) to head (09a164c).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isJSON rejects"0"

2 participants