Skip to content

Commit

Permalink
validate suspicious names using the user's language
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Nov 22, 2024
1 parent 3bda6c7 commit 3447766
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
18 changes: 14 additions & 4 deletions modules/validations/suspicious_name.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { t, localizer } from '../core/localizer';
import { validationIssue, validationIssueFix } from '../core/validation';


export function validationSuspiciousName() {
export function validationSuspiciousName(context) {
const type = 'suspicious_name';
const keysToTestForGenericValues = [
'aerialway', 'aeroway', 'amenity', 'building', 'craft', 'highway',
Expand Down Expand Up @@ -45,9 +45,17 @@ export function validationSuspiciousName() {
return false;
}

function isGenericName(name, tags) {
/** @param {string} name @param {string} presetName */
function nameMatchesPresetName(name, presetName) {
if (!presetName) return false;

return name.toLowerCase() === presetName.toLowerCase();
}

/** @param {string} name @param {string} presetName */
function isGenericName(name, tags, presetName) {
name = name.toLowerCase();
return nameMatchesRawTag(name, tags) || isGenericMatchInNsi(tags);
return nameMatchesRawTag(name, tags) || nameMatchesPresetName(name, presetName) || isGenericMatchInNsi(tags);
}

function makeGenericNameIssue(entityId, nameKey, genericName, langCode) {
Expand Down Expand Up @@ -105,14 +113,16 @@ export function validationSuspiciousName() {

let issues = [];

const presetName = presetManager.match(entity, context.graph()).name();

for (let key in tags) {
const m = key.match(/^name(?:(?::)([a-zA-Z_-]+))?$/);
if (!m) continue;

const langCode = m.length >= 2 ? m[1] : null;
const value = tags[key];

if (isGenericName(value, tags)) {
if (isGenericName(value, tags, presetName)) {
issues.provisional = _waitingForNsi; // retry later if we are waiting on NSI to finish loading
issues.push(makeGenericNameIssue(entity.id, key, value, langCode));
}
Expand Down
29 changes: 29 additions & 0 deletions test/spec/validations/suspicious_name.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ describe('iD.validations.suspicious_name', function () {
iD.fileFetcher.cache().nsi_generics = {
genericWords: ['^stores?$']
};
iD.fileFetcher.cache().preset_presets = {
'Velero': { tags: { craft: 'sailmaker' }, geometry: ['line'] },
'Constructor de barco': { tags: { craft: 'boatbuilder' }, geometry: ['line'] },
};
});

after(function() {
Expand Down Expand Up @@ -183,4 +187,29 @@ describe('iD.validations.suspicious_name', function () {
done();
}, 20);
});

it('flags feature with a name that matches the preset name', async () => {
await iD.presetManager.ensureLoaded(true);
createWay({ craft: 'sailmaker', 'name:ca': 'Velero' });
const validator = iD.validationSuspiciousName(context);

const issues = validate(validator);
expect(issues).to.have.lengthOf(1);
expect(issues[0].type).to.eql('suspicious_name');
expect(issues[0].hash).to.eql('name:ca=Velero');
});

it('flags feature with a name that matches the preset name and tag name', async () => {
await iD.presetManager.ensureLoaded(true);
createWay({ craft: 'boatbuilder', 'name:mi': 'boatbuilder', name: 'cOnStRuCtOr de barco' });
const validator = iD.validationSuspiciousName(context);

const issues = validate(validator);
expect(issues).to.have.lengthOf(2);
expect(issues[0].type).to.eql('suspicious_name');
expect(issues[0].hash).to.eql('name:mi=boatbuilder');

expect(issues[1].type).to.eql('suspicious_name');
expect(issues[1].hash).to.eql('name=cOnStRuCtOr de barco');
});
});

0 comments on commit 3447766

Please sign in to comment.