Skip to content

Commit 14e8540

Browse files
committed
Allow for nested definitions. Fixes #127
1 parent aa41bfa commit 14e8540

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

src/validator.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ JSONEditor.Validator = Class.extend({
3030
});
3131
});
3232
},
33-
_getRefs: function(schema,callback) {
33+
_getRefs: function(schema,callback,path,in_definitions) {
3434
var self = this;
3535
var is_root = schema === this.original_schema;
36+
path = path || "#";
3637

3738
var waiting, finished, check_if_finished, called;
3839

3940
// Work on a deep copy of the schema
4041
schema = $extend({},schema);
4142

4243
// First expand out any definition in the root node
43-
if(is_root && schema.definitions) {
44+
if(schema.definitions && (is_root || in_definitions)) {
4445
var defs = schema.definitions;
4546
delete schema.definitions;
4647

@@ -49,7 +50,7 @@ JSONEditor.Validator = Class.extend({
4950
if(finished >= waiting) {
5051
if(called) return;
5152
called = true;
52-
self._getRefs(schema,callback);
53+
self._getRefs(schema,callback,path);
5354
}
5455
};
5556

@@ -61,10 +62,10 @@ JSONEditor.Validator = Class.extend({
6162
$each(defs,function(i,definition) {
6263
// Expand the definition recursively
6364
self._getRefs(definition,function(def_schema) {
64-
self.refs['#/definitions/'+i] = def_schema;
65+
self.refs[path+'/definitions/'+i] = def_schema;
6566
finished++;
6667
check_if_finished(schema);
67-
});
68+
},path+'/definitions/'+i,true);
6869
});
6970
}
7071
else {
@@ -111,7 +112,7 @@ JSONEditor.Validator = Class.extend({
111112
$each(list,function(i,v) {
112113
v();
113114
});
114-
});
115+
},path);
115116
return;
116117
}
117118

@@ -159,7 +160,7 @@ JSONEditor.Validator = Class.extend({
159160

160161
finished++;
161162
check_if_finished(schema);
162-
});
163+
},path+'/'+key+'/'+j);
163164
}
164165
});
165166
}
@@ -170,7 +171,7 @@ JSONEditor.Validator = Class.extend({
170171

171172
finished++;
172173
check_if_finished(schema);
173-
});
174+
},path+'/'+key);
174175
}
175176
});
176177
}

tests/validation.html

+54
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<script src='../dist/jsoneditor.js'></script>
88
</head>
99
<body>
10+
<div id='output'></div>
1011
<script>
1112
var tests = {
1213
required3: {
@@ -566,6 +567,56 @@
566567
}
567568
]
568569
},
570+
definitions_nested: {
571+
schema: {
572+
type: "object",
573+
properties: {
574+
id: {
575+
$ref: "#/definitions/app/definitions/id"
576+
},
577+
definitions: {
578+
type: "number"
579+
},
580+
test: {
581+
$ref: "#/definitions/test"
582+
}
583+
},
584+
definitions: {
585+
app: {
586+
definitions: {
587+
id: {
588+
type: "string"
589+
}
590+
}
591+
},
592+
test: {
593+
properties: {
594+
definitions: {
595+
type: "number"
596+
}
597+
}
598+
}
599+
}
600+
},
601+
valid: [
602+
{
603+
id: "test",
604+
definitions: 3,
605+
test: {definitions: 3}
606+
}
607+
],
608+
invalid: [
609+
{
610+
id: 1
611+
},
612+
{
613+
definitions: "string"
614+
},
615+
{
616+
test: {definitions: "string"}
617+
}
618+
]
619+
},
569620
$ref: {
570621
schema: {
571622
type: "object",
@@ -698,6 +749,7 @@
698749
var result = self.validate(value);
699750
if(result.length) {
700751
console.error(num,'valid',j,JSON.stringify(result,null,2));
752+
$("#output").append("<div><strong>"+i+" test "+j+"</strong>:: Expected: [], Actual: "+JSON.stringify(result)+"</div>");
701753
}
702754
else {
703755
console.log(num,'valid',j);
@@ -708,6 +760,8 @@
708760
var result = self.validate(value);
709761
if(!result.length) {
710762
console.error(num,'invalid',j,JSON.stringify(result,null,2));
763+
$("#output").append("<div><strong>"+i+" test "+j+"</strong>:: Expected: errors, Actual: []</div>");
764+
711765
}
712766
else {
713767
var errors = [];

0 commit comments

Comments
 (0)