Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ const { argv } = require('yargs')
const ViewSchemaValidator = require('./lib');
const packageJson = require('./package.json');

const mapErrorToLogLine = (errorItem, index) => {
if(typeof errorItem === 'string')
return errorItem;

const message = errorItem && (errorItem.message || errorItem.stack);

if(message)
return `[${index + 1}] ${message}`;

return `[${index + 1}] ${JSON.stringify(errorItem)}`;
};

(async () => {

logger.info(`Package version: ${packageJson.version}`);
Expand Down Expand Up @@ -86,8 +98,11 @@ const packageJson = require('./package.json');
} catch(error) {
logger.error(error.stack || error);

if(error.errors)
error.errors.map(e => logger.error(e));
if(error.errors) {
error.errors
.map(mapErrorToLogLine)
.map(errorLine => logger.error(errorLine));
}

process.exit(1);
}
Expand Down
14 changes: 12 additions & 2 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const DeprecationValidator = require('./deprecation-validator');
const ajv = new Ajv({ allErrors: true, useDefaults: true, schemas: [schemaDefinitions] });

class Validator {
static formatAjvErrors(errors = []) {
return errors.map((currentError, index) => {
const path = currentError.dataPath || currentError.instancePath || '(root)';
const message = currentError.message || 'validation failed';
const keyword = currentError.keyword || 'unknown';

return `[${index + 1}] ${path} ${message} (${keyword})`;
});
}

/**
* Take ajv instance and call compile function for validate and generate schema with defaults
* @name compile
Expand Down Expand Up @@ -52,9 +62,9 @@ class Validator {

this.validate(schema);

if(ajv.errors) {
if(ajv.errors && ajv.errors.length) {
const error = new Error(`Validation error in ${filePath}`);
error.errors = ajv.errors;
error.errors = this.formatAjvErrors(ajv.errors);
throw error;
}

Expand Down
Loading