Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/__test__/streams/appendToStream-errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @jest-environment ./src/__test__/utils/enableVersionCheck.ts */

import {
binaryTestEvents,
createTestNode,
jsonTestEvents,
matchServerVersion,
Expand All @@ -16,6 +17,7 @@ import {
AccessDeniedError,
DeadlineExceededError,
} from "@eventstore/db-client";
import { IsNotUUIDError } from "../../utils";

describe("appendToStream - errors", () => {
const node = createTestNode();
Expand Down Expand Up @@ -143,5 +145,25 @@ describe("appendToStream - errors", () => {
}
}
});

optionalTest(supported)("InvalidId", async () => {
const STREAM_NAME = `${prefix}_invalid_id`;

// json event
await expect(
client.appendToStream(STREAM_NAME, {
...jsonTestEvents(1)[0],
id: "an_invalid_id_example_1",
})
).rejects.toThrowError(IsNotUUIDError);

// binary event
await expect(
client.appendToStream(STREAM_NAME, {
...binaryTestEvents(1)[0],
id: "an_invalid_id_example_2",
})
).rejects.toThrowError(IsNotUUIDError);
});
});
});
3 changes: 3 additions & 0 deletions src/streams/appendToStream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
EventData,
EventType,
} from "../../types";
import { validateEventIds } from "../../utils";

import { append } from "./append";
import { batchAppend } from "./batchAppend";
Expand Down Expand Up @@ -56,6 +57,8 @@ Client.prototype.appendToStream = async function <
): Promise<AppendResult> {
const events = Array.isArray(event) ? event : [event];

validateEventIds(events);

if (
!baseOptions.credentials &&
(await this.supports(StreamsService.batchAppend))
Expand Down
5 changes: 5 additions & 0 deletions src/utils/IsNotUUIDError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class IsNotUUIDError extends Error {
constructor(id: string) {
super(`Id ${id} is not a valid UUID`);
}
}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export * from "./grpcStreamIdentifier";
export * from "./grpcUUID";
export * from "./utilityTypes";
export * from "./isClientCancellationError";
export * from "./IsNotUUIDError";
export * from "./validateEventIds";
10 changes: 10 additions & 0 deletions src/utils/validateEventIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { validate as validateUUID } from "uuid";

import type { EventData, EventType } from "../types";
import { IsNotUUIDError } from "./IsNotUUIDError";

export const validateEventIds = (events: EventData<EventType>[]): void => {
events.forEach((event) => {
if (!validateUUID(event.id)) throw new IsNotUUIDError(event.id);
});
};
Comment thread
w1am marked this conversation as resolved.