EditHookPanel.constants.ts75 lines · main
| 1 | import { getKeyValueFieldArrayValidationIssues } from 'ui-patterns/form/KeyValueFieldArray/validation' |
| 2 | import { z } from 'zod' |
| 3 | |
| 4 | import { httpEndpointUrlSchema } from '@/lib/validation/http-url' |
| 5 | |
| 6 | const httpRequestSchema = z.object({ |
| 7 | function_type: z.literal('http_request'), |
| 8 | http_url: httpEndpointUrlSchema({ |
| 9 | requiredMessage: 'Please provide a URL', |
| 10 | invalidMessage: 'Please provide a valid URL', |
| 11 | prefixMessage: 'Please prefix your URL with http:// or https://', |
| 12 | }), |
| 13 | }) |
| 14 | |
| 15 | const brivenFunctionSchema = z.object({ |
| 16 | function_type: z.literal('briven_function'), |
| 17 | http_url: z |
| 18 | .string() |
| 19 | .min(1, 'Please select an edge function') |
| 20 | .refine((val) => !val.includes('undefined'), 'No edge functions available for selection'), |
| 21 | }) |
| 22 | |
| 23 | const httpHeadersSchema = z.array( |
| 24 | z.object({ id: z.string(), name: z.string().trim(), value: z.string().trim() }) |
| 25 | ) |
| 26 | |
| 27 | const httpParametersSchema = z.array( |
| 28 | z.object({ id: z.string(), name: z.string().trim(), value: z.string().trim() }) |
| 29 | ) |
| 30 | |
| 31 | const addKeyValueIssues = ( |
| 32 | rows: z.infer<typeof httpHeadersSchema> | z.infer<typeof httpParametersSchema>, |
| 33 | ctx: z.RefinementCtx, |
| 34 | pathPrefix: 'httpHeaders' | 'httpParameters' |
| 35 | ) => { |
| 36 | const isHeaderField = pathPrefix === 'httpHeaders' |
| 37 | |
| 38 | getKeyValueFieldArrayValidationIssues({ |
| 39 | rows, |
| 40 | keyFieldName: 'name', |
| 41 | valueFieldName: 'value', |
| 42 | keyRequiredMessage: isHeaderField ? 'Header name is required' : 'Parameter name is required', |
| 43 | valueRequiredMessage: isHeaderField |
| 44 | ? 'Header value is required' |
| 45 | : 'Parameter value is required', |
| 46 | }).forEach((issue) => { |
| 47 | ctx.addIssue({ |
| 48 | code: z.ZodIssueCode.custom, |
| 49 | message: issue.message, |
| 50 | path: [pathPrefix, ...issue.path], |
| 51 | }) |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | export const FormSchema = z |
| 56 | .object({ |
| 57 | name: z.string().min(1, 'Please provide a name for your webhook'), |
| 58 | table_id: z.string().min(1, 'Please select a table'), |
| 59 | http_method: z.enum(['GET', 'POST']), |
| 60 | timeout_ms: z.coerce |
| 61 | .number() |
| 62 | .int() |
| 63 | .gte(1000, 'Timeout should be at least 1000ms') |
| 64 | .lte(10000, 'Timeout should not exceed 10,000ms'), |
| 65 | events: z.array(z.string()).min(1, 'Please select at least one event'), |
| 66 | httpHeaders: httpHeadersSchema, |
| 67 | httpParameters: httpParametersSchema, |
| 68 | }) |
| 69 | .and(z.discriminatedUnion('function_type', [httpRequestSchema, brivenFunctionSchema])) |
| 70 | .superRefine((data, ctx) => { |
| 71 | addKeyValueIssues(data.httpHeaders, ctx, 'httpHeaders') |
| 72 | addKeyValueIssues(data.httpParameters, ctx, 'httpParameters') |
| 73 | }) |
| 74 | |
| 75 | export type WebhookFormValues = z.infer<typeof FormSchema> |