/** * Validate Resource JSON against a schema * @param object Object to be validated * @param schema JSON schema to be used for validation */ validateJSON (object, schema) { let ajv = new Ajv({ allErrors: true }); if (ajv.validate(schema, object)) { return; } let validationErrors = ajv.errors.map(function (error) { return { message: error.message, path: error.dataPath }; }); let validationError = new HttpError( HttpError.statusCodes.UNPROCESSABLE_ENTITY, this.Errors.JSON_IS_INVALID + JSON.stringify(schema) ); validationError.causes = validationErrors; throw validationError; }
ajv.errors.forEach(function(error) { var message = ''; switch(error.keyword) { case 'required': // requirement not fulfilled. message = 'Property \'' + error.params.missingProperty + '\' is missing.'; break; case 'type': message = 'Wrong type: ' + error.dataPath + ' ' + error.message; break; default: message = 'Unknown input error. :('; } var pusherror = { message: message } errors.push(pusherror); });
let errText = '' validation.errors.forEach((err) => { errText += `\n${err.dataPath} ${err.message}: ${JSON.stringify(err.params)}\n` }) throw new Error(errText)