|
1 | | -import * as z from "zod"; |
2 | | - |
3 | | -export const formSchema = z.object({ |
4 | | - server: z.string().min(1), |
5 | | - targetJdkVersion: z.optional(z.string()), |
6 | | - debug: z.optional(z.boolean()), |
7 | | - bypassJavaModule: z.optional(z.boolean()), |
8 | | - shellClassName: z.string().optional(), |
9 | | - shellTool: z.string().min(1), |
10 | | - shellType: z.string().min(1), |
11 | | - urlPattern: z.optional(z.string()), |
12 | | - godzillaPass: z.optional(z.string()), |
13 | | - godzillaKey: z.optional(z.string()), |
14 | | - behinderPass: z.optional(z.string()), |
15 | | - antSwordPass: z.optional(z.string()), |
16 | | - commandParamName: z.optional(z.string()), |
17 | | - implementationClass: z.optional(z.string()), |
18 | | - headerName: z.optional(z.string()), |
19 | | - headerValue: z.optional(z.string()), |
20 | | - injectorClassName: z.optional(z.string()), |
21 | | - packingMethod: z.string().min(1), |
22 | | - shrink: z.optional(z.boolean()), |
23 | | - shellClassBase64: z.optional(z.string()), |
24 | | - encryptor: z.optional(z.string()), |
| 1 | +import { TFunction } from "i18next"; |
| 2 | +import { useCallback } from "react"; |
| 3 | +import { FieldErrors } from "react-hook-form"; |
| 4 | +import * as yup from "yup"; |
| 5 | +import { ShellToolType } from "./shell"; |
| 6 | + |
| 7 | +export const formSchema = yup.object({ |
| 8 | + server: yup.string().required().min(1), |
| 9 | + targetJdkVersion: yup.string().optional(), |
| 10 | + debug: yup.boolean().optional(), |
| 11 | + bypassJavaModule: yup.boolean().optional(), |
| 12 | + shellClassName: yup.string().optional(), |
| 13 | + shellTool: yup.string().required().min(1), |
| 14 | + shellType: yup.string().required().min(1), |
| 15 | + urlPattern: yup.string().optional(), |
| 16 | + godzillaPass: yup.string().optional(), |
| 17 | + godzillaKey: yup.string().optional(), |
| 18 | + behinderPass: yup.string().optional(), |
| 19 | + antSwordPass: yup.string().optional(), |
| 20 | + commandParamName: yup.string().optional(), |
| 21 | + implementationClass: yup.string().optional(), |
| 22 | + headerName: yup.string().optional(), |
| 23 | + headerValue: yup.string().optional(), |
| 24 | + injectorClassName: yup.string().optional(), |
| 25 | + packingMethod: yup.string().required().min(1), |
| 26 | + shrink: yup.boolean().optional(), |
| 27 | + shellClassBase64: yup.string().optional(), |
| 28 | + encryptor: yup.string().optional(), |
25 | 29 | }); |
26 | 30 |
|
27 | | -export type FormSchema = z.infer<typeof formSchema>; |
| 31 | +interface ValidationResult { |
| 32 | + values: FormSchema; |
| 33 | + errors: FieldErrors<FormSchema>; |
| 34 | +} |
| 35 | + |
| 36 | +const urlPatternIsNeeded = (shellType: string) => { |
| 37 | + return ( |
| 38 | + shellType.endsWith("Servlet") || |
| 39 | + shellType.endsWith("ControllerHandler") || |
| 40 | + shellType === "HandlerMethod" || |
| 41 | + shellType === "HandlerFunction" || |
| 42 | + shellType.endsWith("WebSocket") |
| 43 | + ); |
| 44 | +}; |
| 45 | + |
| 46 | +const isInvalidUrl = (urlPattern: string | undefined) => |
| 47 | + urlPattern === "/" || urlPattern === "/*" || !urlPattern?.startsWith("/") || !urlPattern; |
| 48 | + |
| 49 | +export const useYupValidationResolver = (validationSchema: yup.ObjectSchema<any>, t: TFunction) => |
| 50 | + useCallback( |
| 51 | + async (data: FormSchema): Promise<ValidationResult> => { |
| 52 | + try { |
| 53 | + const values = (await validationSchema.validate(data, { |
| 54 | + abortEarly: false, |
| 55 | + })) as FormSchema; |
| 56 | + |
| 57 | + const urlPattern: keyof FormSchema = "urlPattern"; |
| 58 | + const shellClassBase64: keyof FormSchema = "shellClassBase64"; |
| 59 | + const errors = {} as any; |
| 60 | + |
| 61 | + if (urlPatternIsNeeded(values?.shellType) && isInvalidUrl(values?.urlPattern)) { |
| 62 | + errors[urlPattern] = { |
| 63 | + type: "custom", |
| 64 | + message: t("tips.specificUrlPattern"), |
| 65 | + }; |
| 66 | + } |
| 67 | + if (values.shellTool === ShellToolType.Custom && !values.shellClassBase64) { |
| 68 | + errors[shellClassBase64] = { |
| 69 | + type: "custom", |
| 70 | + message: t("tips.customShellClass"), |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + return { |
| 75 | + values, |
| 76 | + errors, |
| 77 | + }; |
| 78 | + } catch (errors) { |
| 79 | + if (errors instanceof yup.ValidationError) { |
| 80 | + return { |
| 81 | + values: {} as FormSchema, |
| 82 | + errors: errors.inner.reduce( |
| 83 | + (allErrors, currentError) => ({ |
| 84 | + // biome-ignore lint/performance/noAccumulatingSpread: <explanation> |
| 85 | + ...allErrors, |
| 86 | + [currentError.path as keyof FormSchema]: { |
| 87 | + type: currentError.type ?? "validation", |
| 88 | + message: currentError.message, |
| 89 | + }, |
| 90 | + }), |
| 91 | + {}, |
| 92 | + ), |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + values: {} as FormSchema, |
| 98 | + errors: { |
| 99 | + server: { |
| 100 | + type: "unknown", |
| 101 | + message: "An unexpected validation error occurred", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }; |
| 105 | + } |
| 106 | + }, |
| 107 | + [validationSchema, t], |
| 108 | + ); |
| 109 | + |
| 110 | +export type FormSchema = yup.InferType<typeof formSchema>; |
0 commit comments