Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: pass options object instead of boolean true to isJSON validator#18220

Open
mixelburg wants to merge 4 commits into
sequelize:mainsequelize/sequelize:mainfrom
mixelburg:fix/isJSON-validationmixelburg/sequelize:fix/isJSON-validationCopy head branch name to clipboard
Open

fix: pass options object instead of boolean true to isJSON validator#18220
mixelburg wants to merge 4 commits into
sequelize:mainsequelize/sequelize:mainfrom
mixelburg:fix/isJSON-validationmixelburg/sequelize:fix/isJSON-validationCopy head branch name to clipboard

Conversation

@mixelburg
Copy link
Copy Markdown

@mixelburg mixelburg commented Apr 30, 2026

Summary

Fixes a regression where validate: { isJSON: true } fails with validator.js v13+.

The validator.isJSON(str, options) function now expects options to be an object, not a boolean. When a model sets isJSON: true, Sequelize extracted true as the validator argument and called validator.isJSON(value, true), which throws because validator.js v13+ treats true as an invalid options argument.

Changes

  • Added special handling in _extractValidatorArgs() for isJSON: when the validator arg is boolean true, pass {} (empty options object) instead.

Test plan

  • The fix is minimal (3 lines added) and follows the existing pattern for other special-case validators (e.g., isImmutable, isLocalizedValidator).

Related

Summary by CodeRabbit

  • Bug Fixes
    • JSON validator configured as true now accepts valid JSON objects/arrays, rejects invalid JSON with a clear validation error, and avoids a prior TypeError regression.
  • Tests
    • Added/updated unit tests to cover the isJSON validator behavior for valid and invalid inputs.

validator.js v13+ expects isJSON(str, options) where options is an
object. When a model defines `validate: { isJSON: true }`, Sequelize
previously passed `true` directly to the validator function, causing
a TypeError. Now it passes `{}` (empty options object) instead.

Fixes sequelize#18141
@mixelburg mixelburg requested a review from a team as a code owner April 30, 2026 22:29
@mixelburg mixelburg requested review from ephys and sdepold April 30, 2026 22:29
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 30, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 07d74cd0-7dc1-43ec-a6e6-05df04debdd0

📥 Commits

Reviewing files that changed from the base of the PR and between 8d3954f and eee03da.

📒 Files selected for processing (1)
  • packages/core/test/unit/instance-validator.test.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/test/unit/instance-validator.test.js

📝 Walkthrough

Walkthrough

Convert isJSON: true validator args to an empty options object ({}) in the instance validator and add unit/regression tests verifying JSON acceptance/rejection and that the boolean-config regression no longer throws.

Changes

isJSON Validator Compatibility

Layer / File(s) Summary
InstanceValidator arg extraction
packages/core/src/instance-validator.js
Special-case _extractValidatorArgs to convert boolean true for isJSON validators into [{}] so validator.js v13+ receives an options object.
Unit & regression tests
packages/core/test/unit/instance-validator.test.js, packages/core/test/unit/model/validation.test.js
Add tests: alias SequelizeValidationError as ValidationError, new isJSON regression suite with model-level tests for valid/invalid JSON and regression ensuring no TypeError when isJSON: true; extend instance validator checks with isJSON cases.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I found a true that caused a fright,
Wrapped it gently so JSON sleeps tight.
Braces empty, errors cease to bite,
Now validations hop into the light. 🥕✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: converting boolean true to an options object for the isJSON validator to maintain compatibility.
Linked Issues check ✅ Passed The pull request fully addresses the requirement from issue #18141 by modifying instance-validator.js to pass an empty options object {} instead of boolean true to isJSON validators.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the isJSON validator compatibility issue; no unrelated modifications were introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Member

@ephys ephys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR!

validatorArgs = [];
} else if (validatorType === 'isJSON' && validatorArgs === true) {
// validator.js v13+ expects an options object for isJSON, not a boolean
validatorArgs = [{}];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be an empty array or does it need an object?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! Yes, empty arrays ([]) are accepted as valid JSON by validator.js v13+ when using the default {} options object. Both objects and arrays pass validation; primitives (strings, numbers, null) are rejected unless allow_primitives: true is set explicitly in the options. I've added a comment in the code to make this intent explicit. Addressed in commit abd6902.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! It needs to be an object — the options bag for validator.js v13+ isJSON(). Passing {} uses validator.js defaults, which accepts both JSON objects ({}) and arrays ([]) as valid JSON. This preserves the same behavior as the old boolean true — the only change is the argument shape validator.js v13+ now requires.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add regression tests in our unit test suites :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Added regression tests in commit abd6902:

  • Added isJSON to the existing validator checks table in packages/core/test/unit/model/validation.test.js (fail: invalid string, pass: JSON object string)
  • Added a dedicated isJSON validator regression describe block in packages/core/test/unit/instance-validator.test.js covering: valid JSON object, empty array ([]), non-empty array ([1,2,3]), invalid JSON string rejection, and a specific check that the validator.js v13+ TypeError regression does not recur.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Added regression tests for isJSON in packages/core/test/unit/instance-validator.test.js covering JSON objects, arrays, non-empty arrays, and invalid JSON. Also pushed a follow-up commit to fix lint warnings (import/newline-after-import, prefer-arrow-callback, prettier).

- Add comment to _extractValidatorArgs explaining that empty arrays ([])
  are accepted as valid JSON when using isJSON: true (validator.js default
  behavior with {} options object — both objects and arrays pass, primitives
  are rejected unless allow_primitives is set)
- Add isJSON to the existing checks table in validation.test.js
- Add explicit isJSON regression tests in instance-validator.test.js
  covering: JSON objects, empty arrays, non-empty arrays, invalid strings,
  and the TypeError regression for validator.js v13+

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/test/unit/instance-validator.test.js`:
- Around line 177-183: The two tests for the isJSON validator ("accepts a valid
JSON object string when isJSON is true" and "does not throw a TypeError when
isJSON is set to true (regression for validator.js v13+)") currently only assert
absence of TypeError; change them to assert successful validation instead by
ensuring the promise resolves (e.g., await
expect(instance.validate()).to.be.fulfilled or simply await
instance.validate()). Update the assertions for instances created via
JsonUser.build({ data: ... }) so they explicitly assert resolution rather than
merely asserting non-TypeError rejections.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ee2926dd-66b5-4e19-94cf-330dcc66cd39

📥 Commits

Reviewing files that changed from the base of the PR and between b2f21bb and abd6902.

📒 Files selected for processing (3)
  • packages/core/src/instance-validator.js
  • packages/core/test/unit/instance-validator.test.js
  • packages/core/test/unit/model/validation.test.js

Comment thread packages/core/test/unit/instance-validator.test.js Outdated
mixelburg added 2 commits May 22, 2026 12:10
Use 'not.to.be.rejected' instead of 'not.to.be.rejectedWith(Error, /TypeError/)'
to assert validation actually passes rather than only rejecting non-TypeError errors.
- Add blank line after last require (import/newline-after-import)
- Convert function expressions to arrow callbacks (prefer-arrow-callback)
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 validation

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.