       error.errors._base = 'max'
       next error
 
+# unique name
+TeamSchema.pre 'save', (next) ->
+  return next() unless @isNew
+  Team.uniqueName @name, (err, yeah) =>
+    return next err if err
+    if yeah
+      next()
+    else
+      error = new mongoose.Document.ValidationError this
+      error.errors.name = 'unique'
+      next error
+
 # create invites
 TeamSchema.pre 'save', (next) ->
   for email in @emails

  name:
    type: String
    required: true
  emails:
    type: [ mongoose.SchemaTypes.Email ]
    validate: [ ((v) -> v.length <= 4), 'max' ]

    max = 225 + 1 # +1 because team fortnight labs doesn't count
    next null, count < max, max - count

TeamSchema.static 'uniqueName', (name, next) ->
  Team.count { name: name }, (err, count) ->
    return next err if err
    next null, !count

# min people validation
TeamSchema.pre 'save', (next) ->
  if @people_ids.length + @emails.length == 0
