Skip to content

Commit

Permalink
Improve JSON form empty strings and dependent fields
Browse files Browse the repository at this point in the history
In the StringWidget component, if the string is empty then reset the
field to undefined. This prevents validation false positives.

Also use the JSON schema 'dependentRequired' keyword in the Confluent
schema, and surface object-level formik errors.
  • Loading branch information
jbeisen committed Dec 21, 2023
1 parent b814077 commit 82b986e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
60 changes: 37 additions & 23 deletions arroyo-console/src/routes/connections/JsonForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function StringWidget({
errors,
onChange,
readonly,
resetField,
}: {
path: string;
title: string;
Expand All @@ -56,7 +57,16 @@ function StringWidget({
errors: any;
onChange: (e: React.ChangeEvent<any>) => void;
readonly?: boolean;
resetField?: (field: string) => any;
}) {
const onChangeWrapper = (e: React.ChangeEvent<any>) => {
onChange(e);

if (resetField && e.target.value == '') {
resetField(path);
}
};

return (
<FormControl isRequired={required} isInvalid={errors[path]}>
<FormLabel>{title}</FormLabel>
Expand All @@ -66,15 +76,15 @@ function StringWidget({
type={password ? 'password' : 'text'}
placeholder={readonly ? undefined : placeholder}
value={value || ''}
onChange={e => onChange(e)}
onChange={onChangeWrapper}
readOnly={readonly}
/>
) : (
<Textarea
name={path}
placeholder={placeholder}
value={value || ''}
onChange={e => onChange(e)}
onChange={onChangeWrapper}
resize={'vertical'}
size={'md'}
readOnly={readonly}
Expand Down Expand Up @@ -659,7 +669,7 @@ export function FormInner({
// @ts-ignore
onChange({ target: { name: path, value: {} } });
}
}, [schema]);
}, []);

function traversePath(values: any, typeKey: string): any {
let value = values;
Expand Down Expand Up @@ -734,6 +744,7 @@ export function FormInner({
errors={errors}
onChange={onChange}
readonly={readonly}
resetField={resetField}
/>
);
}
Expand Down Expand Up @@ -811,7 +822,7 @@ export function FormInner({
<Box p={4}>
<FormInner
path={nextPath}
key={key}
key={value}
// @ts-ignore
schema={inSchema}
errors={errors}
Expand All @@ -827,25 +838,28 @@ export function FormInner({
);
} else if (property.properties != undefined) {
return (
<fieldset key={key} style={{ border: '1px solid #888', borderRadius: '8px' }}>
<legend
style={{ marginLeft: '8px', paddingLeft: '16px', paddingRight: '16px' }}
>
{property.title || key}
</legend>
<Box p={4}>
<FormInner
path={nextPath}
// @ts-ignore
schema={property}
errors={errors}
onChange={onChange}
values={values}
resetField={resetField}
readonly={readonly}
/>
</Box>
</fieldset>
<FormControl isInvalid={errors[nextPath]}>
<fieldset key={key} style={{ border: '1px solid #888', borderRadius: '8px' }}>
<legend
style={{ marginLeft: '8px', paddingLeft: '16px', paddingRight: '16px' }}
>
{property.title || key}
</legend>
<Box p={4}>
<FormInner
path={nextPath}
// @ts-ignore
schema={property}
errors={errors}
onChange={onChange}
values={values}
resetField={resetField}
readonly={readonly}
/>
</Box>
</fieldset>
<FormErrorMessage>{errors[nextPath] ?? ''}</FormErrorMessage>
</FormControl>
);
} else if (property.additionalProperties) {
return (
Expand Down
8 changes: 8 additions & 0 deletions connector-schemas/confluent/connection.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
]
}
},
"dependentRequired": {
"apiKey": [
"endpoint"
],
"apiSecret": [
"endpoint"
]
},
"sensitive": [
"apiSecret"
]
Expand Down

0 comments on commit 82b986e

Please sign in to comment.