Skip to content

Instantly share code, notes, and snippets.

@EpicKiwi
Last active January 7, 2023 16:53
Show Gist options
  • Save EpicKiwi/a903d36eefbd3e8f6aeb59338a92c02b to your computer and use it in GitHub Desktop.
Save EpicKiwi/a903d36eefbd3e8f6aeb59338a92c02b to your computer and use it in GitHub Desktop.
Couchdb Design doc to be used on json-ld database with Schema.org
function(newDoc, oldDoc, userCtx){
if(newDoc._id.startsWith("_design/")){
return
}
if(newDoc._deleted){
return;
}
var context = newDoc["@context"]
if(!context || (context != "http://schema.org/" && context[0] != "http://schema.org/")){
throw({forbidden: "json-ld: Main context MUST be \"http://schema.org/\""})
}
function checkTypes(obj, name = "."){
if(obj["@type"] && obj["@type"].startsWith("http://schema.org/")){
throw({forbidden: `json-ld: @type of object "${name}@type" MUST NOT start with \"http://schema.org/\"`})
}
for(key in obj){
if(typeof obj[key] == "object"){
checkTypes(obj[key], name+""+key+".");
}
}
}
checkTypes(newDoc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment