Skip to content

Commit 6881ed9

Browse files
authored
Merge pull request #673 from devtron-labs/uat/approval-policy-exceptions
fix: CM/CS - incorrect validations for required field for yaml fix
2 parents 1805161 + 43b9c30 commit 6881ed9

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/Shared/Components/CMCS/validations.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ import { getESOSecretDataFromYAML } from './utils'
3232
*/
3333
const validateYaml = (yaml: string): UseFormValidation['custom'] => {
3434
try {
35-
// Check if the YAML string is empty or undefined, if so, throw a no-data error.
35+
// Check if the YAML string is empty or undefined, if so, return required field error.
3636
if (!yaml) {
37-
throw new Error('This is a required field')
37+
return {
38+
isValid: () => false,
39+
message: 'This is a required field',
40+
}
3841
}
3942

4043
// Parse the YAML string into a JSON object.
@@ -47,11 +50,13 @@ const validateYaml = (yaml: string): UseFormValidation['custom'] => {
4750
const errorKeys = [] // To store keys that do not match the key pattern.
4851
const errorValues = [] // To store values that are boolean or numeric, which should be quoted.
4952

53+
let updatedError = ''
54+
5055
// Iterate through the object keys and validate each key-value pair.
5156
Object.keys(json).forEach((k) => {
52-
// If a key or its corresponding value is empty, throw a no-data error.
57+
// If a key or its corresponding value is empty, set required field error.
5358
if (!k && !json[k]) {
54-
throw new Error('This is a required field')
59+
updatedError = 'This is a required field'
5560
}
5661

5762
// Convert the value to a string for easier validation, handle nested objects using YAMLStringify.
@@ -68,7 +73,12 @@ const validateYaml = (yaml: string): UseFormValidation['custom'] => {
6873
}
6974
})
7075

71-
let updatedError = ''
76+
if (updatedError) {
77+
return {
78+
isValid: () => false,
79+
message: updatedError,
80+
}
81+
}
7282

7383
// If there are invalid keys, append a message listing the problematic keys.
7484
if (errorKeys.length > 0) {
@@ -116,9 +126,14 @@ const validateYaml = (yaml: string): UseFormValidation['custom'] => {
116126
*/
117127
const validateEsoSecretYaml = (esoSecretYaml: string): UseFormValidation => {
118128
try {
119-
// Check if the provided YAML string is empty or undefined, and throw a no-data error.
129+
// Check if the provided YAML string is empty or undefined, and return required field error.
120130
if (!esoSecretYaml) {
121-
throw new Error('This is a required field')
131+
return {
132+
custom: {
133+
isValid: () => false,
134+
message: 'This is a required field',
135+
},
136+
}
122137
}
123138

124139
// Parse the YAML string into a JSON object.

0 commit comments

Comments
 (0)