Skip to content

Instantly share code, notes, and snippets.

@koola
Created March 1, 2015 02:40
Show Gist options
  • Save koola/4b9c1700fbf66c032085 to your computer and use it in GitHub Desktop.
Save koola/4b9c1700fbf66c032085 to your computer and use it in GitHub Desktop.
Jasmine toMatchJSONSchema custom matcher
'use strict';
var validate = require('jsonschema').validate;
var customMatchers = {
toMatchJSONSchema: function (expected) {
var notText = this.isNot ? " not" : "";
this.message = function() {
return "Schema is" + notText + " valid"
};
return validate(this.actual, expected).valid
}
};
describe("Valid JSON Schema", function() {
beforeEach(function () {
this.addMatchers(customMatchers);
this.expectedSchema = {
"type": "object",
"properties": {
"dealership": {"type": "string"},
"motors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"brand": { "type": "string" },
"modal": { "type": "string" },
"year": { "type": "integer" }
}
}
}
}
};
this.response = {
"dealership": "Tianfu Rd",
"motors": [
{
"brand": "bmw",
"modal": "320i",
"year": 2014
},
{
"brand": "audi",
"modal": "q5",
"year": 2015
}
]
};
this.badResponse = {
"dealership": "Tianfu Rd",
"motors": [
{
"brand": "bmw",
"modal": "320i",
"year": "2014"
}
]
};
});
it("should validate simple schema", function() {
expect(1).toMatchJSONSchema({"type": "integer"});
});
it("should validate complex schema", function() {
expect(this.response).toMatchJSONSchema(this.expectedSchema);
});
it("should not match with malformed response", function() {
expect(this.badResponse).not.toMatchJSONSchema(this.expectedSchema);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment