Skip to content

Commit d9793e3

Browse files
committed
add input validation
1 parent 4d8cf79 commit d9793e3

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

Diff for: .brackets.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"smartIndent": true,
3+
"spaceUnits": 4,
4+
"useTabChar": false,
25
"language": {
36
"javascript": {
47
"linting.prefer": ["JSHint", "JSCS"],

Diff for: src/ZSchema.js

+12
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ ZSchema.prototype.validateSchema = function (schema) {
116116
return report.isValid();
117117
};
118118
ZSchema.prototype.validate = function (json, schema, callback) {
119+
var whatIs = Utils.whatIs(schema);
120+
if (whatIs !== "string" && whatIs !== "object") {
121+
var e = new Error("Invalid .validate call - schema must be an string or object but " + whatIs + " was passed!");
122+
if (callback) {
123+
process.nextTick(function () {
124+
callback(e, false);
125+
});
126+
return;
127+
}
128+
throw e;
129+
}
130+
119131
var report = new Report(this.options);
120132

121133
schema = SchemaCache.getSchema.call(this, report, schema);

Diff for: test/ZSchemaTestSuite/Issue101.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
module.exports = {
4+
description: "Issue #101 - Cannot read property '__$compiled' of undefined",
5+
async: true,
6+
options: {
7+
asyncTimeout: 500
8+
},
9+
schema: null,
10+
tests: [
11+
{
12+
description: "should fail with correct error",
13+
data: null,
14+
valid: false,
15+
after: function (err) {
16+
expect(err.message).toBe("Invalid .validate call - schema must be an string or object but null was passed!");
17+
}
18+
}
19+
]
20+
};

Diff for: test/spec/ZSchemaTestSuiteSpec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var testSuiteFiles = [
5454
require("../ZSchemaTestSuite/Issue94.js"),
5555
require("../ZSchemaTestSuite/Issue96.js"),
5656
require("../ZSchemaTestSuite/Issue98.js"),
57+
require("../ZSchemaTestSuite/Issue101.js"),
5758
undefined
5859
];
5960

@@ -66,8 +67,8 @@ describe("ZSchemaTestSuite", function () {
6667
}
6768
}
6869

69-
it("should contain 48 files", function () {
70-
expect(testSuiteFiles.length).toBe(48);
70+
it("should contain 49 files", function () {
71+
expect(testSuiteFiles.length).toBe(49);
7172
});
7273

7374
testSuiteFiles.forEach(function (testSuite) {

0 commit comments

Comments
 (0)