2015-09-16 20:02:06 +03:00
|
|
|
import BaseValidator from './base';
|
2019-01-22 16:09:38 +03:00
|
|
|
import validator from 'validator';
|
2018-03-19 20:56:09 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
export default BaseValidator.create({
|
|
|
|
properties: ['label', 'url'],
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
label(model) {
|
|
|
|
let label = model.get('label');
|
|
|
|
let hasValidated = model.get('hasValidated');
|
2015-09-16 20:02:06 +03:00
|
|
|
|
2018-03-19 20:56:09 +03:00
|
|
|
if (isBlank(label)) {
|
2015-09-16 20:02:06 +03:00
|
|
|
model.get('errors').add('label', 'You must specify a label');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
hasValidated.addObject('label');
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
url(model) {
|
|
|
|
let url = model.get('url');
|
|
|
|
let hasValidated = model.get('hasValidated');
|
2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-disable camelcase */
|
2015-10-28 14:36:45 +03:00
|
|
|
let validatorOptions = {require_protocol: true};
|
2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-enable camelcase */
|
2017-09-11 10:56:11 +03:00
|
|
|
let urlRegex = new RegExp(/^(\/|#|[a-zA-Z0-9-]+:)/);
|
2015-09-16 20:02:06 +03:00
|
|
|
|
2018-03-19 20:56:09 +03:00
|
|
|
if (isBlank(url)) {
|
2015-09-16 20:02:06 +03:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|