From 4f25be7adb3bf2f290ab041e0eb1672b9acab605 Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Tue, 14 Nov 2023 16:48:28 -0500 Subject: [PATCH] Add IP validation (#223) --- client/validate.ts | 30 +++++++++++++++--------------- generator/schema/zod.ts | 3 +++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/client/validate.ts b/client/validate.ts index f5268a1..7573be6 100644 --- a/client/validate.ts +++ b/client/validate.ts @@ -126,9 +126,9 @@ export const AddressLot = z.preprocess( export const AddressLotBlock = z.preprocess( processResponseBody, z.object({ - firstAddress: z.string(), + firstAddress: z.string().ip(), id: z.string().uuid(), - lastAddress: z.string(), + lastAddress: z.string().ip(), }) ); @@ -137,7 +137,7 @@ export const AddressLotBlock = z.preprocess( */ export const AddressLotBlockCreate = z.preprocess( processResponseBody, - z.object({ firstAddress: z.string(), lastAddress: z.string() }) + z.object({ firstAddress: z.string().ip(), lastAddress: z.string().ip() }) ); /** @@ -191,7 +191,7 @@ export const Baseboard = z.preprocess( export const BgpPeerConfig = z.preprocess( processResponseBody, z.object({ - addr: z.string(), + addr: z.string().ip(), bgpAnnounceSet: NameOrId, bgpConfig: NameOrId, interfaceName: z.string(), @@ -519,7 +519,7 @@ export const IpKind = z.preprocess( export const ExternalIp = z.preprocess( processResponseBody, - z.object({ ip: z.string(), kind: IpKind }) + z.object({ ip: z.string().ip(), kind: IpKind }) ); /** @@ -785,7 +785,7 @@ export const InstanceNetworkInterfaceCreate = z.preprocess( processResponseBody, z.object({ description: z.string(), - ip: z.string().optional(), + ip: z.string().ip().optional(), name: Name, subnetName: Name, vpcName: Name, @@ -859,7 +859,7 @@ export const InstanceNetworkInterface = z.preprocess( description: z.string(), id: z.string().uuid(), instanceId: z.string().uuid(), - ip: z.string(), + ip: z.string().ip(), mac: MacAddr, name: Name, primary: SafeBoolean, @@ -1045,7 +1045,7 @@ export const LoopbackAddress = z.preprocess( export const LoopbackAddressCreate = z.preprocess( processResponseBody, z.object({ - address: z.string(), + address: z.string().ip(), addressLot: NameOrId, anycast: SafeBoolean, mask: z.number().min(0).max(255), @@ -1243,7 +1243,7 @@ export const Route = z.preprocess( processResponseBody, z.object({ dst: IpNet, - gw: z.string(), + gw: z.string().ip(), vid: z.number().min(0).max(65535).optional(), }) ); @@ -1264,7 +1264,7 @@ export const RouteConfig = z.preprocess( export const RouteDestination = z.preprocess( processResponseBody, z.union([ - z.object({ type: z.enum(["ip"]), value: z.string() }), + z.object({ type: z.enum(["ip"]), value: z.string().ip() }), z.object({ type: z.enum(["ip_net"]), value: IpNet }), z.object({ type: z.enum(["vpc"]), value: Name }), z.object({ type: z.enum(["subnet"]), value: Name }), @@ -1277,7 +1277,7 @@ export const RouteDestination = z.preprocess( export const RouteTarget = z.preprocess( processResponseBody, z.union([ - z.object({ type: z.enum(["ip"]), value: z.string() }), + z.object({ type: z.enum(["ip"]), value: z.string().ip() }), z.object({ type: z.enum(["vpc"]), value: Name }), z.object({ type: z.enum(["subnet"]), value: Name }), z.object({ type: z.enum(["instance"]), value: Name }), @@ -1670,7 +1670,7 @@ export const SwitchPortApplySettings = z.preprocess( export const SwitchPortBgpPeerConfig = z.preprocess( processResponseBody, z.object({ - addr: z.string(), + addr: z.string().ip(), bgpAnnounceSetId: z.string().uuid(), bgpConfigId: z.string().uuid(), interfaceName: z.string(), @@ -1958,7 +1958,7 @@ export const VpcFirewallRuleHostFilter = z.preprocess( z.object({ type: z.enum(["vpc"]), value: Name }), z.object({ type: z.enum(["subnet"]), value: Name }), z.object({ type: z.enum(["instance"]), value: Name }), - z.object({ type: z.enum(["ip"]), value: z.string() }), + z.object({ type: z.enum(["ip"]), value: z.string().ip() }), z.object({ type: z.enum(["ip_net"]), value: IpNet }), ]) ); @@ -1997,7 +1997,7 @@ export const VpcFirewallRuleTarget = z.preprocess( z.object({ type: z.enum(["vpc"]), value: Name }), z.object({ type: z.enum(["subnet"]), value: Name }), z.object({ type: z.enum(["instance"]), value: Name }), - z.object({ type: z.enum(["ip"]), value: z.string() }), + z.object({ type: z.enum(["ip"]), value: z.string().ip() }), z.object({ type: z.enum(["ip_net"]), value: IpNet }), ]) ); @@ -3418,7 +3418,7 @@ export const NetworkingLoopbackAddressDeleteParams = z.preprocess( processResponseBody, z.object({ path: z.object({ - address: z.string(), + address: z.string().ip(), rackId: z.string().uuid(), subnetMask: z.number().min(0).max(255), switchLocation: Name, diff --git a/generator/schema/zod.ts b/generator/schema/zod.ts index 0e791dc..59de3f9 100644 --- a/generator/schema/zod.ts +++ b/generator/schema/zod.ts @@ -44,6 +44,9 @@ export const schemaToZod = makeSchemaGenerator({ if (schema.format === "uuid") { w0(".uuid()"); } + if (schema.format === "ip") { + w0(".ip()"); + } if ("minLength" in schema) { w0(`.min(${schema.minLength})`); }