Skip to content

Commit

Permalink
bug: Apply Joi defaults after saving
Browse files Browse the repository at this point in the history
  • Loading branch information
tgandrews committed Feb 9, 2023
1 parent 8b9ed67 commit 15c9cfb
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 15c9cfb

Please sign in to comment.