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 // Add the id property
if (idInjection) { if (idInjection) {
ModelClass.prototype.__defineGetter__('id', function () { ModelClass.prototype.__defineGetter__('id', function () {
return this.__data.id; return this.__data.id;
}); });
// Set up the id property // 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) { if (!properties.id.id) {
properties.id.id = 1; properties.id.id = 1;
} }
@ -188,7 +189,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
} }
ModelClass.registerProperty = function (attr) { ModelClass.registerProperty = function (attr) {
var DataType = properties[attr].type; var prop = properties[attr];
var DataType = prop.type;
if(!DataType) { if(!DataType) {
throw new Error('Invalid type for property ' + attr); 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') { } else if(typeof DataType === 'string') {
DataType = dataSource.getSchemaType(DataType); 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, { Object.defineProperty(ModelClass.prototype, attr, {
get: function () { get: function () {