Skip to content

Commit

Permalink
Fix boolean field wrong mapping for yup schema (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamakase authored Nov 12, 2020
1 parent 1703c5a commit 2622986
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 10 additions & 1 deletion airbyte-webapp/src/core/jsonSchema/schemaToYup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test("should build schema for simple case", () => {
const schema: JSONSchema7 = {
type: "object",
title: "Postgres Source Spec",
required: ["host", "port", "user", "dbname"],
required: ["host", "port", "user", "dbname", "is_field_no_default"],
properties: {
host: { type: "string", description: "Hostname of the database." },
port: {
Expand All @@ -21,6 +21,13 @@ test("should build schema for simple case", () => {
type: "string",
description: "Username to use to access the database."
},
is_sandbox: {
type: "boolean",
default: false
},
is_field_no_default: {
type: "boolean"
},
dbname: { type: "string", description: "Name of the database." },
password: {
type: "string",
Expand All @@ -39,6 +46,8 @@ test("should build schema for simple case", () => {
.max(65536)
.required("form.empty.error"),
user: yup.string().required("form.empty.error"),
is_sandbox: yup.boolean().default(false),
is_field_no_default: yup.boolean().required("form.empty.error"),
dbname: yup.string().required("form.empty.error"),
password: yup.string()
});
Expand Down
11 changes: 9 additions & 2 deletions airbyte-webapp/src/core/jsonSchema/schemaToYup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const buildYupFormForJsonSchema = (
| yup.NumberSchema
| yup.StringSchema
| yup.ObjectSchema
| yup.BooleanSchema
| null = null;

if (jsonSchema.oneOf && uiConfig && propertyPath) {
Expand All @@ -54,6 +55,9 @@ export const buildYupFormForJsonSchema = (
case "string":
schema = yup.string();
break;
case "boolean":
schema = yup.boolean();
break;
case "integer":
schema = yup.number();

Expand Down Expand Up @@ -90,12 +94,15 @@ export const buildYupFormForJsonSchema = (
);
}

if (schema && jsonSchema.default) {
const hasDefault =
jsonSchema.default !== undefined && jsonSchema.default !== null;

if (schema && hasDefault) {
schema = schema.default(jsonSchema.default);
}

const isRequired =
!jsonSchema?.default &&
!hasDefault &&
parentSchema &&
Array.isArray(parentSchema?.required) &&
parentSchema.required.find(item => item === propertyKey);
Expand Down

0 comments on commit 2622986

Please sign in to comment.