Last active
February 29, 2016 23:03
-
-
Save dniccum/aa4cc537ded4b5a2532c to your computer and use it in GitHub Desktop.
A custom error reporting workflow for SailsJS that uses AngularJS and the NodeJS package sails-hook-validation
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
app = angular.module('sample'); | |
app.factory('ErrorAPI', [function() { | |
return { | |
ParseErrors: function(errors) { | |
if (Array.isArray(errors.data.err)) { | |
for (var item in errors.data.err) { | |
console.log(errors.data.err[item], 'Error'); | |
} | |
} else if (errors.data.err) { | |
console.log(errors.data.err, 'Error'); | |
} else { | |
console.log(errors.data, 'Error'); | |
} | |
} | |
} | |
}]); |
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
module.exports = { | |
ParseUserErrors: function(errors) { | |
var validationErrors = []; | |
for (var key in errors) { | |
if (errors.hasOwnProperty(key)) { | |
for (var item in errors[key]) { | |
if (errors[key][item].rule !== "string") { | |
validationErrors.push(errors[key][item].message); | |
} | |
} | |
} | |
} | |
return validationErrors; | |
} | |
}; |
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
app = angular.module('sample'); | |
app.controller('userController', [function() { | |
$scope.formData = {}; | |
$scope.formSubmit = function() { | |
$http({ | |
method: 'POST', | |
url: '/api/user', | |
withCredentials: true, | |
data: { | |
firstName: $scope.formData.firstName, | |
lastName: $scope.formData.lastName, | |
email: $scope.formData.email, | |
password: $scope.formData.password | |
} | |
}).then(function successCallback(response) { | |
console.log('success',response) | |
}, function errorCallback(errorResponse) { | |
console.log('error',errorResponse); | |
ErrorAPI.ParseErrors(errorResponse); | |
}); | |
}; | |
}]); |
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
module.exports = { | |
attributes: { | |
firstName: { | |
type: 'string', | |
required: true, | |
minLength: 2, | |
maxLength: 30 | |
}, | |
lastName: { | |
type: 'string', | |
required: true, | |
minLength: 2, | |
maxLength: 30 | |
}, | |
email: { | |
type: 'email', | |
required: true, | |
unique: true, | |
maxLength: 40 | |
}, | |
password: { | |
type: 'string', | |
required: true | |
} | |
}, | |
validationMessages: { | |
firstName: { | |
required: "'First Name' should be a name and is required.", | |
minLength: 'The name that was provided was not long enough.', | |
maxLength: 'The name that was provided was too long.' | |
}, | |
lastName: { | |
required: "'Last Name' should be a name and is required.", | |
minLength: 'The name that was provided was not long enough.', | |
maxLength: 'The name that was provided was too long.' | |
}, | |
email: { | |
required: "'Email' is required.", | |
type: 'This is not a valid email address.', | |
maxLength: 'The email address that was provided is too long.' | |
} | |
} | |
}; |
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
module.exports = { | |
create: function (req, res) { | |
User.create({ | |
firstName: req.param('firstName'), | |
lastName: req.param('lastName'), | |
email: req.param('email'), | |
password: req.param('password') | |
}, function userCreated(err, newUser) { | |
if (err) { | |
// err.Errors is the custom array that sails-hook-validation adds | |
if (err && err.Errors) { | |
var parsedErrors = ErrorService.ParseUserErrors(err.Errors); | |
return res.badRequest({err : parsedErrors}); | |
} | |
return res.negotiate(err); | |
} | |
return res.ok(newUser); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment