Skip to content

Commit

Permalink
feat: api validation by rules in apikana-defaults
Browse files Browse the repository at this point in the history
Uses validatorjs to validate rules defined in apikana-defaults (under 'validation').
See the validatorjs documentation on how to define such rules.
The main intention behind this feature was to enforce the presence of
additional properties in an OpenAPI specification.
  • Loading branch information
Finley Thomalla committed May 22, 2024
1 parent d0e80fa commit dcbf278
Show file tree
Hide file tree
Showing 3 changed files with 441 additions and 5,120 deletions.
19 changes: 19 additions & 0 deletions bin/apikana
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ function generate() {
var plop = nodePlop(__dirname + '/../src/plopfile_start.js', { defaults });
var generator = plop.getGenerator('start');
const model = Object.assign({}, packageJSON, { api: openapi });

// Workaround for validatorjs to fix wildcard on `paths`
const paths = [];
Object.values(model.api.paths).forEach(path => paths.push(path));
model.api.paths = paths;

if (defaults.validation) {
const Validator = require('validatorjs')
const validator = new Validator(model.api, defaults.validation);
if(!validator.check()) {
log(colors.bold(colors.red('Validation of the OpenAPI definition has failed with ' + validator.errorCount + ' errors')));
Object.entries(validator.errors.all()).forEach(errorEntry => {
const [errorField, error] = errorEntry;
log(colors.bold(colors.red(errorField + ": " + error)))
});
process.exit(1);
}
}

generator.runActions(model).then(_ => {
require('../src/generate').generate(path.resolve(source), params.target());
});
Expand Down
Loading

0 comments on commit dcbf278

Please sign in to comment.