UNPKG

1.42 kBJavaScriptView Raw
1/*!
2 * Copyright (c) 2012-2018 Digital Bazaar, Inc. All rights reserved.
3 */
4const bedrock = require('bedrock');
5
6module.exports = function(context, extend) {
7 const schema = {
8 title: 'JSON-LD context',
9 description: 'A JSON-LD Context'
10 };
11 if(!Array.isArray(context)) {
12 schema.anyOf = [{
13 type: 'string'
14 // enum added below if context param truthy
15 }, {
16 type: 'object'
17 // FIXME: improve context object validator
18 }, {
19 type: 'array',
20 // items added below if context param truthy
21 }];
22 if(context) {
23 schema.anyOf[0].enum = [context];
24 schema.anyOf[2].items = [{const: context}];
25 schema.anyOf[2].additionalItems = false;
26 }
27 } else {
28 Object.assign(schema, {
29 type: 'array',
30 minItems: context.length,
31 uniqueItems: true,
32 items: [],
33 errors: {
34 invalid: 'The JSON-LD context information is invalid.',
35 missing: 'The JSON-LD context information is missing.'
36 }
37 });
38 for(let i = 0; i < context.length; ++i) {
39 if(typeof context[i] === 'string') {
40 schema.items.push({
41 type: 'string',
42 enum: [context[i]]
43 });
44 } else {
45 // FIXME: improve context object validator
46 schema.items.push({type: 'object'});
47 }
48 }
49 }
50 if(extend) {
51 return bedrock.util.extend(true, bedrock.util.clone(schema), extend);
52 }
53 return schema;
54};