feat(frontend): implemented Yup port validation

pull/75/head
Samuel Rowe 3 years ago
parent 63b9d11491
commit 31336c6f55

@ -13,6 +13,23 @@ const initialValues: IEditServiceForm = {
labels: [] labels: []
}; };
yup.addMethod<yup.StringSchema>(yup.string, "port", function (message) {
return this.test("test-port", message, function (value):
| boolean
| yup.ValidationError {
const { path, createError } = this;
if (value) {
const result = parseInt(value, 10);
if (isNaN(result) || result < 0 || result > 65535) {
return createError({ path, message });
}
}
return true;
});
});
export const validationSchema = yup.object({ export const validationSchema = yup.object({
serviceName: yup serviceName: yup
.string() .string()
@ -29,8 +46,12 @@ export const validationSchema = yup.object({
.required("Container name is required"), .required("Container name is required"),
ports: yup.array( ports: yup.array(
yup.object({ yup.object({
hostPort: yup.string().required("Host port is required"), hostPort: (yup.string().required("Host port is required") as any).port(
containerPort: yup.string(), "Host port should be an integer in the range 0-65535"
),
containerPort: (yup.string() as any).port(
"Container port should be an integer in the range 0-65535"
),
protocol: yup protocol: yup
.string() .string()
.oneOf(["tcp", "udp"], "Protocol should be tcp or udp") .oneOf(["tcp", "udp"], "Protocol should be tcp or udp")

Loading…
Cancel
Save