From 7e6117895c395509675b37dfc5d04fa8b5438215 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 31 Aug 2023 14:54:38 +0200 Subject: [PATCH] fix(AutoIncrementSimple): add error when there is no "field" key in field object --- src/autoIncrement.ts | 4 ++++ test/__snapshots__/basic.test.ts.snap | 2 ++ test/basic.test.ts | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/autoIncrement.ts b/src/autoIncrement.ts index dad376e..e4773c8 100644 --- a/src/autoIncrement.ts +++ b/src/autoIncrement.ts @@ -32,6 +32,10 @@ export function AutoIncrementSimple( // check if all fields are valid for (const field of fields) { + if (typeof field.field != 'string') { + throw new Error(`Got Field object, but did not contain a "field" key with "string" value, got: ${JSON.stringify(field)}`); + } + const schemaField = schema.path(field.field); // check if the field is even existing diff --git a/test/__snapshots__/basic.test.ts.snap b/test/__snapshots__/basic.test.ts.snap index 25d5c36..52a613a 100644 --- a/test/__snapshots__/basic.test.ts.snap +++ b/test/__snapshots__/basic.test.ts.snap @@ -6,6 +6,8 @@ exports[`Basic Suite AutoIncrementID should throw a error if "overwriteModelName exports[`Errors should Error if no field is given to AutoIncrementSimple 1`] = `"Options with at least one field are required!"`; +exports[`Errors should Error if no field is given to AutoIncrementSimple 2`] = `"Got Field object, but did not contain a \\"field\\" key with \\"string\\" value, got: {}"`; + exports[`Errors should Error if the schema path does not exist 1`] = `"Field \\"SomeNonExistingField\\" does not exists on the Schema!"`; exports[`Errors should Error if the schema path is not an number 1`] = `"Field \\"nonNumberField\\" is not a Number or BigInt!"`; diff --git a/test/basic.test.ts b/test/basic.test.ts index 7081353..c4cda07 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -540,4 +540,17 @@ describe('Errors', () => { }); expect(() => schema.plugin(AutoIncrementSimple, [])).toThrowErrorMatchingSnapshot(); }); + + it('should Error if no field is given to AutoIncrementSimple', () => { + const schema = new mongoose.Schema({ + nonNumberField: String, + }); + expect(() => + schema.plugin( + AutoIncrementSimple, + // @ts-expect-error TEST + {} + ) + ).toThrowErrorMatchingSnapshot(); + }); });