You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
keywordObjectId throws instead of returning false, breaking anyOf / union validation #3685
AJV is supposed to try each branch and succeed if any passes. But because the keyword throws instead of returning false, AJV cannot gracefully fall through to the { "type": "null" } branch — the exception propagates up the call stack.
Steps to reproduce
Register keywordObjectId on an AJV instance with coerceTypes: true and useDefaults: true
Validate an object where refId is missing (so the default null is applied)
Expected: Validation passes — null matches the Type.Null() branch
Actual: Throws Error: invalid objectid for property "refId"
Root cause
With coerceTypes: true, AJV attempts to coerce null into a string when evaluating the first anyOf branch. The coerced value (empty string) reaches the objectid keyword, which calls new ObjectId(""), catches the error, and re-throws instead of returning false.
AJV keyword validators must return false (not throw) to signal failure, otherwise AJV cannot continue evaluating alternative branches.
Suggested fix
return function (value: string, obj: any) {
const { parentData, parentDataProperty } = obj
try {
parentData[parentDataProperty] = new ObjectId(value)
return true
} catch (error) {
- throw new Error(`invalid objectid for property "${parentDataProperty}"`)+ return false
}
}
This allows AJV to produce a standard validation error (referencing the keyword) rather than an uncaught exception, and correctly supports anyOf / oneOf unions with nullable objectid fields.
Activity
Huaian666 commentedon Jun 6, 2026
migrated our api last weekend. went smoother than expected honestly