Created
January 11, 2014 02:09
-
-
Save mikermcneil/8366092 to your computer and use it in GitHub Desktop.
hack to determine whether an error is a validation error from waterline (for Sails/Waterline 0.9.x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Note that in Sails v1 this is changing a bit. For details, see the docs on sailsjs.com (or balderdashy/sails#3459 (comment) for a bit more background)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More compact solution