Skip to content

Instantly share code, notes, and snippets.

@johnnncodes
Forked from mikermcneil/isValidationError.js
Created February 12, 2014 01:08
Show Gist options
  • Save johnnncodes/8947941 to your computer and use it in GitHub Desktop.
Save johnnncodes/8947941 to your computer and use it in GitHub Desktop.
var _ = require('lodash');
/**
* `isValidationError`
*
* Is this a waterline validation error?
*/
function isWaterlineValidationError (err) {
if (_.isPlainObject(err)) {
var keys = Object.keys(err);
if (keys.length) {
var failedValidation = err[keys[0]];
if (_.isArray(failedValidation) && failedValidation.length &&
_.isPlainObject(failedValidation[0]) && failedValidation[0]['rule']
) {
return true;
}
}
}
if ( _.isString(err) && err.match(/duplicate key value violates unique constraint/g) ) {
return true;
}
if ( _.isString(err) && err.match(/^Bad request/ig)) {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment