Skip to content

Commit 242e49c

Browse files
authored
Merge pull request #2989 from devtron-labs/fix/project-validation
fix: project-validation
2 parents 6e0ba21 + 458bd2f commit 242e49c

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/components/project/ProjectList.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default class ProjectList extends Component<ProjectListProps, ProjectList
4848
},
4949
}
5050
this.saveProject = this.saveProject.bind(this)
51-
this.handleChange = this.handleChange.bind(this)
5251
this.discard = this.discard.bind(this)
5352
this.addProject = this.addProject.bind(this)
5453
}
@@ -81,19 +80,29 @@ export default class ProjectList extends Component<ProjectListProps, ProjectList
8180
}
8281
}
8382

84-
handleChange(event, index: number, key: 'name'): void {
83+
handleChange = (event, index: number, key: 'name'): void => {
8584
const { projects, isValid, errorMessage } = { ...this.state }
86-
if (event.target.value.includes(' ')) {
85+
const value = event.target.value
86+
if (!value) {
8787
isValid[key] = false
88-
errorMessage[key] = `Do not use 'spaces' in name`
89-
} else if (event.target.value && event.target.value.length > 2) {
90-
isValid[key] = true
91-
errorMessage[key] = ''
88+
errorMessage[key] = REQUIRED_FIELD_MSG
9289
} else {
93-
isValid[key] = false
94-
errorMessage[key] = 'Atleast 3 characters required.'
90+
if (value.includes(' ')) {
91+
isValid[key] = false
92+
errorMessage[key] = `Do not use 'spaces' in name`
93+
} else if (value.length > 16) {
94+
isValid[key] = false
95+
errorMessage[key] = 'Maximum 16 characters required'
96+
} else if (!/^[a-z0-9]+([-a-z0-9]*[a-z0-9])?$/i.test(value)) {
97+
isValid[key] = false
98+
errorMessage[key] = `Use only lowercase alphanumeric characters, '-' (Cannot start/end with '-')`
99+
} else {
100+
isValid[key] = true
101+
errorMessage[key] = ''
102+
}
95103
}
96-
projects[index][key] = event.target.value
104+
105+
projects[index][key] = value
97106
this.setState({ projects, isValid })
98107
}
99108

0 commit comments

Comments
 (0)