             else
               @unset()
 
-        regEx: SimpleSchema.RegEx.Id
+        optional: true
+        regEx: new RegExp "(" + SimpleSchema.RegEx.Id.source + ")|^0$"
         type: String
 
     if updatedAt

    if createdBy
      definition[createdBy] =
        autoValue: ->
          unless @isFromTrustedCode
            if @isInsert
              @userId
            else if @isUpsert


        denyInsert: true
        optional: true
        regEx: new RegExp "(" + SimpleSchema.RegEx.Id.source + ")|^0$"
        type: String

    @attachSchema new SimpleSchema definition

  else
    if Meteor.isServer
      @before.insert (userId, doc) ->
        if createdAt
          doc[createdAt] = new Date
        if createdBy and not doc.createdBy?
          userId ?= '0'
          doc[createdBy] = userId

      @before.update (userId, doc, fieldNames, modifier, options) ->
        $set = modifier.$set

        unless $set
          $set = {}

        if updatedAt
          $set[updatedAt] = new Date
        if updatedBy and not doc.updatedBy?
          userId ?= '0'
          $set[updatedBy] = userId

CollectionBehaviour.define 'timestampable', behaviour
