-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: schema update add default to all json-form schema
- Loading branch information
1 parent
ffa2009
commit 98f581b
Showing
3 changed files
with
68 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
export default function updateSchemaDefaults(schema, formValues) { | ||
// Return early if schema or formValues is not valid | ||
if (!schema || !formValues || typeof formValues !== "object") { | ||
return schema; | ||
} | ||
|
||
// Handle different schema structures | ||
if (schema.type === "object" && schema.properties) { | ||
updatePropertiesWithNesting(schema.properties, formValues); | ||
} else if (schema.allOf) { | ||
schema.allOf.forEach((subSchema) => { | ||
if (subSchema.properties) { | ||
updatePropertiesWithNesting(subSchema.properties, formValues); | ||
} | ||
}); | ||
} | ||
|
||
return schema; | ||
} | ||
|
||
function updatePropertiesWithNesting(properties, formValues) { | ||
for (let key in formValues) { | ||
if (properties[key] && !properties[key]?.options?.hidden) { | ||
if ( | ||
properties[key].type === "object" && | ||
properties[key].properties && | ||
typeof formValues[key] === "object" | ||
) { | ||
// Handle nested objects | ||
updatePropertiesWithNesting( | ||
properties[key].properties, | ||
formValues[key], | ||
); | ||
} else { | ||
// Handle direct values | ||
if (isValidDefaultValue(properties[key], formValues[key])) { | ||
properties[key].default = formValues[key]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function isValidDefaultValue(property, value) { | ||
switch (property.type) { | ||
case "string": | ||
return typeof value === "string"; | ||
case "number": | ||
case "integer": | ||
return typeof value === "number"; | ||
case "boolean": | ||
return typeof value === "boolean"; | ||
case "array": | ||
return Array.isArray(value); | ||
case "object": | ||
return ( | ||
typeof value === "object" && value !== null && !Array.isArray(value) | ||
); | ||
default: | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters