Public
Edited
Dec 20
Insert cell
Insert cell
jsonldDocument = ({
"@context": {
name: "http://schema.org/name",
description: "http://schema.org/description"
},
name: "Example Name",
description: "An example description for JSON-LD."
})
Insert cell
parseAndValidateJSONLD(jsonldDocument)
Insert cell
jsonldContext = ({
name: "http://schema.org/name",
description: "http://schema.org/description"
})
Insert cell
/**
* Validates the @context part of a JSON-LD document.
* @param {Object} context - The @context part of a JSON-LD document.
* @returns {Promise<boolean>} - Returns true if the @context is valid, otherwise throws an error.
*/
async function validateJSONLDContext(context) {
try {
// Create a minimal JSON-LD document with only the context
const minimalDoc = { "@context": context };

// Attempt to expand the minimal document
await jsonld.expand(minimalDoc);

console.log('Validation successful for @context.');
return true; // If it expands without error, the @context is valid
} catch (error) {
console.error('Invalid @context:', error.message);
throw new Error(`@context validation failed: ${error.message}`);
}
}
Insert cell
invalidJsonldContext = ({
"name": "http://schema.org/name",
"invalidKey": "http://example.com/invalidKey", // Changed to a valid IRI
"personType": {
"@id": "http://schema.org/Person",
"@type": "@id"
},
"description": "http://schema.org/description" // Changed to a single IRI
})
Insert cell
validateJSONLDContext(invalidJsonldContext)
Insert cell
async function parseAndValidateJSONLD(document, options = {}) {
try {
const expanded = await jsonld.expand(document, options);
if (expanded.length === 0) {
throw new Error('The document is empty or invalid JSON-LD.');
}

return expanded;
} catch (error) {
console.error('Validation failed:', error.message);
throw error;
}
}
Insert cell
jsonld = (await import("https://esm.sh/jsonld@8.3.2")).default
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more