-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add zod validation to internal and external endpoints #612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ECWireless
wants to merge
6
commits into
main
Choose a base branch
from
feat/add-zod-validation-to-internal-and-external-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
05b82b0
temp: add dummy total token supply endpoint
ECWireless f2ad4d2
Fix: Add Zod Validation to Internal and External Endpoints (Issue #43…
Roaring30s be283e5
fix: remove redundant validation
ECWireless 9365cae
fix: remove unused files
ECWireless c72d62f
fix: address copilot comments
ECWireless 5496d80
fix: use AddressSchema rather than regex
ECWireless File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| const ChangeSchema = z.object({ | ||
| type: z.string(), | ||
| content: z.string(), | ||
| }); | ||
|
|
||
| export const ChangefeedReleaseSchema = z.object({ | ||
| title: z.string(), | ||
| description: z.string().nullable(), | ||
| isPublished: z.boolean(), | ||
| publishedAt: z.string(), | ||
| changes: z.array(ChangeSchema), | ||
| }); | ||
|
|
||
| export const ChangefeedResponseSchema = z.object({ | ||
| name: z.string(), | ||
| releases: z.object({ | ||
| edges: z.array( | ||
| z.object({ | ||
| node: ChangefeedReleaseSchema, | ||
| }) | ||
| ), | ||
| }), | ||
| }); | ||
|
|
||
| export const ChangefeedGraphQLResultSchema = z.object({ | ||
| data: z.object({ | ||
| projectBySlugs: z.unknown(), | ||
| }), | ||
| }); |
This file contains hidden or 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,84 @@ | ||
| import { isAddress } from "viem"; | ||
| import { z } from "zod"; | ||
|
|
||
| /** | ||
| * Common schemas used across multiple API endpoints | ||
| */ | ||
|
|
||
| /** | ||
| * Validates Ethereum addresses using viem. | ||
| * Accepts valid lowercase/uppercase addresses and valid EIP-55 mixed-case addresses. | ||
| */ | ||
| export const AddressSchema = z.string().refine(isAddress, { | ||
| message: "Invalid Ethereum address", | ||
| }); | ||
|
|
||
| /** | ||
| * Schema for account balance API response | ||
| */ | ||
| export const AccountBalanceSchema = z.object({ | ||
| balance: z.string(), | ||
| allowance: z.string(), | ||
| }); | ||
|
|
||
| /** | ||
| * Validates a numeric string (for BigNumber values) | ||
| */ | ||
| export const NumericStringSchema = z.string().regex(/^\d+$/, { | ||
| message: "Must be a numeric string", | ||
| }); | ||
|
|
||
| /** | ||
| * Validates optional query parameters | ||
| */ | ||
| export const OptionalStringSchema = z.string().optional(); | ||
|
|
||
| /** | ||
| * Validates a region string (non-empty) | ||
| */ | ||
| export const RegionSchema = z.string().min(1, "Region cannot be empty"); | ||
|
|
||
| /** | ||
| * Schema for a single region object | ||
| */ | ||
| export const RegionObjectSchema = z.object({ | ||
| id: z.string(), | ||
| name: z.string(), | ||
| type: z.enum(["transcoding", "ai"]), | ||
| }); | ||
|
|
||
| /** | ||
| * Schema for regions API response | ||
| */ | ||
| export const RegionsSchema = z.object({ | ||
| regions: z.array(RegionObjectSchema), | ||
| }); | ||
|
|
||
| /** | ||
| * Schema for web URLs with an explicit http/https protocol requirement. | ||
| */ | ||
| export const WebUrlSchema = z.string().refine( | ||
| (val) => { | ||
| try { | ||
| const parsedUrl = new URL(val); | ||
| return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:"; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }, | ||
| { message: "Invalid URL format. Must use http or https." } | ||
| ); | ||
ECWireless marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Standard Twitter/X handle check: alphanumeric + underscore, max 15 chars (excludes @) | ||
| */ | ||
| export const TwitterHandleSchema = z | ||
| .string() | ||
| .regex(/^[A-Za-z0-9_]{1,15}$/, "Invalid Twitter handle"); | ||
|
|
||
| /** | ||
| * Standard GitHub handle check: alphanumeric + hyphens, max 39 chars | ||
| */ | ||
| export const GithubHandleSchema = z | ||
| .string() | ||
| .regex(/^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i, "Invalid GitHub handle"); | ||
This file contains hidden or 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,25 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| const ContractLinkSchema = z.object({ | ||
| name: z.string(), | ||
| address: z.string(), | ||
| link: z.string(), | ||
| }); | ||
|
|
||
| export const ContractInfoSchema = z.object({ | ||
| Controller: ContractLinkSchema.nullable(), | ||
| L1Migrator: ContractLinkSchema.nullable(), | ||
| L2Migrator: ContractLinkSchema.nullable(), | ||
| PollCreator: ContractLinkSchema.nullable(), | ||
| BondingManager: ContractLinkSchema.nullable(), | ||
| LivepeerToken: ContractLinkSchema.nullable(), | ||
| LivepeerTokenFaucet: ContractLinkSchema.nullable(), | ||
| MerkleSnapshot: ContractLinkSchema.nullable(), | ||
| Minter: ContractLinkSchema.nullable(), | ||
| RoundsManager: ContractLinkSchema.nullable(), | ||
| ServiceRegistry: ContractLinkSchema.nullable(), | ||
| TicketBroker: ContractLinkSchema.nullable(), | ||
| LivepeerGovernor: ContractLinkSchema.nullable(), | ||
| Treasury: ContractLinkSchema.nullable(), | ||
| BondingVotes: ContractLinkSchema.nullable(), | ||
| }); |
This file contains hidden or 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,9 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| export const CurrentRoundInfoSchema = z.object({ | ||
| id: z.number(), | ||
| startBlock: z.number(), | ||
| initialized: z.boolean(), | ||
| currentL1Block: z.number(), | ||
| currentL2Block: z.number(), | ||
| }); |
This file contains hidden or 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,79 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { | ||
| AddressSchema, | ||
| GithubHandleSchema, | ||
| TwitterHandleSchema, | ||
| WebUrlSchema, | ||
| } from "./common"; | ||
|
|
||
| /** | ||
| * Schema for ENS identity data | ||
| */ | ||
| export const EnsIdentitySchema = z.object({ | ||
| id: z.string(), | ||
| idShort: z.string(), | ||
| avatar: z.string().nullable().optional(), | ||
| name: z.string().nullable().optional(), | ||
| // Strict validation that falls back to null if invalid | ||
| url: WebUrlSchema.nullable().optional().catch(null), | ||
| twitter: TwitterHandleSchema.nullable().optional().catch(null), | ||
| github: GithubHandleSchema.nullable().optional().catch(null), | ||
| description: z.string().nullable().optional(), | ||
| isLoading: z.boolean().optional(), | ||
| }); | ||
|
|
||
| /** | ||
| * Blacklist of addresses that should be rejected | ||
| */ | ||
| const ENS_BLACKLIST = ["0xcb69ffc06d3c218472c50ee25f5a1d3ca9650c44"].map((a) => | ||
| a.toLowerCase() | ||
| ); | ||
|
|
||
| /** | ||
| * Blacklist of ENS names that should be rejected | ||
| */ | ||
| const ENS_NAME_BLACKLIST = ["salty-minning.eth"]; | ||
|
|
||
| /** | ||
| * Address schema with blacklist validation for ENS endpoints | ||
| */ | ||
| export const EnsAddressSchema = AddressSchema.refine( | ||
| (address) => !ENS_BLACKLIST.includes(address.toLowerCase()), | ||
| { | ||
| message: "Address is blacklisted", | ||
| } | ||
| ); | ||
|
|
||
| /** | ||
| * Schema for ENS name validation (with blacklist check) | ||
| */ | ||
| export const EnsNameSchema = z | ||
| .string() | ||
| .min(1, "ENS name cannot be empty") | ||
| .refine((name) => !ENS_NAME_BLACKLIST.includes(name), { | ||
| message: "ENS name is blacklisted", | ||
| }); | ||
|
|
||
| /** | ||
| * Schema for array of ENS identities | ||
| */ | ||
| export const EnsIdentityArraySchema = z.array(EnsIdentitySchema); | ||
|
|
||
| export const EnsAvatarResultSchema = z.string().nullable(); | ||
|
|
||
| /** | ||
| * Schema for ENS text record responses from provider | ||
| * Validates that text records are strings (or null if not set) | ||
| */ | ||
| export const EnsTextRecordSchema = z.string().nullable(); | ||
|
|
||
| /** | ||
| * Schema for ENS avatar response from provider | ||
| * Validates the avatar object structure returned by getAvatar() | ||
| */ | ||
| export const EnsAvatarProviderSchema = z | ||
| .object({ | ||
| url: z.string(), | ||
| }) | ||
| .nullable(); |
This file contains hidden or 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 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { AddressSchema, NumericStringSchema } from "./common"; | ||
|
|
||
| export const GenerateProofInputSchema = z.object({ | ||
| account: AddressSchema, | ||
| delegate: AddressSchema, | ||
| stake: NumericStringSchema, | ||
| fees: NumericStringSchema, | ||
| }); | ||
|
|
||
| export const GenerateProofOutputSchema = z.array(z.string()); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.