Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
fix(schema): Add property defaults to schema
Browse files Browse the repository at this point in the history
Fixes #424
  • Loading branch information
kylef committed Nov 18, 2016
1 parent 491d264 commit 57c4067
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/refract/JSONSchemaVisitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ namespace refract
if (IsTypeAttribute(e, "nullable")) {
renderer.addSchemaType("null");
}

if (e.value.second) {
IElement::MemberElementCollection::const_iterator defaultIt = e.value.second->attributes.find("default");

if (defaultIt != e.value.second->attributes.end()) {
renderer.addMember("default", (*defaultIt)->clone());
}
}

addMember(str->value, renderer.getOwnership());
}
else {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/mson/enum-empty-default.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"body": "{\n \"type\": \"\"\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\n \"regular\"\n ]\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\n \"regular\"\n ],\n \"default\": []\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down Expand Up @@ -139,7 +139,7 @@
}
],
"body": "{\n \"type\": \"\"\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\n \"regular\"\n ]\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\n \"regular\"\n ],\n \"default\": []\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/render/array-samples.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"body": "{\n \"a1\": [\n \"d\",\n \"f\",\n \"l\",\n \"t\"\n ],\n \"a2\": [\n \"s\",\n \"m\",\n \"p\",\n \"l\"\n ],\n \"a3\": [\n \"1\",\n \"2\",\n \"3\"\n ]\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"a1\": {\n \"type\": \"array\",\n \"default\": [\n \"d\",\n \"f\",\n \"l\",\n \"t\"\n ]\n },\n \"a2\": {\n \"type\": \"array\"\n },\n \"a3\": {\n \"type\": \"array\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"a1\": {\n \"type\": \"array\",\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"d\"\n },\n {\n \"element\": \"string\",\n \"content\": \"f\"\n },\n {\n \"element\": \"string\",\n \"content\": \"l\"\n },\n {\n \"element\": \"string\",\n \"content\": \"t\"\n }\n ]\n },\n \"a2\": {\n \"type\": \"array\"\n },\n \"a3\": {\n \"type\": \"array\"\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down Expand Up @@ -221,7 +221,7 @@
}
],
"body": "{\n \"a1\": [\n \"d\",\n \"f\",\n \"l\",\n \"t\"\n ],\n \"a2\": [\n \"s\",\n \"m\",\n \"p\",\n \"l\"\n ],\n \"a3\": [\n \"1\",\n \"2\",\n \"3\"\n ]\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"a1\": {\n \"type\": \"array\",\n \"default\": [\n \"d\",\n \"f\",\n \"l\",\n \"t\"\n ]\n },\n \"a2\": {\n \"type\": \"array\"\n },\n \"a3\": {\n \"type\": \"array\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"a1\": {\n \"type\": \"array\",\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"d\"\n },\n {\n \"element\": \"string\",\n \"content\": \"f\"\n },\n {\n \"element\": \"string\",\n \"content\": \"l\"\n },\n {\n \"element\": \"string\",\n \"content\": \"t\"\n }\n ]\n },\n \"a2\": {\n \"type\": \"array\"\n },\n \"a3\": {\n \"type\": \"array\"\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/render/enum-default.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"body": "{\n \"enumField\": \"c\"\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"enumField\": {\n \"type\": \"string\",\n \"enum\": [\n \"a\",\n \"b\",\n \"c\"\n ],\n \"default\": \"c\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"enumField\": {\n \"type\": \"string\",\n \"enum\": [\n \"a\",\n \"b\",\n \"c\"\n ],\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"c\"\n }\n ]\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down Expand Up @@ -141,7 +141,7 @@
}
],
"body": "{\n \"enumField\": \"c\"\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"enumField\": {\n \"type\": \"string\",\n \"enum\": [\n \"a\",\n \"b\",\n \"c\"\n ],\n \"default\": \"c\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"enumField\": {\n \"type\": \"string\",\n \"enum\": [\n \"a\",\n \"b\",\n \"c\"\n ],\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"c\"\n }\n ]\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/render/inheritance-object-sample.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"body": "{\n \"r1\": \"v1\",\n \"r2\": \"v2\",\n \"r3\": \"v3\"\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\",\n \"default\": \"v3\"\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down Expand Up @@ -105,7 +105,7 @@
}
],
"body": "{\n \"r1\": \"v1\",\n \"r2\": \"v2\",\n \"r3\": \"v3\"\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\",\n \"default\": \"v3\"\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/render/mixin-object-sample.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"body": "{\n \"i1\": \"sample\",\n \"r1\": \"v1\",\n \"r2\": \"v2\",\n \"r3\": \"v3\",\n \"i2\": \"\"\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"i1\": {\n \"type\": \"string\"\n },\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\"\n },\n \"i2\": {\n \"type\": \"string\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"i1\": {\n \"type\": \"string\"\n },\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\",\n \"default\": \"v3\"\n },\n \"i2\": {\n \"type\": \"string\"\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down Expand Up @@ -143,7 +143,7 @@
}
],
"body": "{\n \"i1\": \"sample\",\n \"r1\": \"v1\",\n \"r2\": \"v2\",\n \"r3\": \"v3\",\n \"i2\": \"\"\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"i1\": {\n \"type\": \"string\"\n },\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\"\n },\n \"i2\": {\n \"type\": \"string\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"i1\": {\n \"type\": \"string\"\n },\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\",\n \"default\": \"v3\"\n },\n \"i2\": {\n \"type\": \"string\"\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/render/primitive-samples.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
],
"body": "{\n \"s1\": \"default\",\n \"s2\": \"sample\",\n \"s3\": \"v1\",\n \"n1\": 5,\n \"n2\": 8,\n \"b1\": true\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"s1\": {\n \"type\": \"string\"\n },\n \"s2\": {\n \"type\": \"string\"\n },\n \"s3\": {\n \"type\": \"string\"\n },\n \"n1\": {\n \"type\": \"number\"\n },\n \"n2\": {\n \"type\": \"number\"\n },\n \"b1\": {\n \"type\": \"boolean\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"s1\": {\n \"type\": \"string\",\n \"default\": \"default\"\n },\n \"s2\": {\n \"type\": \"string\"\n },\n \"s3\": {\n \"type\": \"string\",\n \"default\": \"d1\"\n },\n \"n1\": {\n \"type\": \"number\"\n },\n \"n2\": {\n \"type\": \"number\",\n \"default\": 7\n },\n \"b1\": {\n \"type\": \"boolean\"\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down Expand Up @@ -210,7 +210,7 @@
}
],
"body": "{\n \"s1\": \"default\",\n \"s2\": \"sample\",\n \"s3\": \"v1\",\n \"n1\": 5,\n \"n2\": 8,\n \"b1\": true\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"s1\": {\n \"type\": \"string\"\n },\n \"s2\": {\n \"type\": \"string\"\n },\n \"s3\": {\n \"type\": \"string\"\n },\n \"n1\": {\n \"type\": \"number\"\n },\n \"n2\": {\n \"type\": \"number\"\n },\n \"b1\": {\n \"type\": \"boolean\"\n }\n }\n}",
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"s1\": {\n \"type\": \"string\",\n \"default\": \"default\"\n },\n \"s2\": {\n \"type\": \"string\"\n },\n \"s3\": {\n \"type\": \"string\",\n \"default\": \"d1\"\n },\n \"n1\": {\n \"type\": \"number\"\n },\n \"n2\": {\n \"type\": \"number\",\n \"default\": 7\n },\n \"b1\": {\n \"type\": \"boolean\"\n }\n }\n}",
"content": [
{
"element": "dataStructure",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/schema/default-attribute.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"attributes": {
"contentType": "application/schema+json"
},
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"string\",\n \"enum\": [\n \"3\",\n \"4\"\n ],\n \"default\": \"4\"\n }\n }\n}"
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"string\",\n \"enum\": [\n \"3\",\n \"4\"\n ],\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"4\"\n }\n ]\n }\n }\n}"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/schema/default-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"attributes": {
"contentType": "application/schema+json"
},
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"array\",\n \"default\": [\n \"4\"\n ]\n }\n }\n}"
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"array\",\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"4\"\n }\n ]\n }\n }\n}"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/schema/default-section.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"attributes": {
"contentType": "application/schema+json"
},
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"string\",\n \"enum\": [\n \"3\",\n \"4\"\n ],\n \"default\": \"4\"\n }\n }\n}"
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"string\",\n \"enum\": [\n \"3\",\n \"4\"\n ],\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"4\"\n }\n ]\n }\n }\n}"
}
]
}
Expand Down

0 comments on commit 57c4067

Please sign in to comment.