Skip to content

Commit

Permalink
improve: enforce validation
Browse files Browse the repository at this point in the history
  • Loading branch information
james-a-morris committed Feb 12, 2024
1 parent d8d5603 commit c2264ce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/caching/Arweave/ArweaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class ArweaveClient {
* @param structValidator An optional struct validator to validate the retrieved value. If the value does not match the struct, null is returned.
* @returns The record if it exists, otherwise null
*/
async get<T>(transactionID: string, validator?: Struct<T>): Promise<T | null> {
async get<T>(transactionID: string, validator: Struct<T>): Promise<T | null> {
const rawData = await this.client.transactions.getData(transactionID, { decode: true, string: true });
if (!rawData) {
return null;
Expand All @@ -81,11 +81,12 @@ export class ArweaveClient {
if (data.status === 400) {
return null;
}
if (validator && !is(data, validator)) {
// If the validator does not match the retrieved value, return null and log a warning
if (!is(data, validator)) {
this.logger.warn("Retrieved value from Arweave does not match the expected type");
return null;
}
return data as T;
return data;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/arweaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("ArweaveClient", () => {
await mineBlock();
await mineBlock();

const retrievedValue = await client.get(txID!);
const retrievedValue = await client.get(txID!, object());
expect(retrievedValue).to.deep.equal(value);
});

Expand All @@ -78,14 +78,14 @@ describe("ArweaveClient", () => {
await mineBlock();
await mineBlock();

const retrievedValue = await client.get(txID!);
const retrievedValue = await client.get(txID!, object());

const expectedValue = { test: "value", bigNumber: "1000000000000000000" };
expect(retrievedValue).to.deep.equal(expectedValue);
});

it("should fail to get a non-existent record", async () => {
const retrievedValue = await client.get("non-existent");
const retrievedValue = await client.get("non-existent", object());
expect(retrievedValue).to.be.null;
});

Expand Down

0 comments on commit c2264ce

Please sign in to comment.