Skip to content

Commit

Permalink
Pod name validator
Browse files Browse the repository at this point in the history
  • Loading branch information
strzinek authored and jelly committed Aug 19, 2022
1 parent fc93a54 commit 8540cbe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/ContainerRenameModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import cockpit from 'cockpit';

import * as client from './client.js';
import * as utils from './util.js';
import { ErrorNotification } from './Notification.jsx';
import { useDialogs } from "dialogs.jsx";

Expand All @@ -24,7 +25,7 @@ const ContainerRenameModal = ({ container, version, updateContainerAfterEvent })
setName(value);
if (value === "") {
setNameError(_("Container name is required."));
} else if (/^[a-zA-Z0-9][a-zA-Z0-9_\\.-]*$/.test(value)) {
} else if (utils.is_valid_container_name(value)) {
setNameError(null);
} else {
setNameError(_("Invalid characters. Name can only contain letters, numbers, and certain punctuation (_ . -)."));
Expand Down
19 changes: 15 additions & 4 deletions src/PodCreateModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PublishPort } from './PublishPort.jsx';
import { DynamicListForm } from './DynamicListForm.jsx';
import { Volume } from './Volume.jsx';
import * as client from './client.js';
import * as utils from './util.js';
import cockpit from 'cockpit';
import { useDialogs } from "dialogs.jsx";

Expand All @@ -20,6 +21,7 @@ const systemOwner = "system";

export const PodCreateModal = ({ props, user }) => {
const [podName, setPodName] = useState(dockerNames.getRandomName());
const [nameError, setNameError] = useState(null);
const [publish, setPublish] = useState([]);
const [volumes, setVolumes] = useState([]);
const [owner, setOwner] = useState(props.systemServiceAvailable ? systemOwner : user);
Expand Down Expand Up @@ -77,18 +79,26 @@ export const PodCreateModal = ({ props, user }) => {
};

const onValueChanged = (key, value) => {
if (key === "podName")
if (key === "podName") {
setPodName(value);
// TODO: add name validation
} else if (utils.is_valid_container_name(value)) {
setNameError(null);
} else {
setNameError(_("Invalid characters. Name can only contain letters, numbers, and certain punctuation (_ . -)."));
}
};

const defaultBody = (
<Form>
<FormGroup fieldId='create-pod-dialog-name' label={_("Name")} className="ct-m-horizontal">
<FormGroup fieldId='create-pod-dialog-name' label={_("Name")} className="ct-m-horizontal"
validated={nameError ? "error" : "default"}
helperTextInvalid={nameError}>
<TextInput id='create-pod-dialog-name'
className="pod-name"
placeholder={_("Pod name")}
value={podName}
validated={nameError ? "error" : "default"}
aria-label={nameError}
onChange={value => onValueChanged('podName', value)} />
</FormGroup>
<FormGroup isInline hasNoPaddingTop fieldId='create-pod-dialog-owner' label={_("Owner")}>
Expand Down Expand Up @@ -132,7 +142,8 @@ export const PodCreateModal = ({ props, user }) => {
title={_("Create pod")}
footer={<>
{dialogError && <ErrorNotification errorMessage={dialogError} errorDetail={dialogErrorDetail} />}
<Button variant='primary' id="create-pod-create-btn" onClick={() => onCreateClicked()}>
<Button variant='primary' id="create-pod-create-btn" onClick={() => onCreateClicked()}
isDisabled={nameError}>
{_("Create")}
</Button>
<Button variant='link' className='btn-cancel' onClick={Dialogs.close}>
Expand Down
4 changes: 4 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ export function unquote_cmdline(text) {
export function image_name(image) {
return image.RepoTags ? image.RepoTags[0] : "<none>:<none>";
}

export function is_valid_container_name(name) {
return /^[a-zA-Z0-9][a-zA-Z0-9_\\.-]*$/.test(name);
}

0 comments on commit 8540cbe

Please sign in to comment.