Ghost/core/client/app/validators/nav-item.js
Kevin Ansfield 3d6856614f Use es6 across client and add ember-suave to enforce rules
no issue
- add ember-suave dependency
- upgrade grunt-jscs dependency
- add a new .jscsrc for the client's tests directory that extends from client's base .jscsrc
- separate client tests in Gruntfile jscs task so they pick up the test's .jscsrc
- standardize es6 usage across client
2015-11-30 10:41:01 +00:00

59 lines
1.7 KiB
JavaScript

import BaseValidator from './base';
export default BaseValidator.create({
properties: ['label', 'url'],
label(model) {
let label = model.get('label');
let hasValidated = model.get('hasValidated');
if (this.canBeIgnored(model)) {
return;
}
if (validator.empty(label)) {
model.get('errors').add('label', 'You must specify a label');
this.invalidate();
}
hasValidated.addObject('label');
},
url(model) {
let url = model.get('url');
let hasValidated = model.get('hasValidated');
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
let validatorOptions = {require_protocol: true};
/* jscs:enable requireCamelCaseOrUpperCaseIdentifiers */
let urlRegex = new RegExp(/^(\/|#|[a-zA-Z0-9\-]+:)/);
if (this.canBeIgnored(model)) {
return;
}
if (validator.empty(url)) {
model.get('errors').add('url', 'You must specify a URL or relative path');
this.invalidate();
} else if (url.match(/\s/) || (!validator.isURL(url, validatorOptions) && !url.match(urlRegex))) {
model.get('errors').add('url', 'You must specify a valid URL or relative path');
this.invalidate();
}
hasValidated.addObject('url');
},
canBeIgnored(model) {
let label = model.get('label');
let url = model.get('url');
let isLast = model.get('last');
// if nav item is last and completely blank, mark it valid and skip
if (isLast && (validator.empty(url) || url === '/') && validator.empty(label)) {
model.get('errors').clear();
return true;
}
return false;
}
});