|
| 1 | +import { z, type ZodString } from "zod"; |
| 2 | + |
| 3 | +const NO_UNICODE_REGEX = /^[\x20-\x7E]*$/; |
| 4 | +export const NO_UNICODE_ERROR = "String cannot contain special characters or Unicode symbols"; |
| 5 | + |
| 6 | +const ALLOWED_USERNAME_CHARACTERS_REGEX = /^[a-zA-Z0-9._-]+$/; |
| 7 | +export const ALLOWED_USERNAME_CHARACTERS_ERROR = |
| 8 | + "Username can only contain letters, numbers, dots, hyphens, and underscores"; |
| 9 | + |
| 10 | +const ALLOWED_REGION_CHARACTERS_REGEX = /^[a-zA-Z0-9_-]+$/; |
| 11 | +export const ALLOWED_REGION_CHARACTERS_ERROR = "Region can only contain letters, numbers, hyphens, and underscores"; |
| 12 | + |
| 13 | +const ALLOWED_CLUSTER_NAME_CHARACTERS_REGEX = /^[a-zA-Z0-9_-]+$/; |
| 14 | +export const ALLOWED_CLUSTER_NAME_CHARACTERS_ERROR = |
| 15 | + "Cluster names can only contain ASCII letters, numbers, and hyphens."; |
| 16 | + |
| 17 | +const ALLOWED_PROJECT_NAME_CHARACTERS_REGEX = /^[a-zA-Z0-9\s()@&+:._',-]+$/; |
| 18 | +export const ALLOWED_PROJECT_NAME_CHARACTERS_ERROR = |
| 19 | + "Project names can't be longer than 64 characters and can only contain letters, numbers, spaces, and the following symbols: ( ) @ & + : . _ - ' ,"; |
| 20 | +export const CommonArgs = { |
| 21 | + string: (): ZodString => z.string().regex(NO_UNICODE_REGEX, NO_UNICODE_ERROR), |
| 22 | + |
| 23 | + objectId: (fieldName: string): z.ZodString => |
| 24 | + z |
| 25 | + .string() |
| 26 | + .min(1, `${fieldName} is required`) |
| 27 | + .length(24, `${fieldName} must be exactly 24 characters`) |
| 28 | + .regex(/^[0-9a-fA-F]+$/, `${fieldName} must contain only hexadecimal characters`), |
| 29 | +}; |
| 30 | + |
| 31 | +export const AtlasArgs = { |
| 32 | + projectId: (): z.ZodString => CommonArgs.objectId("projectId"), |
| 33 | + |
| 34 | + organizationId: (): z.ZodString => CommonArgs.objectId("organizationId"), |
| 35 | + |
| 36 | + clusterName: (): z.ZodString => |
| 37 | + z |
| 38 | + .string() |
| 39 | + .min(1, "Cluster name is required") |
| 40 | + .max(64, "Cluster name must be 64 characters or less") |
| 41 | + .regex(ALLOWED_CLUSTER_NAME_CHARACTERS_REGEX, ALLOWED_CLUSTER_NAME_CHARACTERS_ERROR), |
| 42 | + |
| 43 | + projectName: (): z.ZodString => |
| 44 | + z |
| 45 | + .string() |
| 46 | + .min(1, "Project name is required") |
| 47 | + .max(64, "Project name must be 64 characters or less") |
| 48 | + .regex(ALLOWED_PROJECT_NAME_CHARACTERS_REGEX, ALLOWED_PROJECT_NAME_CHARACTERS_ERROR), |
| 49 | + |
| 50 | + username: (): z.ZodString => |
| 51 | + z |
| 52 | + .string() |
| 53 | + .min(1, "Username is required") |
| 54 | + .max(100, "Username must be 100 characters or less") |
| 55 | + .regex(ALLOWED_USERNAME_CHARACTERS_REGEX, ALLOWED_USERNAME_CHARACTERS_ERROR), |
| 56 | + |
| 57 | + ipAddress: (): z.ZodString => z.string().ip({ version: "v4" }), |
| 58 | + |
| 59 | + cidrBlock: (): z.ZodString => z.string().cidr(), |
| 60 | + |
| 61 | + region: (): z.ZodString => |
| 62 | + z |
| 63 | + .string() |
| 64 | + .min(1, "Region is required") |
| 65 | + .max(50, "Region must be 50 characters or less") |
| 66 | + .regex(ALLOWED_REGION_CHARACTERS_REGEX, ALLOWED_REGION_CHARACTERS_ERROR), |
| 67 | + |
| 68 | + password: (): z.ZodString => |
| 69 | + z.string().min(1, "Password is required").max(100, "Password must be 100 characters or less"), |
| 70 | +}; |
0 commit comments