Say I'm using a Repository pattern for data access as below:
#userController.coffee
# `userId` is obtained from the session
user =
email: 'Bob'
password: 'Secret'
db.userRepo(@userId).create user, (err, data) =>
# return results in http response or socket.io
This is what can go wrong during this method invocation:
user
has validation errors such as missing fields, etc.user.email
.Options for callback arguments:
(err, data)
- where err
is an array of all errors encountered.(err, data)
- where err
is validation errors and database errors are thrown as exceptions.(err, data)
- same as above except when the user
already exists it returns null, because this is not an error but expected behaviour.(err, data, validation)
- where validation
is an array of validation errors or null
.(err, data, model)
- returning a model class with a validation property - active-record style.Feel free to propose something different.
I'm planning on using a combination of node-validator and revalidator to validate objects and arguments.
Backstory: I have just moved over from Scala/Play to Node/Express for reasons of larger community, good web-sockets support, and developer productivity. After starting out with the Sequelize ORM I decided it was too constraining, had trouble with creating joins and my schema was simple anyway, so I started writing raw SQL. After an initial boost in development speed, I'm finding myself wanting static-typing back everyday. The number of tests and validation code I am writing is significantly larger.
I'd recommend (err, data, validation)
so you can easily check for invalid input, which the user needs to re-enter, and actual DB errors err
, which could print a pretty error message while writing details to a log file during testing & prototyping.