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) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let label = model.label;
|
|
|
|
let hasValidated = model.hasValidated;
|
2015-09-16 20:02:06 +03:00
|
|
|
|
2018-03-19 20:56:09 +03:00
|
|
|
if (isBlank(label)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('label', 'You must specify a label');
|
2015-09-16 20:02:06 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
hasValidated.addObject('label');
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
url(model) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let url = model.url;
|
|
|
|
let hasValidated = model.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)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('url', 'You must specify a URL or relative path');
|
2015-09-16 20:02:06 +03:00
|
|
|
this.invalidate();
|
|
|
|
} else if (url.match(/\s/) || (!validator.isURL(url, validatorOptions) && !url.match(urlRegex))) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('url', 'You must specify a valid URL or relative path');
|
2015-09-16 20:02:06 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
hasValidated.addObject('url');
|
|
|
|
}
|
|
|
|
});
|