fix: pass options object instead of boolean true to isJSON validator#18220
fix: pass options object instead of boolean true to isJSON validator#18220mixelburg wants to merge 4 commits intosequelize:mainsequelize/sequelize:mainfrom mixelburg:fix/isJSON-validationmixelburg/sequelize:fix/isJSON-validationCopy head branch name to clipboard
Conversation
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
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughConvert ChangesisJSON Validator Compatibility
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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
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. Comment |
| validatorArgs = []; | ||
| } else if (validatorType === 'isJSON' && validatorArgs === true) { | ||
| // validator.js v13+ expects an options object for isJSON, not a boolean | ||
| validatorArgs = [{}]; |
There was a problem hiding this comment.
Can it be an empty array or does it need an object?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Don't forget to add regression tests in our unit test suites :)
There was a problem hiding this comment.
Done! Added regression tests in commit abd6902:
- Added
isJSONto the existing validator checks table inpackages/core/test/unit/model/validation.test.js(fail: invalid string, pass: JSON object string) - Added a dedicated
isJSON validator regressiondescribe block inpackages/core/test/unit/instance-validator.test.jscovering: 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.
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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
📒 Files selected for processing (3)
packages/core/src/instance-validator.jspackages/core/test/unit/instance-validator.test.jspackages/core/test/unit/model/validation.test.js
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)
Summary
Fixes a regression where
validate: { isJSON: true }fails with validator.js v13+.The
validator.isJSON(str, options)function now expectsoptionsto be an object, not a boolean. When a model setsisJSON: true, Sequelize extractedtrueas the validator argument and calledvalidator.isJSON(value, true), which throws because validator.js v13+ treatstrueas an invalid options argument.Changes
_extractValidatorArgs()forisJSON: when the validator arg is booleantrue, pass{}(empty options object) instead.Test plan
isImmutable,isLocalizedValidator).Related
Summary by CodeRabbit
truenow accepts valid JSON objects/arrays, rejects invalid JSON with a clear validation error, and avoids a prior TypeError regression.isJSONvalidator behavior for valid and invalid inputs.