From e4301f2cf50de4e7c0052fbb403b3139e1e04d29 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 17 Dec 2024 15:25:24 +0300 Subject: [PATCH] add form fields validation --- .../components/verify-form/consts.ts | 48 +++++++++++++------ .../header/ui/bottomSide/BottomSide.tsx | 2 +- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/idea/frontend/src/features/code-verifier/components/verify-form/consts.ts b/idea/frontend/src/features/code-verifier/components/verify-form/consts.ts index f7464562a..7ae7aa8d4 100644 --- a/idea/frontend/src/features/code-verifier/components/verify-form/consts.ts +++ b/idea/frontend/src/features/code-verifier/components/verify-form/consts.ts @@ -38,19 +38,39 @@ const DEFAULT_VALUES = { [FIELD_NAME.BUILD_IDL]: false, }; -const SCHEMA = z.object({ - [FIELD_NAME.DOCKER_IMAGE_VERSION]: z.string(), - - [FIELD_NAME.CODE_ID]: z // TODO: is there any case to validate that code hash is existing in the CURRENT network? - .string() - .trim() - .refine((value) => isHex(value), { message: 'Value should be hex' }), - - [FIELD_NAME.REPO_LINK]: z.string().trim(), // TODO: url validation - [FIELD_NAME.PROJECT_ID_TYPE]: z.string(), - [FIELD_NAME.PROJECT_ID]: z.string().trim(), // TODO: name/path validation - [FIELD_NAME.NETWORK]: z.string(), - [FIELD_NAME.BUILD_IDL]: z.boolean(), -}); +const SEMVER_REGEX = /^\d+\.\d+\.\d+$/; +const GITHUB_REPO_URL_REGEX = /^(https?:\/\/)?(www\.)?github\.com\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+(\/)?$/; +const CARGO_TOML_PATH_REGEX = /^(?:\.\/)?(?:[^/]+\/)*Cargo\.toml$/; + +const SCHEMA = z + .object({ + [FIELD_NAME.DOCKER_IMAGE_VERSION]: z + .string() + .trim() + .refine((value) => SEMVER_REGEX.test(value), { message: 'Invalid version format' }), + + [FIELD_NAME.CODE_ID]: z + .string() + .trim() + .refine((value) => isHex(value, 256), { message: 'Invalid hex' }), + + [FIELD_NAME.REPO_LINK]: z + .string() + .trim() + .refine((value) => GITHUB_REPO_URL_REGEX.test(value), { message: 'Invalid GitHub repository URL' }), + + [FIELD_NAME.PROJECT_ID_TYPE]: z.string(), + [FIELD_NAME.PROJECT_ID]: z.string().trim().min(1), + [FIELD_NAME.NETWORK]: z.string(), + [FIELD_NAME.BUILD_IDL]: z.boolean(), + }) + .refine( + ({ projectIdType, projectId }) => + projectIdType === PROJECT_ID_TYPE.CARGO_TOML_PATH ? CARGO_TOML_PATH_REGEX.test(projectId) : true, + { + message: 'Invalid path to Cargo.toml', + path: [FIELD_NAME.PROJECT_ID], + }, + ); export { DEFAULT_VALUES, SCHEMA, NETWORK, FIELD_NAME, PROJECT_ID_TYPE, NETWORK_OPTIONS }; diff --git a/idea/frontend/src/widgets/header/ui/bottomSide/BottomSide.tsx b/idea/frontend/src/widgets/header/ui/bottomSide/BottomSide.tsx index 442d00110..f9366de9f 100644 --- a/idea/frontend/src/widgets/header/ui/bottomSide/BottomSide.tsx +++ b/idea/frontend/src/widgets/header/ui/bottomSide/BottomSide.tsx @@ -6,9 +6,9 @@ import uploadCodeSVG from '@/shared/assets/images/actions/uploadCode.svg?react'; import uploadFileSVG from '@/shared/assets/images/actions/uploadFile.svg?react'; import sendMessageSVG from '@/shared/assets/images/actions/send.svg?react'; import { OnboardingTooltip } from '@/shared/ui/onboardingTooltip'; +import { VerifyLink } from '@/features/code-verifier'; import styles from './BottomSide.module.scss'; -import { VerifyLink } from '@/features/code-verifier'; const BottomSide = () => { const { showModal } = useModal();