diff --git a/packages/mongodb/src/converters.ts b/packages/mongodb/src/converters.ts index ae878cbae..e38a677de 100644 --- a/packages/mongodb/src/converters.ts +++ b/packages/mongodb/src/converters.ts @@ -57,7 +57,7 @@ export const keywordObjectId = { parentData[parentDataProperty] = new ObjectId(value) return true } catch (error) { - throw new Error(`invalid objectid for property "${parentDataProperty}"`) + return false } } } diff --git a/packages/mongodb/test/converters.test.ts b/packages/mongodb/test/converters.test.ts index f8d965e8c..910ba58fa 100644 --- a/packages/mongodb/test/converters.test.ts +++ b/packages/mongodb/test/converters.test.ts @@ -89,7 +89,7 @@ describe('objectid keyword', () => { assert.equal(typeof data.otherId, 'string') }) - it('fails on invalid objectids', async () => { + it('fails validation on invalid objectids', async () => { const schema = { type: 'object', properties: { @@ -104,6 +104,28 @@ describe('objectid keyword', () => { } assert.equal(typeof data._id, 'string') - assert.throws(() => validate(data), /invalid objectid for property "_id"/) + assert.equal(validate(data), false) + assert.equal(validate.errors?.[0].keyword, 'objectid') + }) + + it('continues validating nullable unions when an objectid branch fails', async () => { + const nullableValidator = new Ajv({ coerceTypes: true, useDefaults: true }) + nullableValidator.addKeyword(keywordObjectId) + + const schema: any = { + type: 'object', + properties: { + refId: { + anyOf: [{ type: 'string', objectid: true }, { type: 'null' }], + default: null + } + }, + additionalProperties: false + } + const validate = nullableValidator.compile(schema) + const data: { refId?: ObjectId | null } = {} + + assert.equal(validate(data), true) + assert.equal(data.refId, null) }) })