Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪟🧹 Improve connector form types #20057

Merged
merged 5 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions airbyte-webapp/src/core/form/types.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import { JSONSchema7Type, JSONSchema7TypeName } from "json-schema";
import { JSONSchema7TypeName } from "json-schema";

import { AirbyteJSONSchema } from "core/jsonSchema";

interface FormItem {
/**
* When turning the JSON schema into `FormBlock`s,
* some often used props are copied over for easy access.
*/
type FormRelevantJSONSchema = Pick<
AirbyteJSONSchema,
"default" | "examples" | "description" | "pattern" | "order" | "const" | "title" | "airbyte_hidden" | "enum"
>;

interface FormItem extends FormRelevantJSONSchema {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to keep order, title, description, and airbyte_hidden in this interface, now that they are coming from FormRelevantJSONSchema?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not, good catch.

fieldKey: string;
path: string;
isRequired: boolean;
order?: number;
title?: string;
description?: string;
airbyte_hidden?: boolean;
}

export interface FormBaseItem extends FormItem, AirbyteJSONSchema {
export interface FormBaseItem extends FormItem {
_type: "formItem";
type: JSONSchema7TypeName;
isSecret?: boolean;
multiline?: boolean;
default?: JSONSchema7Type;
}

export interface FormGroupItem extends FormItem {
_type: "formGroup";
jsonSchema: AirbyteJSONSchema;
properties: FormBlock[];
isLoading?: boolean;
examples?: JSONSchema7Type;
}

export interface FormConditionItem extends FormItem {
Expand Down
51 changes: 51 additions & 0 deletions airbyte-webapp/src/core/jsonSchema/schemaToUiWidget.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FormGroupItem } from "core/form/types";

import { jsonSchemaToUiWidget } from "./schemaToUiWidget";
import { AirbyteJSONSchemaDefinition } from "./types";

Expand Down Expand Up @@ -115,6 +117,55 @@ it("should reformat jsonSchema to internal widget representation", () => {
expect(builtSchema).toEqual(expected);
});

it("should turn single enum into const but keep multi value enum", () => {
const schema: AirbyteJSONSchemaDefinition = {
type: "object",
required: ["a", "b", "c"],
properties: {
a: { type: "string", enum: ["val1", "val2"] },
b: { type: "string", enum: ["val1"], default: "val1" },
c: { type: "string", const: "val3" },
},
};

const builtSchema = jsonSchemaToUiWidget(schema, "key");

const expectedProperties = [
{
_type: "formItem",
enum: ["val1", "val2"],
fieldKey: "a",
isRequired: true,
isSecret: false,
multiline: false,
path: "key.a",
type: "string",
},
{
_type: "formItem",
const: "val1",
default: "val1",
fieldKey: "b",
isRequired: true,
isSecret: false,
multiline: false,
path: "key.b",
type: "string",
},
{
_type: "formItem",
const: "val3",
fieldKey: "c",
isRequired: true,
isSecret: false,
multiline: false,
path: "key.c",
type: "string",
},
];
expect((builtSchema as FormGroupItem).properties).toEqual(expectedProperties);
});

it("should reformat jsonSchema to internal widget representation with parent schema", () => {
const schema: AirbyteJSONSchemaDefinition = {
type: "object",
Expand Down
23 changes: 11 additions & 12 deletions airbyte-webapp/src/core/jsonSchema/schemaToUiWidget.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pick from "lodash/pick";

import { FormBlock } from "core/form/types";
import { isDefined } from "utils/common";

Expand Down Expand Up @@ -108,32 +110,29 @@ function isKeyRequired(key: string, parentSchema?: AirbyteJSONSchemaDefinition):
return isRequired;
}

const defaultFields: Array<keyof AirbyteJSONSchema> = [
const defaultFields = [
"default",
"examples",
"description",
"pattern",
"order",
"const",
"title",
"enum",

// airbyte specific fields
"airbyte_hidden",
];
] as const;

const pickDefaultFields = (schema: AirbyteJSONSchema): Partial<AirbyteJSONSchema> => {
const partialSchema: Partial<AirbyteJSONSchema> = {
...Object.fromEntries(Object.entries(schema).filter(([k]) => defaultFields.includes(k as keyof AirbyteJSONSchema))),
};
const pickDefaultFields = (schema: AirbyteJSONSchema) => {
const partialSchema = pick(schema, defaultFields);

if (typeof schema.items === "object" && !Array.isArray(schema.items) && schema.items.enum) {
partialSchema.enum = schema.items.enum;
} else if (schema.enum) {
if (schema.enum?.length === 1 && isDefined(schema.default)) {
partialSchema.const = schema.default;
} else {
partialSchema.enum = schema.enum;
}
} else if (schema.enum && schema.enum?.length === 1 && isDefined(schema.default)) {
partialSchema.const = schema.default;
// remove enum key as it has been "picked" already above
delete partialSchema.enum;
}

return partialSchema;
Expand Down