Skip to content

Commit

Permalink
feat: schema update add default to all json-form schema
Browse files Browse the repository at this point in the history
  • Loading branch information
srijitcoder committed Nov 5, 2024
1 parent ffa2009 commit 98f581b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { default as getFileChangesStatus } from "./get-file-changes-status";
export { default as createColorBlocks } from "./create-file-diff-color-block";
export { encodeString, decodeString } from "./encoding-decoding-string.js";
export { default as isValidFormJSON } from "./is-valid-form-json";
export { default as updateSchemaDefaults } from "./update-schema-default";
62 changes: 62 additions & 0 deletions src/helpers/update-schema-default.js
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;
}
}
19 changes: 5 additions & 14 deletions src/views/FileEditView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
} from "@/api/index.js";
import { useRoute, useRouter } from "vue-router";
import { querySessionDetailsMethod } from "@/methods/session-view/index.js";
import { decodeString, useLoader } from "@/helpers/index.js";
import {
decodeString,
updateSchemaDefaults,
useLoader,
} from "@/helpers/index.js";
import {
queryFileDetailsMethod,
initEOXJSONFormMethod,
Expand Down Expand Up @@ -96,19 +100,6 @@ const updateNavButtonConfig = (text = "Saved", disabled = true) => {
reset.value = disabled;
};
function updateSchemaDefaults(schema, formValues) {
let properties = schema.allOf[0].properties;
for (let key in formValues) {
if (properties[key] && !properties[key]?.options?.hidden) {
properties[key].default = formValues[key];
}
}
schema.allOf[0].properties = properties;
return schema;
}
const onFileChange = (e) => {
if (e.detail && isFormJSON.value) {
const newSchema = updateSchemaDefaults(
Expand Down

0 comments on commit 98f581b

Please sign in to comment.