Skip to content

Commit

Permalink
bug: Joi defaults not applied on save (#40)
Browse files Browse the repository at this point in the history
When saving an empty list then dynamodb drops them and so the result from create does not have them. We now validate the create result to ensure it matches the schema. This should have been as we need to guarantee the first item we save matches our expected schema.
  • Loading branch information
tgandrews committed Feb 9, 2023
1 parent 8b9ed67 commit 5664766
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,26 @@ describe("omanyd", () => {
id: savedThing.id,
});
});

it("should save and return empty arrays", async () => {
interface Thing {
id: string;
list: string[];
}
const ThingStore = Omanyd.define<Thing>({
name: "basicEmptyArray",
hashKey: "id",
schema: Joi.object({
id: Omanyd.types.id(),
list: Joi.array().items(Joi.string()).default([]),
}),
});

await Omanyd.createTables();

const savedThing = await ThingStore.create({ list: [] });
expect(savedThing.list).toEqual([]);
});
});

describe("range key", () => {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function define<T extends PlainObject>(options: Options) {
async create(obj: Omit<T, "id"> | T): Promise<T> {
const validated: T = await validator.validateAsync(obj);
const result = await t.create(validated);
return result as unknown as T;
const validatedResult = await validator.validateAsync(result);
return validatedResult as unknown as T;
},

async put(obj: T): Promise<T> {
Expand Down

0 comments on commit 5664766

Please sign in to comment.