Validate this dao's attribute values according to validation rules set in the dao definition.
@return: {null} if and only if validation successful; otherwise an object containing { field name : [error msgs] } entries.
DAO.prototype.validate = function() {
var self = this
var failures = {}
// for each field and value
Utils._.each(self.values, function(value, field) {
// if field has validators
if (self.validators.hasOwnProperty(field)) {
// for each validator
Utils._.each(self.validators[field], function(details, validatorType) {
var is_custom_fn = false // if true then it's a custom validation method
var fn_method = null // the validation function to call
var fn_args = [] // extra arguments to pass to validation function
var fn_msg = "" // the error message to return if validation fails
// is it a custom validator function?
if (Utils._.isFunction(details)) {
is_custom_fn = true
fn_method = Utils._.bind(details, self, value)
}
// is it a validator module function?
else {
// extra args
fn_args = details.hasOwnProperty("args") ? details.args : []
if (!Utils._.isArray(fn_args))
fn_args = [fn_args]
// error msg
fn_msg = details.hasOwnProperty("msg") ? details.msg : false
// check method exists
var v = Validator.check(value, fn_msg)
if (!Utils._.isFunction(v[validatorType]))
throw new Error("Invalid validator function: " + validatorType)
// bind to validator obj
fn_method = Utils._.bind(v[validatorType], v)
}
try {
fn_method.apply(null, fn_args)
} catch (err) {
err = err.message
// if we didn't provide a custom error message then augment the default one returned by the validator
if (!fn_msg && !is_custom_fn)
err += ": " + field
// each field can have multiple validation failures stored against it
if (failures.hasOwnProperty(field)) {
failures[field].push(err)
} else {
failures[field] = [err]
}
}
}) // for each validator for this field
} // if field has validator set
}) // for each field
return (Utils._.isEmpty(failures) ? null : failures)
}
DAO.prototype.updateAttributes = function(updates) {
this.setAttributes(updates)
return this.save()
}
DAO.prototype.setAttributes = function(updates) {
var self = this
var readOnlyAttributes = Utils._.keys(this.__factory.primaryKeys)
readOnlyAttributes.push('id')
readOnlyAttributes.push('createdAt')
readOnlyAttributes.push('updatedAt')
readOnlyAttributes.push('deletedAt')
Utils._.each(updates, function(value, attr) {
var updateAllowed = (
(readOnlyAttributes.indexOf(attr) == -1) &&
(readOnlyAttributes.indexOf(Utils._.underscored(attr)) == -1) &&
(self.attributes.indexOf(attr) > -1)
)
updateAllowed && (self[attr] = value)
})
}
DAO.prototype.destroy = function() {
if(this.__options.timestamps && this.__options.paranoid) {
var attr = this.__options.underscored ? 'deleted_at' : 'deletedAt'
this[attr] = new Date()
return this.save()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return this.QueryInterface.delete(this, this.__factory.tableName, identifier)
}
}
DAO.prototype.equals = function(other) {
var result = true
, self = this
Utils._.each(this.values, function(value, key) {
result = result && (value == other[key])
})
return result
}
DAO.prototype.equalsOneOf = function(others) {
var result = false
, self = this
others.forEach(function(other) { result = result || self.equals(other) })
return result
}
DAO.prototype.addAttribute = function(attribute, value) {
this[attribute] = value
this.attributes.push(attribute)
}
DAO.prototype.setValidators = function(attribute, validators) {
this.validators[attribute] = validators
}
DAO.prototype.toJSON = function() {
return this.values;
}
// private
var initAttributes = function(values) {
// add all passed values to the dao and store the attribute names in this.attributes
for (var key in values) {
if (values.hasOwnProperty(key)) {
this.addAttribute(key, values[key])
}
}
// set id to null if not passed as value
// a newly created dao has no id
var defaults = this.__options.hasPrimaryKeys ? {} : { id: null }
if(this.__options.timestamps) {
defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date()
defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date()
if(this.__options.paranoid) {
defaults[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = null
}
}
for (var attr in defaults) {
var value = defaults[attr]
if(!this.hasOwnProperty(attr)) {
this.addAttribute(attr, Utils.toDefaultValue(value))
}
}
}
Add the instance methods to DAO
Utils._.extend(DAO.prototype, Mixin.prototype)
return DAO
})()
validate
DAO.prototype.validate()
Validate this dao's attribute values according to validation rules set in the dao definition.