Skip to content

Dynamic Schema issues in array #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dist/js/brutusin-json-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,8 @@ if (typeof brutusin === "undefined") {

obj.getData = function () {
function removeEmptiesAndNulls(object, s) {
if (ss === null) {
ss = SCHEMA_ANY;
if (!s) { // 03_AddLevel2Item_SelectOPTION-2.png
s = SCHEMA_ANY; // 03_AddLevel2Item_SelectOPTION-2.png
}
if (s.$ref) {
s = getDefinition(s.$ref);
Expand Down Expand Up @@ -1307,7 +1307,7 @@ if (typeof brutusin === "undefined") {

function cleanSchemaMap(schemaId) {
for (var prop in schemaMap) {
if (schemaId.startsWith(prop)) {
if (prop.startsWith(schemaId)) { // 02_AddLevel1Item_Delete_AgainAdd.png
delete schemaMap[prop];
}
}
Expand Down Expand Up @@ -1409,7 +1409,7 @@ if (typeof brutusin === "undefined") {
throw ("Node '" + name + "' is of type array");
}
var element = currentToken.substring(1, currentToken.length - 1);
if (element.equals("#")) {
if (element === "#") { // 01_elementEquals.png
for (var i = 0; i < data.length; i++) {
var child = data[i];
visit(name + currentToken, queue.slice(0), child, data, i);
Expand Down
95 changes: 95 additions & 0 deletions examples/example1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../dist/css/brutusin-json-forms.min.css">

<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" charset="utf8" src="../dist/js/brutusin-json-forms.js"></script>
<script type="text/javascript" charset="utf8" src="../dist/js/brutusin-json-forms-bootstrap.min.js"></script>
</head>
<body>
<div id="formDialog" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left"></h4>
<div class="btn-toolbar pull-right formDialogControlsGroup">
<button type="button" class="btn btn-sm btn-warning" id="formDialogCancel" data-dismiss="modal">Cancel</button>
</div>
</div>
<div id="formDialogBody" class="modal-body">
<div id="formDialogContainer"></div>
</div>
</div>
</div>
</div>
</body>
<script>
$(document).ready(function() {
var formSchema = {
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"properties": {
"speciesList": {
"type": "array",
"title": "List of species",
"items": {
"type": "object",
"properties": {
"species": {
"title": "Species supported",
"description": "Changes in this property (`$.speciesList[#].species`) trigger the resolution of the actual (depending on the values being selected) schema of a dependent property (`$.speciesList[#].subspecies`)",
"type": "string",
"enum": [
"human",
"dog",
"cat"
],
"required": true
},
"subspecies": {
"dependsOn": [
"species"
]
}
}
}
}
}
};

var BrutusinForms = brutusin["json-forms"];
var schemaResolver = function(names, data, cb) {
var schemas = new Object();
var schema = new Object();
var idx = data.speciesList.length - 1;

if (data.speciesList[idx].species === "human") {
schema.type = "null"; // no such property for humans
} else {
schema.type = "string";
if (data.speciesList[idx].species === "dog") {
schema.title = "Dog breed";
schema.enum = ["bulldog", "labrador"]
} else {
schema.title = "Cat breed";
schema.enum = ["siamese", "persian"]
}
}
schemas["$.speciesList[#].subspecies"] = schema;
setTimeout(function(){cb(schemas)},500); // in order to show asynchrony
};

var form = BrutusinForms.create(formSchema);
form.schemaResolver = schemaResolver;

form.render(document.getElementById("formDialogContainer"));

var dialog = $("#formDialog");
dialog.modal();
});
</script>
</html>
113 changes: 113 additions & 0 deletions examples/example2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../dist/css/brutusin-json-forms.min.css">

<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" charset="utf8" src="../dist/js/brutusin-json-forms.js"></script>
<script type="text/javascript" charset="utf8" src="../dist/js/brutusin-json-forms-bootstrap.min.js"></script>
</head>
<body>
<div id="formDialog" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left"></h4>
<div class="btn-toolbar pull-right formDialogControlsGroup">
<button type="button" class="btn btn-sm btn-warning" id="formDialogCancel" data-dismiss="modal">Cancel</button>
</div>
</div>
<div id="formDialogBody" class="modal-body">
<div id="formDialogContainer"></div>
</div>
</div>
</div>
</div>
</body>
<script>
$(document).ready(function() {
var formSchema = {
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"definitions": {
"name": {
"type": "string",
"title": "Name",
"required": true,
"pattern": "^[A-Za-z][0-9a-zA-Z-_ ]{7,}$",
"description": "Mandatory alphanumeric starting with an alphabet. Space, hyphen and underscore allowed. Min length: 8"
},
"options": {
"type": "string",
"title": "Options",
"required": true,
"enum": ["OPTION-1", "OPTION-2", "OPTION-3"],
"description": "Mandatory option"
},
"level2Array": {
"type": "array",
"title": "Level-2 Array",
"description": "List of Level-2 items",
"items": {
"type": "object",
"properties": {
"name": {
"$ref": "#/definitions/name"
},
},
},
},
},
"properties": {
"name": {
"$ref": "#/definitions/name"
},
"level1Array": {
"type": "array",
"title": "Level-1 Array",
"description": "List of Level-1 items",
"items": {
"type": "object",
"properties": {
"option": {
"$ref": "#/definitions/options"
},
"level2Array": {
"dependsOn": [ "option" ]
},
},
},
},
},
};

var BrutusinForms = brutusin["json-forms"];
var schemaResolver = function(names, data, cb) {
var schemas = new Object();
var schema = { "$ref": "#/definitions/level2Array" };
var optIdx;

if(data && data.level1Array && data.level1Array.length) {
optIdx = data.level1Array.length-1;
if(data.level1Array[optIdx].option !== "OPTION-1") {
schema = { type: "null" };
}
}

schemas["$.level1Array[#].level2Array"] = schema;
setTimeout(function(){cb(schemas)},500);
};

var form = BrutusinForms.create(formSchema);
form.schemaResolver = schemaResolver;

form.render(document.getElementById("formDialogContainer"));

var dialog = $("#formDialog");
dialog.modal();
});
</script>
</html>
16 changes: 6 additions & 10 deletions src/js/brutusin-json-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ if (typeof brutusin === "undefined") {
} else if (s.media) {
input = document.createElement("input");
input.type = "file";
appendChild(input, option, s);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addition of this line is unintended. I will remove this line from my version and resubmit the pull request.

// XXX TODO, encode the SOB properly.
} else if (s.enum) {
input = document.createElement("select");
Expand Down Expand Up @@ -204,7 +205,7 @@ if (typeof brutusin === "undefined") {
}
}
if (s.enum.length === 1)
input.selectedIndex = 0;
input.selectedIndex = 1;
Copy link
Contributor Author

@nagarajanchinnasamy nagarajanchinnasamy Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be:
input.selectedIndex = 0;

Will change and re-submit

else
input.selectedIndex = selectedIndex;
} else {
Expand All @@ -222,10 +223,6 @@ if (typeof brutusin === "undefined") {
// #46, problem in IE11. TODO polyfill?
input.type = "text";
}
} else if (s.format === "date") {
input.type = "date";
} else if (s.format === "time") {
input.type = "time";
} else if (s.format === "email") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deletion of above 4 lines unintended. Will add these in my version and re-submit the pull request.

input.type = "email";
} else if (s.format === "text") {
Expand Down Expand Up @@ -349,7 +346,7 @@ if (typeof brutusin === "undefined") {
input = document.createElement("input");
input.type = "checkbox";
if (value === true || value !== false && s.default) {
input.checked = true;
input.checked = true;
}
} else {
input = document.createElement("select");
Expand Down Expand Up @@ -750,7 +747,6 @@ if (typeof brutusin === "undefined") {
if (s.readOnly)
addButton.disabled = true;
addButton.setAttribute('type', 'button');
addButton.className = "addItem";
addButton.getValidationError = function () {
if (s.minItems && s.minItems > table.rows.length) {
return BrutusinForms.messages["minItems"].format(s.minItems);
Expand Down Expand Up @@ -831,7 +827,7 @@ if (typeof brutusin === "undefined") {

obj.getData = function () {
function removeEmptiesAndNulls(object, s) {
if (s === null) {
if (!s) {
s = SCHEMA_ANY;
}
if (s.$ref) {
Expand Down Expand Up @@ -1311,7 +1307,7 @@ if (typeof brutusin === "undefined") {

function cleanSchemaMap(schemaId) {
for (var prop in schemaMap) {
if (prop.startsWith(schemaId)) {
if (prop.startsWith(schemaId)) { // 02_AddLevel1Item_Delete_AgainAdd.png
delete schemaMap[prop];
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprisingly, in the origin itself, the code differs between src and dist!!!

src has this fix... but dist doesn't have the fix.

Expand Down Expand Up @@ -1413,7 +1409,7 @@ if (typeof brutusin === "undefined") {
throw ("Node '" + name + "' is of type array");
}
var element = currentToken.substring(1, currentToken.length - 1);
if (element.equals("#")) {
if (element === "#") {
for (var i = 0; i < data.length; i++) {
var child = data[i];
visit(name + currentToken, queue.slice(0), child, data, i);
Expand Down