2015-06-07 06:19:19 +03:00
|
|
|
import BaseValidator from './base';
|
2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
var UserValidator = BaseValidator.create({
|
|
|
|
properties: ['name', 'bio', 'email', 'location', 'website', 'roles'],
|
|
|
|
isActive: function (model) {
|
|
|
|
return (model.get('status') === 'active');
|
2014-07-11 23:01:42 +04:00
|
|
|
},
|
2015-06-07 06:19:19 +03:00
|
|
|
name: function (model) {
|
|
|
|
var name = model.get('name');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2014-07-11 23:01:42 +04:00
|
|
|
if (!validator.isLength(name, 0, 150)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('name', 'Name is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
bio: function (model) {
|
|
|
|
var bio = model.get('bio');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2014-07-11 23:01:42 +04:00
|
|
|
if (!validator.isLength(bio, 0, 200)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('bio', 'Bio is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
email: function (model) {
|
|
|
|
var email = model.get('email');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (!validator.isEmail(email)) {
|
|
|
|
model.get('errors').add('email', 'Please supply a valid email address');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
location: function (model) {
|
|
|
|
var location = model.get('location');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2014-07-11 23:01:42 +04:00
|
|
|
if (!validator.isLength(location, 0, 150)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('location', 'Location is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
website: function (model) {
|
|
|
|
var website = model.get('website');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2014-08-01 00:29:35 +04:00
|
|
|
if (!Ember.isEmpty(website) &&
|
2014-11-17 07:35:32 +03:00
|
|
|
(!validator.isURL(website, {require_protocol: false}) ||
|
2014-07-14 20:32:55 +04:00
|
|
|
!validator.isLength(website, 0, 2000))) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('website', 'Website is not a valid url');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
roles: function (model) {
|
|
|
|
if (!this.isActive(model)) {
|
|
|
|
var roles = model.get('roles');
|
2014-07-14 20:32:55 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (roles.length < 1) {
|
|
|
|
model.get('errors').add('role', 'Please select a role');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default UserValidator;
|