Skip to content

Commit

Permalink
add form fields validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Dec 17, 2024
1 parent 7bf1f9b commit 5d603e5
Showing 1 changed file with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 5d603e5

Please sign in to comment.