toggl-webhook
Version:
Wrapper for toggl webhook api v1
34 lines (33 loc) • 1.05 kB
JavaScript
import * as Validations from './schemas/validations.js';
export class ValidationError extends Error {
constructor(message, errors) {
super(message);
Object.defineProperty(this, "message", {
enumerable: true,
configurable: true,
writable: true,
value: message
});
Object.defineProperty(this, "errors", {
enumerable: true,
configurable: true,
writable: true,
value: errors
});
}
}
export default (schemaId, data, throwErros = true) => {
const validationKeys = Object.keys(Validations);
let result = true;
let errors = [];
if (validationKeys.includes(schemaId)) {
const validation = Validations[schemaId];
result = validation(data);
if (result === false)
errors = validation.errors || [];
}
if (result === false && throwErros === true) {
throw new ValidationError('Invalid param passed', errors);
}
return { result, errors };
};