-
Notifications
You must be signed in to change notification settings - Fork 107
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
Add schema validation support for tests #215
Changes from 5 commits
5f44064
b1183ef
0acaf99
bf1096a
7abc00e
058ea93
4a8bf61
14fb10e
fa5d0b5
8212d1f
048058d
9f4bd27
2dbec89
89677f9
51d8a7e
935e8a7
dd2c48e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,7 @@ | |
"webpack-node-externals": "^1.5.4" | ||
}, | ||
"devDependencies": { | ||
"ajv": "^5.5.2", | ||
"ava": "^0.25.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
'use strict'; | ||
|
||
const Ajv = require('ajv'); | ||
const crypto = require('crypto'); | ||
const url = require('url'); | ||
const aws = require('./aws'); | ||
const { readFile } = require('fs'); | ||
|
||
/** | ||
* Generate a 40-character random string | ||
|
@@ -107,3 +109,83 @@ async function createQueue() { | |
return url.format(returnedQueueUrl); | ||
} | ||
exports.createQueue = createQueue; | ||
|
||
/** | ||
* Read a file and return a promise with the data | ||
* | ||
* Takes the same parameters as fs.readFile: | ||
* | ||
* https://nodejs.org/docs/v6.10.3/api/fs.html#fs_fs_readfile_file_options_callback | ||
* | ||
* @param {string|Buffer|integer} file - filename or file descriptor | ||
* @param {any} options - encoding and flag options | ||
* @returns {Promise} - the contents of the file | ||
*/ | ||
function promisedReadFile(file, options) { | ||
return new Promise((resolve, reject) => { | ||
readFile(file, options, (err, data) => { | ||
if (err) reject(err); | ||
else resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Validate an object using json-schema | ||
* | ||
* Issues a test failure if there were validation errors | ||
* | ||
* @param {Object} t - an ava test | ||
* @param {string} schemaFilename - the filename of the schema | ||
* @param {Object} data - the object to be validated | ||
* @returns {boolean} - whether the object is valid or not | ||
*/ | ||
async function validateJSON(t, schemaFilename, data) { | ||
const schema = await promisedReadFile(schemaFilename, 'utf8').then(JSON.parse); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the schema file does not exist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assumption is that you would only add this check to your test if the schema file exists. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice to just comment the expected behavior or spit out an error in that case. I don't love that the schema locations are assumed, but think it's fine for now and we can make updates if we run into a different scenario. |
||
const ajv = new Ajv(); | ||
const valid = (new Ajv()).validate(schema, data); | ||
if (!valid) t.fail(`input validation failed: ${ajv.errorsText()}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return valid; | ||
} | ||
|
||
/** | ||
* Validate a task input object using json-schema | ||
* | ||
* Issues a test failure if there were validation errors | ||
* | ||
* @param {Object} t - an ava test | ||
* @param {Object} data - the object to be validated | ||
* @returns {boolean} - whether the object is valid or not | ||
*/ | ||
async function validateInput(t, data) { | ||
return validateJSON(t, './schemas/input.json', data); | ||
} | ||
exports.validateInput = validateInput; | ||
|
||
/** | ||
* Validate a task config object using json-schema | ||
* | ||
* Issues a test failure if there were validation errors | ||
* | ||
* @param {Object} t - an ava test | ||
* @param {Object} data - the object to be validated | ||
* @returns {boolean} - whether the object is valid or not | ||
*/ | ||
async function validateConfig(t, data) { | ||
return validateJSON(t, './schemas/config.json', data); | ||
} | ||
exports.validateConfig = validateConfig; | ||
|
||
/** | ||
* Validate a task output object using json-schema | ||
* | ||
* Issues a test failure if there were validation errors | ||
* | ||
* @param {Object} t - an ava test | ||
* @param {Object} data - the object to be validated | ||
* @returns {boolean} - whether the object is valid or not | ||
*/ | ||
async function validateOutput(t, data) { | ||
return validateJSON(t, './schemas/input.json', data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be output.json There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jinx |
||
} | ||
exports.validateOutput = validateOutput; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also require the
host
?