From 6c59e1a59110b9666032e30e02980171502a3956 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 24 Oct 2025 13:30:36 -0400 Subject: [PATCH] Add validate functions to Event namespace. --- src/event.ts | 2 ++ src/validate.test.ts | 18 +++++++----------- src/validate.ts | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/event.ts b/src/event.ts index a2d675f..a29ba6b 100644 --- a/src/event.ts +++ b/src/event.ts @@ -1,7 +1,9 @@ import { EventID } from "./id"; import { Sign } from "./sign"; +import { Validate } from "./validate"; export const Event = { ...EventID, ...Sign, + ...Validate, }; diff --git a/src/validate.test.ts b/src/validate.test.ts index 4d52d55..6f24421 100644 --- a/src/validate.test.ts +++ b/src/validate.test.ts @@ -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(); }); }); diff --git a/src/validate.ts b/src/validate.ts index 8b69e4b..4b21eab 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -93,7 +93,7 @@ function validate(event: EventData): void { validateSignature(event); } -export const EventValidation = { +export const Validate = { validate, validateStructure, validateID,