Skip to content

Commit

Permalink
Refactor JSON array into standalone field type / validation / defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler committed Jul 17, 2024
1 parent b4abd93 commit f728c18
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 7 additions & 1 deletion common/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export type Index = {
********** WORKFLOW TYPES/INTERFACES **********
*/

export type ConfigFieldType = 'string' | 'json' | 'select' | 'model' | 'map';
export type ConfigFieldType =
| 'string'
| 'json'
| 'jsonArray'
| 'select'
| 'model'
| 'map';
export type ConfigFieldValue = string | {};
export interface IConfigField {
type: ConfigFieldType;
Expand Down
5 changes: 4 additions & 1 deletion public/utils/config_to_form_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function ingestConfigToFormik(
let ingestFormikValues = {} as FormikValues;
if (ingestConfig) {
ingestFormikValues['enabled'] = ingestConfig.enabled;
ingestFormikValues['docs'] = ingestDocs || '[]';
ingestFormikValues['docs'] = ingestDocs || getInitialValue('jsonArray');
ingestFormikValues['enrich'] = processorsConfigToFormik(
ingestConfig.enrich
);
Expand Down Expand Up @@ -120,5 +120,8 @@ export function getInitialValue(fieldType: ConfigFieldType): ConfigFieldValue {
case 'json': {
return '{}';
}
case 'jsonArray': {
return '[]';
}
}
}
21 changes: 18 additions & 3 deletions public/utils/config_to_schema_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function ingestConfigToSchema(
): ObjectSchema<any> {
const ingestSchemaObj = {} as { [key: string]: Schema };
if (ingestConfig) {
ingestSchemaObj['docs'] = getFieldSchema('json');
ingestSchemaObj['docs'] = getFieldSchema('jsonArray');
ingestSchemaObj['enrich'] = processorsConfigToSchema(ingestConfig.enrich);
ingestSchemaObj['index'] = indexConfigToSchema(ingestConfig.index);
}
Expand Down Expand Up @@ -108,16 +108,31 @@ function getFieldSchema(fieldType: ConfigFieldType): Schema {
break;
}
case 'json': {
baseSchema = yup.string().test('json', 'Invalid JSON array', (value) => {
baseSchema = yup.string().test('json', 'Invalid JSON', (value) => {
try {
// @ts-ignore
return Array.isArray(JSON.parse(value));
JSON.parse(value);
return true;
} catch (error) {
return false;
}
});

break;
}
case 'jsonArray': {
baseSchema = yup
.string()
.test('jsonArray', 'Invalid JSON array', (value) => {
try {
// @ts-ignore
return Array.isArray(JSON.parse(value));
return true;
} catch (error) {
return false;
}
});

break;
}
}
Expand Down

0 comments on commit f728c18

Please sign in to comment.