From 926970feeb8755dd3ab087ed32aa7817f558d79f Mon Sep 17 00:00:00 2001
From: Lars Trieloff <trieloff@adobe.com>
Date: Wed, 13 Dec 2017 19:29:30 +0000
Subject: [PATCH] #14 highlight schemas with custom properties

---
 examples/docs/README.md                       |  1 +
 examples/docs/abstract.schema.md              |  2 +-
 examples/docs/custom.schema.md                | 17 ++++++++
 examples/docs/definitions.schema.md           |  2 +-
 examples/docs/example.schema.md               |  2 +-
 examples/docs/extensible.schema.md            |  2 +-
 examples/docs/simple.schema.md                |  2 +-
 examples/docs/subdir/subdir.schema.md         |  2 +-
 examples/generated-schemas/custom.schema.json | 43 +++++++++++++++++++
 examples/schemas/custom.schema.json           | 37 ++++++++++++++++
 lib/markdownWriter.js                         | 13 +++++-
 11 files changed, 116 insertions(+), 7 deletions(-)
 create mode 100644 examples/docs/custom.schema.md
 create mode 100644 examples/generated-schemas/custom.schema.json
 create mode 100644 examples/schemas/custom.schema.json

diff --git a/examples/docs/README.md b/examples/docs/README.md
index ecc614e..be74562 100644
--- a/examples/docs/README.md
+++ b/examples/docs/README.md
@@ -7,6 +7,7 @@
 ## /
 
 * [Abstract](./abstract.schema.md) – `https://example.com/schemas/abstract`
+* [Custom](./custom.schema.md) – `https://example.com/schemas/custom`
 * [Definitions](./definitions.schema.md) – `https://example.com/schemas/definitions`
 * [Example](./example.schema.md) – `https://example.com/schemas/example`
 * [Extensible](./extensible.schema.md) – `https://example.com/schemas/extensible`
diff --git a/examples/docs/abstract.schema.md b/examples/docs/abstract.schema.md
index c035b2e..ea39f5b 100644
--- a/examples/docs/abstract.schema.md
+++ b/examples/docs/abstract.schema.md
@@ -13,5 +13,5 @@ This is an abstract schema. It has `definitions`, but does not declare any prope
 
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
-| Cannot be instantiated | Is extensible | true | [abstract.schema.json](abstract.schema.json) |
+| Cannot be instantiated | Yes | Forbidden | [abstract.schema.json](abstract.schema.json) |
 ---
\ No newline at end of file
diff --git a/examples/docs/custom.schema.md b/examples/docs/custom.schema.md
new file mode 100644
index 0000000..c81635f
--- /dev/null
+++ b/examples/docs/custom.schema.md
@@ -0,0 +1,17 @@
+
+---
+template: reference
+foo: bar
+---
+
+# Custom
+```
+https://example.com/schemas/custom
+```
+
+This is an extensible schema. It has `definitions`, that can be used in other schemas. Additionally, it allows custom properties.
+
+| Abstract | Extensible | Custom Properties | Defined In |
+|----------|------------|-------------------|------------|
+| Can be instantiated | Yes | Allowed | [custom.schema.json](custom.schema.json) |
+---
\ No newline at end of file
diff --git a/examples/docs/definitions.schema.md b/examples/docs/definitions.schema.md
index cc35ca4..f1c52de 100644
--- a/examples/docs/definitions.schema.md
+++ b/examples/docs/definitions.schema.md
@@ -14,5 +14,5 @@ It is imported using `allOf` and `$ref`.
 
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
-| Can be instantiated | Is extensible | true | [definitions.schema.json](definitions.schema.json) |
+| Can be instantiated | Yes | Forbidden | [definitions.schema.json](definitions.schema.json) |
 ---
\ No newline at end of file
diff --git a/examples/docs/example.schema.md b/examples/docs/example.schema.md
index add444b..8c3bfa8 100644
--- a/examples/docs/example.schema.md
+++ b/examples/docs/example.schema.md
@@ -13,5 +13,5 @@ This is an example schema with examples. Too many examples? There can never be t
 
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
-| Can be instantiated | Is not extensible | true | [example.schema.json](example.schema.json) |
+| Can be instantiated | No | Forbidden | [example.schema.json](example.schema.json) |
 ---
\ No newline at end of file
diff --git a/examples/docs/extensible.schema.md b/examples/docs/extensible.schema.md
index 1056513..735a598 100644
--- a/examples/docs/extensible.schema.md
+++ b/examples/docs/extensible.schema.md
@@ -13,5 +13,5 @@ This is an extensible schema. It has `definitions`, that can be used in other sc
 
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
-| Cannot be instantiated | Is extensible | true | [extensible.schema.json](extensible.schema.json) |
+| Cannot be instantiated | Yes | Forbidden | [extensible.schema.json](extensible.schema.json) |
 ---
\ No newline at end of file
diff --git a/examples/docs/simple.schema.md b/examples/docs/simple.schema.md
index af3cf5b..7a9d099 100644
--- a/examples/docs/simple.schema.md
+++ b/examples/docs/simple.schema.md
@@ -13,5 +13,5 @@ This is a *very* simple example of a JSON schema. There is only one property.
 
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
-| Can be instantiated | Is not extensible | true | [simple.schema.json](simple.schema.json) |
+| Can be instantiated | No | Forbidden | [simple.schema.json](simple.schema.json) |
 ---
\ No newline at end of file
diff --git a/examples/docs/subdir/subdir.schema.md b/examples/docs/subdir/subdir.schema.md
index 73c5fcf..b7fee1d 100644
--- a/examples/docs/subdir/subdir.schema.md
+++ b/examples/docs/subdir/subdir.schema.md
@@ -13,5 +13,5 @@ A schema in a sub directory
 
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
-| Cannot be instantiated | Is extensible | true | [subdir/subdir.schema.json](subdir/subdir.schema.json) |
+| Cannot be instantiated | Yes | Forbidden | [subdir/subdir.schema.json](subdir/subdir.schema.json) |
 ---
\ No newline at end of file
diff --git a/examples/generated-schemas/custom.schema.json b/examples/generated-schemas/custom.schema.json
new file mode 100644
index 0000000..febfea6
--- /dev/null
+++ b/examples/generated-schemas/custom.schema.json
@@ -0,0 +1,43 @@
+{
+    "meta:license": [
+        "Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
+        "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
+        "you may not use this file except in compliance with the License. You may obtain a copy",
+        "of the License at http://www.apache.org/licenses/LICENSE-2.0"
+    ],
+    "$schema": "http://json-schema.org/draft-06/schema#",
+    "$id": "https://example.com/schemas/custom",
+    "title": "Custom",
+    "description": "This is an extensible schema. It has `definitions`, that can be used in other schemas. Additionally, it allows custom properties.",
+    "type": "object",
+    "meta:extensible": true,
+    "definitions": {
+        "first": {
+            "properties": {
+                "foo": {
+                    "type": "string",
+                    "description": "A unique identifier given to every addressable thing."
+                }
+            }
+        },
+        "second": {
+            "properties": {
+                "bar": {
+                    "type": "string",
+                    "description": "A unique identifier given to every addressable thing."
+                }
+            }
+        }
+    },
+    "allOf": [
+        {
+            "$ref": "https://ns.adobe.com/xdm/common/extensible.schema.json#/definitions/@context"
+        },
+        {
+            "$ref": "#/definitions/first"
+        },
+        {
+            "$ref": "#/definitions/second"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/examples/schemas/custom.schema.json b/examples/schemas/custom.schema.json
new file mode 100644
index 0000000..d80987f
--- /dev/null
+++ b/examples/schemas/custom.schema.json
@@ -0,0 +1,37 @@
+{
+    "meta:license": [
+        "Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
+        "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
+        "you may not use this file except in compliance with the License. You may obtain a copy",
+        "of the License at http://www.apache.org/licenses/LICENSE-2.0"
+    ],
+    "$schema": "http://json-schema.org/draft-06/schema#",
+    "$id": "https://example.com/schemas/custom",
+    "title": "Custom",
+    "description": "This is an extensible schema. It has `definitions`, that can be used in other schemas. Additionally, it allows custom properties.",
+    "type": "object",
+    "meta:extensible": true,
+    "definitions": {
+        "first": {
+            "properties": {
+                "foo": {
+                    "type": "string",
+                    "description": "A unique identifier given to every addressable thing."
+                }
+            }
+        },
+        "second": {
+            "properties": {
+                "bar": {
+                    "type": "string",
+                    "description": "A unique identifier given to every addressable thing."
+                }
+            }
+        }
+    },
+    "allOf": [
+      { "$ref": "https://ns.adobe.com/xdm/common/extensible.schema.json#/definitions/@context"},
+      { "$ref": "#/definitions/first" },
+      { "$ref": "#/definitions/second" }
+    ]
+}
\ No newline at end of file
diff --git a/lib/markdownWriter.js b/lib/markdownWriter.js
index 79f2a78..51d91e6 100644
--- a/lib/markdownWriter.js
+++ b/lib/markdownWriter.js
@@ -29,13 +29,24 @@ function assoc(obj, key, value) {
   return obj;
 }
 
+function custom(schema) {
+  if (schema.allOf) {
+    for (let i=0; i<schema.allOf.length; i++) {
+      if (schema.allOf[i].$ref && schema.allOf[i].$ref === 'https://ns.adobe.com/xdm/common/extensible.schema.json#/definitions/@context') {
+        return true;
+      }
+    }
+  }
+  return false;
+}
+
 function schemaProps(schema, schemaPath, filename) {
   console.log(_.keys(schema));
   return {
     // if there are definitions, but no properties
     abstract: (schema.definitions !== undefined && _.keys(schema.properties).length === 0) ? 'Cannot be instantiated' : 'Can be instantiated',
     extensible: (schema.definitions !== undefined || schema['meta:extensible'] === true) ? 'Yes' : 'No',
-    custom: true,
+    custom: custom(schema) ? 'Allowed' : 'Forbidden',
     original: filename.substr(schemaPath.length).substr(1),
   };
 }
-- 
GitLab