ProjectCreation.schema.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { z } from 'zod'
  2. import { DEFAULT_MINIMUM_PASSWORD_STRENGTH } from '@/lib/constants'
  3. export const FormSchema = z
  4. .object({
  5. organization: z.string({
  6. required_error: 'Please select an organization',
  7. }),
  8. projectName: z
  9. .string()
  10. .trim()
  11. .min(1, 'Please enter a project name.') // Required field check
  12. .min(3, 'Project name must be at least 3 characters long.') // Minimum length check
  13. .max(64, 'Project name must be no longer than 64 characters.'), // Maximum length check
  14. highAvailability: z.boolean(),
  15. postgresVersion: z.string({
  16. required_error: 'Please enter a Postgres version.',
  17. }),
  18. instanceType: z.string().optional(),
  19. dbRegion: z.string({
  20. required_error: 'Please select a region.',
  21. }),
  22. cloudProvider: z.string({
  23. required_error: 'Please select a cloud provider.',
  24. }),
  25. dbPass: z
  26. .string({ required_error: 'Please enter a database password.' })
  27. .min(1, 'Password is required.'),
  28. dbPassStrength: z
  29. .union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)])
  30. .default(0),
  31. dbPassStrengthMessage: z.string().default(''),
  32. dbPassStrengthWarning: z.string().default(''),
  33. instanceSize: z.string().optional(),
  34. githubRepositoryId: z.string().optional().default(''),
  35. githubInstallationId: z.number().optional(),
  36. githubRepositoryName: z.string().optional().default(''),
  37. dataApi: z.boolean(),
  38. dataApiDefaultPrivileges: z.boolean(),
  39. enableRlsEventTrigger: z.boolean(),
  40. postgresVersionSelection: z.string(),
  41. useOrioleDb: z.boolean(),
  42. })
  43. .superRefine(
  44. ({ dbPassStrength, dbPassStrengthWarning, highAvailability, cloudProvider }, ctx) => {
  45. if (dbPassStrength < DEFAULT_MINIMUM_PASSWORD_STRENGTH) {
  46. ctx.addIssue({
  47. code: z.ZodIssueCode.custom,
  48. path: ['dbPass'],
  49. message: dbPassStrengthWarning || 'Password not secure enough',
  50. })
  51. }
  52. if (highAvailability && cloudProvider !== 'AWS_K8S') {
  53. ctx.addIssue({
  54. code: z.ZodIssueCode.custom,
  55. path: ['cloudProvider'],
  56. message: 'High availability is only supported on AWS (Revamped)',
  57. })
  58. }
  59. }
  60. )
  61. export type CreateProjectForm = z.infer<typeof FormSchema>