Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for mirror name in UI #649

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions ui/app/mirrors/create/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
import { QRepConfig, QRepWriteType } from '@/grpc_generated/flow';
import { Dispatch, SetStateAction } from 'react';
import { CDCConfig, TableMapRow } from '../../dto/MirrorsDTO';
import { cdcSchema, qrepSchema, tableMappingSchema } from './schema';
import {
cdcSchema,
flowNameSchema,
qrepSchema,
tableMappingSchema,
} from './schema';

const validateCDCFields = (
tableMapping: TableMapRow[],
Expand Down Expand Up @@ -85,10 +90,13 @@ export const handleCreateCDC = async (
setLoading: Dispatch<SetStateAction<boolean>>,
route: RouteCallback
) => {
if (!flowJobName) {
setMsg({ ok: false, msg: 'Mirror name is required' });
const flowNameValid = flowNameSchema.safeParse(flowJobName);
if (!flowNameValid.success) {
const flowNameErr = flowNameValid.error.issues[0].message;
setMsg({ ok: false, msg: flowNameErr });
return;
}

const isValid = validateCDCFields(rows, setMsg, config);
if (!isValid) return;
const tableNameMapping = reformattedTableMapping(rows);
Expand Down Expand Up @@ -125,10 +133,13 @@ export const handleCreateQRep = async (
route: RouteCallback,
xmin?: boolean
) => {
if (!flowJobName) {
setMsg({ ok: false, msg: 'Mirror name is required' });
const flowNameValid = flowNameSchema.safeParse(flowJobName);
if (!flowNameValid.success) {
const flowNameErr = flowNameValid.error.issues[0].message;
setMsg({ ok: false, msg: flowNameErr });
return;
}

if (xmin == true) {
config.watermarkColumn = 'xmin';
config.query = `SELECT * FROM ${config.watermarkTable} WHERE xmin::text::bigint BETWEEN {{.start}} AND {{.end}}`;
Expand Down
10 changes: 10 additions & 0 deletions ui/app/mirrors/create/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import * as z from 'zod';

export const flowNameSchema = z
.string({
invalid_type_error: 'Mirror name is invalid.',
required_error: 'Mirror name is required.',
})
.min(1, { message: 'Mirror name cannot be empty.' })
.regex(/^[\w]*$/, {
message: 'Mirror name must contain only letters, numbers and underscores',
});

export const tableMappingSchema = z
.array(
z.object({
Expand Down
Loading