lib/factory.js
function (Class, descriptors, staticDescriptors) {
    var casters = {},
        validators = {},
        body = '"use strict";\n\
                var valid = true,\n\
                    errors = {},\n\
                    result;\n\
                if (values === undefined) {\n\
                  values = subject || {};\n\
                  subject = new this();\n\
                }\n';

    each(descriptors, function (descriptor, key) {
      if (key === '[[state]]' ||
          key === 'initialize' ||
          (!descriptor.writable && typeof descriptor.set !== 'function') ||
          typeof descriptor.value === 'function'
      ) {
        return;
      }

      var accessor = createAccessor(key);
      body += 'if (values'+accessor+' !== undefined) {\n';
      if (descriptor.type) {
        casters[key] = Casting.get(descriptor.type);
        body += 'if (values'+accessor+' === null) {\n\
                   subject'+accessor+' = null;\n\
                 }\n\
                 else {\n\
                  result = tryCatch(casters'+accessor+', values'+accessor+');\n\
                  if (result.error) {\n\
                    valid = false;\n\
                    errors'+accessor+' = result.error.message;\n\
                  }\n\
                  else {\n\
                    subject'+accessor+' = result.value;\n\
                  }\n\
                }\n';
      }
      else {
        body += 'subject'+accessor+' = values'+accessor+';\n';
      }
      body += '}\n';
      if (descriptor.rules) {
        validators[key] = Validating.forDescriptor(key, descriptor);
        body += 'if (!errors'+accessor+') {\n\
                  result = validators'+accessor+'(subject'+accessor+');\n\
                  if (!result.valid) {\n\
                    valid = false;\n\
                    errors'+accessor+' = result.error;\n\
                  }\n\
                }\n';
      }
    });

    body += 'return {\n\
              valid: valid,\n\
              value: subject,\n\
              errors: errors\n\
            };';

    var input = this.createDynamicFunction('validators', 'casters', 'tryCatch', 'subject', 'values', body);

    var fn = function (subject, values) {
      return input.call(this, validators, casters, tryCatch1, subject, values);
    };
    fn.isAutoGenerated = true;
    return fn;
  }
function (Class, descriptors, staticDescriptors) {
    var casters = {},
        validators = {},
        body = '"use strict";\n\
                var valid = true,\n\
                    errors = {},\n\
                    result;\n\
                if (values === undefined) {\n\
                  values = subject || {};\n\
                  subject = new this();\n\
                }\n';

    each(descriptors, function (descriptor, key) {
      if (key === '[[state]]' ||
          key === 'initialize' ||
          (!descriptor.writable && typeof descriptor.set !== 'function') ||
          typeof descriptor.value === 'function'
      ) {
        return;
      }

      var accessor = createAccessor(key);
      body += 'if (values'+accessor+' !== undefined) {\n';
      if (descriptor.type) {
        casters[key] = Casting.get(descriptor.type);
        body += 'if (values'+accessor+' === null) {\n\
                   subject'+accessor+' = null;\n\
                 }\n\
                 else {\n\
                  result = tryCatch(casters'+accessor+', values'+accessor+');\n\
                  if (result.error) {\n\
                    valid = false;\n\
                    errors'+accessor+' = result.error.message;\n\
                  }\n\
                  else {\n\
                    subject'+accessor+' = result.value;\n\
                  }\n\
                }\n';
      }
      else {
        body += 'subject'+accessor+' = values'+accessor+';\n';
      }
      body += '}\n';
      if (descriptor.rules) {
        validators[key] = Validating.forDescriptor(key, descriptor);
        body += 'if (!errors'+accessor+') {\n\
                  result = validators'+accessor+'(subject'+accessor+');\n\
                  if (!result.valid) {\n\
                    valid = false;\n\
                    errors'+accessor+' = result.error;\n\
                  }\n\
                }\n';
      }
    });

    body += 'return {\n\
              valid: valid,\n\
              value: subject,\n\
              errors: errors\n\
            };';

    var input = this.createDynamicFunction('validators', 'casters', 'tryCatch', 'subject', 'values', body);

    var fn = function (subject, values) {
      return input.call(this, validators, casters, tryCatch1, subject, values);
    };
    fn.isAutoGenerated = true;
    return fn;
  }
function ( Class , descriptors , staticDescriptors ) { var casters = { } , validators = { } , body = '"use strict";\n\
                var valid = true,\n\
                    errors = {},\n\
                    result;\n\
                if (values === undefined) {\n\
                  values = subject || {};\n\
                  subject = new this();\n\
                }\n' ; each ( descriptors , function ( descriptor , key ) { if ( key === '[[state]]' || key === 'initialize' || ( ! descriptor . writable && typeof descriptor . set !== 'function' ) || typeof descriptor . value === 'function' ) { return ; } var accessor = createAccessor ( key ) ; body += 'if (values' + accessor + ' !== undefined) {\n' ; if ( descriptor . type ) { casters [ key ] = Casting . get ( descriptor . type ) ; body += 'if (values' + accessor + ' === null) {\n\
                   subject' + accessor + ' = null;\n\
                 }\n\
                 else {\n\
                  result = tryCatch(casters' + accessor + ', values' + accessor + ');\n\
                  if (result.error) {\n\
                    valid = false;\n\
                    errors' + accessor + ' = result.error.message;\n\
                  }\n\
                  else {\n\
                    subject' + accessor + ' = result.value;\n\
                  }\n\
                }\n' ; } else { body += 'subject' + accessor + ' = values' + accessor + ';\n' ; } body += '}\n' ; if ( descriptor . rules ) { validators [ key ] = Validating . forDescriptor ( key , descriptor ) ; body += 'if (!errors' + accessor + ') {\n\
                  result = validators' + accessor + '(subject' + accessor + ');\n\
                  if (!result.valid) {\n\
                    valid = false;\n\
                    errors' + accessor + ' = result.error;\n\
                  }\n\
                }\n' ; } } ) ; body += 'return {\n\
              valid: valid,\n\
              value: subject,\n\
              errors: errors\n\
            };' ; var input = this . createDynamicFunction ( 'validators' , 'casters' , 'tryCatch' , 'subject' , 'values' , body ) ; var fn = function ( subject , values ) { return input . call ( this , validators , casters , tryCatch1 , subject , values ) ; } ; fn . isAutoGenerated = true ; return fn ; }
Create a function which can accept user input for the model.

@param  {Function} Class             The class.
@param  {Object}   descriptors       The property descriptors.
@param  {Object}   staticDescriptors The static property descriptors.

@return {Function} The generated function.
Create a function which can accept user input for the model .