Skip to content

Commit

Permalink
fix: remove array handling code section
Browse files Browse the repository at this point in the history
Changes:
- after removing the section the code was still working fine
- for inserting examples inside an array we only need to add the `/0`
  trailing end in the json_pointer object where deemed necessary
  • Loading branch information
AnimeshKumar923 committed Jun 21, 2024
1 parent fa8b894 commit 3edc981
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions scripts/validation/embedded-examples-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,11 @@ function applyUpdates(updates, baseDoc) {
return;
}

// For non-root cases, use jsonpointer to get and set the correct location
const targetObject = jsonpointer.get(baseDoc, jsonPointerPath);
const updatedObject = mergePatch.apply(targetObject || {}, update.example);
jsonpointer.set(baseDoc, jsonPointerPath, updatedObject);

const parentPath = jsonPointerPath.replace(/\/[^/]+$/, '') || '/';
const targetKey = jsonPointerPath.split('/').pop();
const parentObject = jsonpointer.get(baseDoc, parentPath) || {};

// Check if the target key points to an array
if (Array.isArray(parentObject[targetKey])) {
// Apply patch inside the array
parentObject[targetKey] = parentObject[targetKey].map((item) => {
if (item.name === update.example.name) {
return mergePatch.apply(item, update.example);
}
return item;
});
} else {
// Apply regular merge if not an array
parentObject[targetKey] = mergePatch.apply(parentObject[targetKey] || {}, update.example);
}

jsonpointer.set(baseDoc, parentPath, parentObject);
} catch (e) {
console.error(`\nError processing update for '${update.name}' at path '${update.json_pointer}'`, e);
}
Expand Down

0 comments on commit 3edc981

Please sign in to comment.