From 3648dc024e05121f1848f0fcd92eaca97105365d Mon Sep 17 00:00:00 2001 From: utnim2 Date: Wed, 5 Jun 2024 02:52:30 +0530 Subject: [PATCH] required array is removed when changing types and removing a property --- .../components/VisualEditor/SchemaObject.tsx | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/ui/components/VisualEditor/SchemaObject.tsx b/packages/ui/components/VisualEditor/SchemaObject.tsx index d254eb467..d66c8a4f7 100644 --- a/packages/ui/components/VisualEditor/SchemaObject.tsx +++ b/packages/ui/components/VisualEditor/SchemaObject.tsx @@ -49,6 +49,26 @@ const SchemaObject: React.FC = ({ } else { _.unset(updatedSchema, normalizedPath); } + + const propertyName = normalizedPath.split(".").pop(); + console.log("propertyName", propertyName) + console.log("updatedSchema.properties.required", updatedSchema) + console.log("normalizedPath", normalizedPath) + const parentPath = normalizedPath.slice( + 0, + normalizedPath.lastIndexOf(".") + ); + const schemaPath = parentPath.split('.properties'); + const requiredPath = schemaPath.slice(0, -1).join('.properties') + '.required'; + const nestedRequired = _.get(updatedSchema, requiredPath, []); + + if (nestedRequired.includes(propertyName)) { + _.set( + updatedSchema, + requiredPath, + nestedRequired.filter((requiredProp: string) => requiredProp !== propertyName) + ); + } console.log("updatedSchema", JSON.stringify(updatedSchema)) onSchemaChange(updatedSchema); }; @@ -68,6 +88,15 @@ const SchemaObject: React.FC = ({ _.set(currentSchema, typePath , newTypeValue); const itemsPath = `${normalizedPath}.items`; _.unset(currentSchema, itemsPath); + + const propertyName = normalizedPath.split(".").slice(-1)[0]; + const parentPath = normalizedPath.slice(0, normalizedPath.lastIndexOf('.')); + const requiredPath = `${parentPath}.${propertyName}.required`; + const nestedRequired = _.get(currentSchema, requiredPath, []); + + if (nestedRequired?.length > 0 ) { + _.unset(currentSchema, requiredPath); + } } _.unset(currentSchema, `${normalizedPath}.properties`)