Files
iD/modules/validations/invalid_format.js
SilentSpike f02238ce23 Fix varied validation messages and redeclaration
I've hijacked the data property for the purposes of changing the message
based on whether multiple values in a list are erroneous
2019-06-06 13:55:53 -04:00

91 lines
3.3 KiB
JavaScript

import { t } from '../util/locale';
import { utilDisplayLabel } from '../util';
import { validationIssue } from '../core/validation';
export function validationFormatting() {
var type = 'invalid_format';
var validation = function(entity, context) {
var issues = [];
function isInvalidEmail(email) {
// Same regex as used by HTML5 "email" inputs
// Emails in OSM are going to be official so they should be pretty simple
var valid_email = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
return !valid_email.test(email);
}
function isSchemeMissing(url) {
var valid_scheme = /^https?:\/\//i;
return !valid_scheme.test(url);
}
function showReferenceEmail(selection) {
selection.selectAll('.issue-reference')
.data([0])
.enter()
.append('div')
.attr('class', 'issue-reference')
.text(t('issues.invalid_format.email.reference'));
}
function showReferenceWebsite(selection) {
selection.selectAll('.issue-reference')
.data([0])
.enter()
.append('div')
.attr('class', 'issue-reference')
.text(t('issues.invalid_format.website.reference'));
}
if (entity.tags.website) {
// Multiple websites are possible
var websites = entity.tags.website.split(';').filter(isSchemeMissing);
if (websites.length) {
issues.push(new validationIssue({
type: type,
subtype: 'website',
severity: 'warning',
message: function() {
var entity = context.hasEntity(this.entityIds[0]);
return entity ? t('issues.invalid_format.website.message' + this.data,
{ feature: utilDisplayLabel(entity, context), site: websites.join(', ') }) : '';
},
reference: showReferenceWebsite,
entityIds: [entity.id],
hash: websites.join(),
data: (websites.length > 1) ? '_multi' : ''
}));
}
}
if (entity.tags.email) {
// Multiple emails are possible
var emails = entity.tags.email.split(';').filter(isInvalidEmail);
if (emails.length) {
issues.push(new validationIssue({
type: type,
subtype: 'email',
severity: 'warning',
message: function() {
var entity = context.hasEntity(this.entityIds[0]);
return entity ? t('issues.invalid_format.email.message' + this.data,
{ feature: utilDisplayLabel(entity, context), email: emails.join(', ') }) : '';
},
reference: showReferenceEmail,
entityIds: [entity.id],
hash: emails.join(),
data: (emails.length > 1) ? '_multi' : ''
}));
}
}
return issues;
};
validation.type = type;
return validation;
}