Add validate functions to Event namespace.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { EventID } from "./id";
|
||||
import { Sign } from "./sign";
|
||||
import { Validate } from "./validate";
|
||||
|
||||
export const Event = {
|
||||
...EventID,
|
||||
...Sign,
|
||||
...Validate,
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, expect, test } from "vitest";
|
||||
|
||||
import type { EventData } from "./types";
|
||||
import { testEvent, testPK } from "./util.test";
|
||||
import { EventValidation } from "./validate";
|
||||
import { Validate } from "./validate";
|
||||
|
||||
interface ValidateEventTestCase {
|
||||
name: string;
|
||||
@@ -114,9 +114,7 @@ const structureTestCases: ValidateEventTestCase[] = [
|
||||
|
||||
describe("EventValidation.validateStructure", () => {
|
||||
test.each(structureTestCases)("$name", ({ event, expectedError }) => {
|
||||
expect(() => EventValidation.validateStructure(event)).toThrow(
|
||||
expectedError,
|
||||
);
|
||||
expect(() => Validate.validateStructure(event)).toThrow(expectedError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -126,7 +124,7 @@ describe("EventValidation.validateID", () => {
|
||||
...testEvent,
|
||||
id: "7f661c2a3c1ed67dc959d6cd968d743d5e6e334313df44724bca939e2aa42c9e",
|
||||
};
|
||||
expect(() => EventValidation.validateID(event)).toThrow(
|
||||
expect(() => Validate.validateID(event)).toThrow(
|
||||
"does not match computed id",
|
||||
);
|
||||
});
|
||||
@@ -134,7 +132,7 @@ describe("EventValidation.validateID", () => {
|
||||
|
||||
describe("EventValidation.validateSignature", () => {
|
||||
test("accepts valid signature", () => {
|
||||
expect(() => EventValidation.validateSignature(testEvent)).not.toThrow();
|
||||
expect(() => Validate.validateSignature(testEvent)).not.toThrow();
|
||||
});
|
||||
|
||||
test("rejects invalid signature", () => {
|
||||
@@ -142,7 +140,7 @@ describe("EventValidation.validateSignature", () => {
|
||||
...testEvent,
|
||||
sig: "9e43cbcf7e828a21c53fa35371ee79bffbfd7a3063ae46fc05ec623dd3186667c57e3d006488015e19247df35eb41c61013e051aa87860e23fa5ffbd44120482",
|
||||
};
|
||||
expect(() => EventValidation.validateSignature(event)).toThrow(
|
||||
expect(() => Validate.validateSignature(event)).toThrow(
|
||||
"event signature is invalid",
|
||||
);
|
||||
});
|
||||
@@ -199,9 +197,7 @@ describe("EventValidation.validateSignature - malformed inputs", () => {
|
||||
"$name",
|
||||
({ id, sig, pubkey, expectedError }) => {
|
||||
const event: EventData = { ...testEvent, id, sig, pubkey };
|
||||
expect(() => EventValidation.validateSignature(event)).toThrow(
|
||||
expectedError,
|
||||
);
|
||||
expect(() => Validate.validateSignature(event)).toThrow(expectedError);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -220,6 +216,6 @@ describe("EventValidation.validate", () => {
|
||||
content: "valid event",
|
||||
sig: "668a715f1eb983172acf230d17bd283daedb2598adf8de4290bcc7eb0b802fdb60669d1e7d1104ac70393f4dbccd07e8abf897152af6ce6c0a75499874e27f14",
|
||||
};
|
||||
expect(() => EventValidation.validate(event)).not.toThrow();
|
||||
expect(() => Validate.validate(event)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -93,7 +93,7 @@ function validate(event: EventData): void {
|
||||
validateSignature(event);
|
||||
}
|
||||
|
||||
export const EventValidation = {
|
||||
export const Validate = {
|
||||
validate,
|
||||
validateStructure,
|
||||
validateID,
|
||||
|
||||
Reference in New Issue
Block a user