Skip to content

Commit

Permalink
Feature/custom schema for validation (#378)
Browse files Browse the repository at this point in the history
* Added arg option '--scheme'

* Implemented 'getSchema' function, that looks for a specified custom schema

* Added try catch clause, and reduced lines of code. Fixed a typo

Co-authored-by: rick0335 <[email protected]>
  • Loading branch information
rickifunk and rick0335 authored Aug 17, 2020
1 parent 62cf811 commit 3ac2ea8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ lib.preFlow(async (err, results) => {
'-d, --dir <path>',
'Used by `serve` to indicate a public directory path.',
'public',
)
.option(
'--schema <relativePath>',
'Used by `test` to validate against a custom schema.',
);

program
Expand Down
19 changes: 18 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@ const promisify = require('util.promisify');
const ZSchema = require('z-schema');
const ZSchemaErrors = require('z-schema-errors');

const path = require('path');
const fs = require('fs');
const getSchema = function () {
const index = process.argv.findIndex((value) => value === '--schema');
if (index === -1) {
return schema;
} // Return the default schema, if a custom schema wasn't specified

try {
const customSchemaPath = path.join(process.cwd(), process.argv[index + 1]);
return JSON.parse(fs.readFileSync(customSchemaPath, { encoding: 'utf-8' }));
} catch (error) {
console.log(error.message);
process.exit();
}
};

const reporter = ZSchemaErrors.init();
const validator = new ZSchema();
const validate = promisify((obj, ...args) =>
validator.validate(obj, schema, ...args),
validator.validate(obj, getSchema(), ...args),
);
module.exports = async (resume) => {
try {
Expand Down

0 comments on commit 3ac2ea8

Please sign in to comment.