Skip to content

Commit

Permalink
Merge branch 'embedded-examples-validation' into spec-examples-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AnimeshKumar923 committed Jul 2, 2024
2 parents b0d36e7 + f31ff2a commit e1b0e65
Show file tree
Hide file tree
Showing 89 changed files with 11 additions and 60,058 deletions.
31 changes: 11 additions & 20 deletions scripts/validation/embedded-examples-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,29 @@ function extractCommentsAndExamples(content) {
// Extract comments and examples from the markdown file
const combinedData = extractCommentsAndExamples(markdownContent);

// Function to convert URI Fragment to JSON Pointer
function uriFragmentToJsonPointer(uriFragment) {
if (uriFragment === '#') return '';
return uriFragment.slice(1).split('/').map(decodeURIComponent).join('/');
}

// Function to apply JSON Merge Patch updates to the document
function applyUpdates(updates, baseDoc) {
updates.forEach(update => {
try {
const jsonPointerPath = update.json_pointer;
const jsonPointerPath = uriFragmentToJsonPointer(update.json_pointer);

// Handle root document case
if (jsonPointerPath === '') {
baseDoc = mergePatch.apply(baseDoc, update.example);
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
42 changes: 0 additions & 42 deletions scripts/validation/ex-doc-v1.md

This file was deleted.

Loading

0 comments on commit e1b0e65

Please sign in to comment.