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