Add required validation

This commit is contained in:
Ritchie Martori 2013-07-28 13:17:12 -07:00
parent 024a6cb52f
commit 08505d1e78
1 changed files with 9 additions and 2 deletions

View File

@ -141,12 +141,13 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
}
// Add the id property
if (idInjection) {
ModelClass.prototype.__defineGetter__('id', function () {
return this.__data.id;
});
// Set up the id property
properties.id = properties.id || { type: Number, id: 1 };
properties.id = properties.id || { type: Number, id: 1, generated: true };
if (!properties.id.id) {
properties.id.id = 1;
}
@ -188,7 +189,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
}
ModelClass.registerProperty = function (attr) {
var DataType = properties[attr].type;
var prop = properties[attr];
var DataType = prop.type;
if(!DataType) {
throw new Error('Invalid type for property ' + attr);
}
@ -202,6 +204,11 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
} else if(typeof DataType === 'string') {
DataType = dataSource.getSchemaType(DataType);
}
if(prop.required) {
var requiredOptions = typeof prop.required === 'object' ? prop.required : undefined;
ModelClass.validatesPresenceOf(attr, requiredOptions);
}
Object.defineProperty(ModelClass.prototype, attr, {
get: function () {