Skip to content

Commit

Permalink
fix(AutoIncrementSimple): add error when there is no "field" key in f…
Browse files Browse the repository at this point in the history
…ield object
  • Loading branch information
hasezoey committed Sep 1, 2023
1 parent 493127a commit 7e61178
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/autoIncrement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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!"`;
13 changes: 13 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 7e61178

Please sign in to comment.