Ghost/ghost/admin/validators/user.js
Jason Williams faf6832cab Add validation to invite new user form.
Closes #3246
- Add a UserValidator to the validation engine that runs a set
  of validations based on the user status.
- Added validations for invited users and active users.
2014-07-11 19:09:34 +00:00

63 lines
1.9 KiB
JavaScript

var UserValidator = Ember.Object.create({
check: function (model) {
var validator = this.validators[model.get('status')];
if (typeof validator !== 'function') {
return [];
}
return validator(model);
},
validators: {
invited: function (model) {
var validationErrors = [],
email = model.get('email'),
role = model.get('role');
if (!validator.isEmail(email)) {
validationErrors.push({ message: 'Please supply a valid email address' });
}
if (!validator.isLength(role, 1)) {
validationErrors.push({ message: 'Please select a role' });
}
return validationErrors;
},
active: function (model) {
var validationErrors = [],
name = model.get('name'),
bio = model.get('bio'),
email = model.get('email'),
location = model.get('location'),
website = model.get('website');
if (!validator.isLength(name, 0, 150)) {
validationErrors.push({ message: 'Name is too long' });
}
if (!validator.isLength(bio, 0, 200)) {
validationErrors.push({ message: 'Bio is too long' });
}
if (!validator.isEmail(email)) {
validationErrors.push({ message: 'Please supply a valid email address' });
}
if (!validator.isLength(location, 0, 150)) {
validationErrors.push({ message: 'Location is too long' });
}
if (!validator.isURL(website, { protocols: ['http', 'https'], require_protocol: true }) ||
!validator.isLength(website, 0, 2000)) {
validationErrors.push({ message: 'Please use a valid url' });
}
}
}
});
export default UserValidator;