Created
August 31, 2010 03:53
-
-
Save gsmcwhirter/558518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function (newDoc, oldDoc, cdbuser) | |
{ | |
var email_regex = new RegExp('^([a-zA-Z0-9_\\-.]+)@(([a-zA-Z0-9\\-]+\\.)+)([a-zA-Z]{2,9})$'); | |
function require(beTrue) | |
{ | |
if(typeof(beTrue) == 'array') | |
{ | |
for(testidx in beTrue){ | |
var test = beTrue[testidx]; | |
if (!test.test) throw({forbidden: test.msg}); | |
} | |
} | |
else | |
{ | |
if (!beTrue.test) throw({forbidden: beTrue.msg}); | |
} | |
} | |
function keys(a) | |
{ | |
var ret = []; | |
if (typeof(a) == 'object') | |
{ | |
for (idx in a) | |
{ | |
ret.push(idx); | |
} | |
} | |
return ret; | |
} | |
function equals(a, b) | |
{ | |
if(typeof(a) == 'object' && typeof(b) == 'object') | |
{ | |
if (keys(a).length != keys(b).length) | |
{ | |
return false; | |
} | |
for (idx in a) | |
{ | |
if (!equals(a[idx], b[idx])) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
else | |
{ | |
return a == b; | |
} | |
} | |
function preserve_history(newer, older) | |
{ | |
for (idx in older) | |
{ | |
if (!equals(newer[idx], older[idx])) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
if (newDoc.type == 'player') | |
{ | |
require([ | |
{test: (!oldDoc || oldDoc.type == newDoc.type), | |
msg: 'You may not change the type.'}, | |
{test: (!oldDoc || oldDoc.created_at == newDoc.created_at), | |
msg: 'You may not change the creation timestamp.'}, | |
{test: (newDoc._id == newDoc.username), | |
msg: 'Username must match _id.'}, | |
{test: (newDoc.pending_email_change == {} || | |
newDoc.pending_email_change.email.match(email_regex)), | |
msg: 'Pending e-mail is not valid.'}, | |
{test: (newDoc.email_history[newDoc.email_history.length - 1].email.match(email_regex)} | |
{test: (!oldDoc || preserve_history(newDoc.email_history, oldDoc.email_history)), | |
msg: 'You must preserve email history.'}, | |
{test: (!oldDoc || preserve_history(newDoc.name_history, oldDoc.name_history)), | |
msg: 'You must preserve name history.'} | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment