-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): zod validation and vitest testing
- Loading branch information
Showing
8 changed files
with
992 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,8 @@ | ||
export type DisputeDetails = { | ||
title: string; | ||
description: string; | ||
question: string; | ||
type: QuestionType; | ||
answers: Answer[]; | ||
policyURI: string; | ||
attachment: Attachment; | ||
frontendUrl: string; | ||
arbitrableChainID: string; | ||
arbitrableAddress: `0x${string}`; | ||
arbitratorChainID: string; | ||
arbitratorAddress: `0x${string}`; | ||
category: string; | ||
lang: string; | ||
specification: string; | ||
aliases?: Alias[]; | ||
version: string; | ||
}; | ||
|
||
export enum QuestionType { | ||
Bool = "bool", | ||
Datetime = "datetime", | ||
MultipleSelect = "multiple-select", | ||
SingleSelect = "single-select", | ||
Uint = "uint", | ||
} | ||
|
||
export type Answer = { | ||
title: string; | ||
description: string; | ||
id: `0x${string}`; | ||
reserved: boolean; | ||
}; | ||
|
||
export type Alias = { | ||
name: string; | ||
address: `0x${string}` | `${string}.eth`; | ||
}; | ||
|
||
export type Attachment = { | ||
label: string; | ||
uri: string; | ||
}; | ||
import { z } from "zod"; | ||
import DisputeDetailsSchema, { AliasSchema, AnswerSchema, AttachmentSchema } from "./disputeDetailsSchema"; | ||
|
||
export { QuestionType } from "./disputeDetailsSchema"; | ||
export type DisputeDetails = z.infer<typeof DisputeDetailsSchema>; | ||
export type Answer = z.infer<typeof AnswerSchema>; | ||
export type Alias = z.infer<typeof AliasSchema>; | ||
export type Attachment = z.infer<typeof AttachmentSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions
14
kleros-sdk/dataMappings/test.ts → kleros-sdk/test/dataMappings.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { ethAddressSchema, ensNameSchema, ethAddressOrEnsNameSchema } from "dataMappings/utils/disputeDetailsSchema"; | ||
|
||
describe("Dispute Details Schema", () => { | ||
it("snapshot", () => { | ||
expect({ foo: "bar" }).toMatchSnapshot(); | ||
}); | ||
|
||
const validAddresses = [ | ||
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", // checksummed | ||
"0xD8DA6BF26964AF9D7EED9E03E53415D37AA96045", // uppercase | ||
"0xd8da6bf26964af9d7eed9e03e53415d37aa96045", // lowercase | ||
]; | ||
|
||
const invalidAddressesNoEns = [ | ||
"", | ||
"yo", | ||
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA960", | ||
"d8dA6BF26964aF9D7eEd9e03E53415D37aA960", | ||
"xd8dA6BF26964aF9D7eEd9e03E53415D37aA960", | ||
"0xd8Wa6bf26964af9d7eed9e03e53415d37aa96045", | ||
]; | ||
|
||
const validEnsNames = ["vitalik.eth", "vitalik.eth.eth"]; | ||
|
||
const invalidEnsNamesNoAddress = ["", "vitalik", "vitalik.ether", "vitalik.sol", "eth.vitalik"]; | ||
|
||
describe("ethAddressSchema", () => { | ||
it("Should accept a valid address", async () => { | ||
validAddresses.forEach((address) => { | ||
expect(() => ethAddressSchema.parse(address)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
it("Should refuse an invalid address", async () => { | ||
expect(() => ethAddressSchema.parse(undefined)).toThrowError("invalid_type"); | ||
|
||
const invalidAddress = "Provided address is invalid."; | ||
[...invalidAddressesNoEns, ...validEnsNames].forEach((address) => { | ||
expect(() => ethAddressSchema.parse(address)).toThrowError(invalidAddress); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("ensNameSchema", () => { | ||
it("Should accept a valid ENS name", async () => { | ||
validEnsNames.forEach((ensName) => { | ||
expect(() => ensNameSchema.parse(ensName)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
it("Should refuse an invalid ENS name", async () => { | ||
expect(() => ensNameSchema.parse(undefined)).toThrowError("invalid_type"); | ||
|
||
const invalidEns = "Provided ENS name is invalid."; | ||
[...invalidEnsNamesNoAddress, ...validAddresses].forEach((ensName) => { | ||
expect(() => ensNameSchema.parse(ensName)).toThrowError(invalidEns); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("ethAddressOrEnsNameSchema", () => { | ||
it("Should accept a valid address", async () => { | ||
validAddresses.forEach((address) => { | ||
expect(() => ethAddressOrEnsNameSchema.parse(address)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
it("Should accept a valid ENS name", async () => { | ||
validEnsNames.forEach((ensName) => { | ||
expect(() => ethAddressOrEnsNameSchema.parse(ensName)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
it("Should refuse an invalid address", async () => { | ||
expect(() => ethAddressOrEnsNameSchema.parse(undefined)).toThrowError("invalid_type"); | ||
|
||
invalidAddressesNoEns.forEach((address) => { | ||
// FIXME: the errorMap is not working, expecting "Provided address or ENS name is invalid."); | ||
expect(() => ethAddressOrEnsNameSchema.parse(address)).toThrowError("custom"); | ||
}); | ||
}); | ||
|
||
it("Should refuse an invalid ENS name", async () => { | ||
expect(() => ethAddressOrEnsNameSchema.parse(undefined)).toThrowError("invalid_type"); | ||
|
||
invalidEnsNamesNoAddress.forEach((ensName) => { | ||
// FIXME: the errorMap is not working, expecting "Provided address or ENS name is invalid."); | ||
expect(() => ethAddressOrEnsNameSchema.parse(ensName)).toThrowError("custom"); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("disputeDetailsSchema", () => { | ||
// TODO: add tests | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference types="vitest" /> | ||
|
||
// Configure Vitest (https://vitest.dev/config/) | ||
|
||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
/* for example, use global to avoid globals imports (describe, test, expect): */ | ||
// globals: true, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.