@@ -32,9 +32,12 @@ import { getESOSecretDataFromYAML } from './utils'
32
32
*/
33
33
const validateYaml = ( yaml : string ) : UseFormValidation [ 'custom' ] => {
34
34
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.
36
36
if ( ! yaml ) {
37
- throw new Error ( 'This is a required field' )
37
+ return {
38
+ isValid : ( ) => false ,
39
+ message : 'This is a required field' ,
40
+ }
38
41
}
39
42
40
43
// Parse the YAML string into a JSON object.
@@ -47,11 +50,13 @@ const validateYaml = (yaml: string): UseFormValidation['custom'] => {
47
50
const errorKeys = [ ] // To store keys that do not match the key pattern.
48
51
const errorValues = [ ] // To store values that are boolean or numeric, which should be quoted.
49
52
53
+ let updatedError = ''
54
+
50
55
// Iterate through the object keys and validate each key-value pair.
51
56
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.
53
58
if ( ! k && ! json [ k ] ) {
54
- throw new Error ( 'This is a required field' )
59
+ updatedError = 'This is a required field'
55
60
}
56
61
57
62
// Convert the value to a string for easier validation, handle nested objects using YAMLStringify.
@@ -68,7 +73,12 @@ const validateYaml = (yaml: string): UseFormValidation['custom'] => {
68
73
}
69
74
} )
70
75
71
- let updatedError = ''
76
+ if ( updatedError ) {
77
+ return {
78
+ isValid : ( ) => false ,
79
+ message : updatedError ,
80
+ }
81
+ }
72
82
73
83
// If there are invalid keys, append a message listing the problematic keys.
74
84
if ( errorKeys . length > 0 ) {
@@ -116,9 +126,14 @@ const validateYaml = (yaml: string): UseFormValidation['custom'] => {
116
126
*/
117
127
const validateEsoSecretYaml = ( esoSecretYaml : string ) : UseFormValidation => {
118
128
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.
120
130
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
+ }
122
137
}
123
138
124
139
// Parse the YAML string into a JSON object.
0 commit comments