-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New validator: mutually exclusive tags #10035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tyrasd
merged 1 commit into
openstreetmap:develop
from
mtmail:mutually-exclusive-tags-validator
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { actionChangeTags } from '../actions/change_tags'; | ||
| import { t } from '../core/localizer'; | ||
| import { utilDisplayLabel } from '../util'; | ||
| import { validationIssue, validationIssueFix } from '../core/validation'; | ||
| import { osmMutuallyExclusiveTagPairs } from '../osm/tags'; | ||
|
|
||
| export function validationMutuallyExclusiveTags(/* context */) { | ||
| const type = 'mutually_exclusive_tags'; | ||
|
|
||
| // https://wiki.openstreetmap.org/wiki/Special:WhatLinksHere/Property:P44 | ||
| const tagKeyPairs = osmMutuallyExclusiveTagPairs; | ||
|
|
||
| const validation = function checkMutuallyExclusiveTags(entity /*, graph */) { | ||
|
|
||
| let pairsFounds = tagKeyPairs.filter((pair) => { | ||
| return (pair[0] in entity.tags && pair[1] in entity.tags); | ||
|
mtmail marked this conversation as resolved.
|
||
| }).filter((pair) => { | ||
| // noname=no is double-negation, thus positive and not conflicting. We'll ignore those | ||
| return !((pair[0].match(/^(addr:)?no[a-z]/) && entity.tags[pair[0]] === 'no') || | ||
| (pair[1].match(/^(addr:)?no[a-z]/) && entity.tags[pair[1]] === 'no')); | ||
| }); | ||
|
|
||
| // Additional: | ||
| // Check if name and not:name (and similar) are set and both have the same value | ||
|
mtmail marked this conversation as resolved.
|
||
| // not:name can actually have multiple values, separate by ; | ||
| // https://taginfo.openstreetmap.org/search?q=not%3A#keys | ||
| Object.keys(entity.tags).forEach((key) => { | ||
| let negative_key = 'not:' + key; | ||
| if (negative_key in entity.tags && entity.tags[negative_key].split(';').includes(entity.tags[key])) { | ||
| pairsFounds.push([negative_key, key, 'same_value']); | ||
| } | ||
| // For name:xx we also compare against the not:name tag | ||
| if (key.match(/^name:[a-z]+/)) { | ||
| negative_key = 'not:name'; | ||
| if (negative_key in entity.tags && entity.tags[negative_key].split(';').includes(entity.tags[key])) { | ||
| pairsFounds.push([negative_key, key, 'same_value']); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| let issues = pairsFounds.map((pair) => { | ||
| const subtype = pair[2] || 'default'; | ||
| return new validationIssue({ | ||
| type: type, | ||
| subtype: subtype, | ||
| severity: 'warning', | ||
| message: function(context) { | ||
| let entity = context.hasEntity(this.entityIds[0]); | ||
| return entity ? t.append(`issues.${type}.${subtype}.message`, { | ||
| feature: utilDisplayLabel(entity, context.graph()), | ||
| tag1: pair[0], | ||
| tag2: pair[1] | ||
|
mtmail marked this conversation as resolved.
|
||
| }) : ''; | ||
| }, | ||
|
mtmail marked this conversation as resolved.
|
||
| reference: (selection) => showReference(selection, pair, subtype), | ||
| entityIds: [entity.id], | ||
| dynamicFixes: () => pair.slice(0,2).map((tagToRemove) => createIssueFix(tagToRemove)) | ||
| }); | ||
| }); | ||
|
|
||
| function createIssueFix(tagToRemove) { | ||
| return new validationIssueFix({ | ||
| icon: 'iD-operation-delete', | ||
| title: t.append('issues.fix.remove_named_tag.title', { tag: tagToRemove }), | ||
| onClick: function(context) { | ||
| const entityId = this.issue.entityIds[0]; | ||
| const entity = context.entity(entityId); | ||
| let tags = Object.assign({}, entity.tags); // shallow copy | ||
| delete tags[tagToRemove]; | ||
| context.perform( | ||
| actionChangeTags(entityId, tags), | ||
| t('issues.fix.remove_named_tag.annotation', { tag: tagToRemove }) | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function showReference(selection, pair, subtype) { | ||
| selection.selectAll('.issue-reference') | ||
| .data([0]) | ||
| .enter() | ||
| .append('div') | ||
| .attr('class', 'issue-reference') | ||
| .call(t.append(`issues.${type}.${subtype}.reference`, { tag1: pair[0], tag2: pair[1] })); | ||
| } | ||
|
|
||
| return issues; | ||
| }; | ||
|
|
||
| validation.type = type; | ||
|
|
||
| return validation; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| describe('iD.validations.mutually_exclusive_tags', function () { | ||
| var context; | ||
|
|
||
| beforeEach(function() { | ||
| context = iD.coreContext().init(); | ||
| }); | ||
|
|
||
| function createNode(tags) { | ||
| context.perform( | ||
| iD.actionAddEntity({id: 'n-1', loc: [4,4], tags: tags}) | ||
| ); | ||
| } | ||
|
|
||
| function validate(validator) { | ||
| var changes = context.history().changes(); | ||
| var entities = changes.modified.concat(changes.created); | ||
| var issues = []; | ||
| entities.forEach(function(entity) { | ||
| issues = issues.concat(validator(entity, context.graph())); | ||
| }); | ||
| return issues; | ||
| } | ||
|
|
||
|
|
||
| it('has no errors on init', function(done) { | ||
| var validator = iD.validationMutuallyExclusiveTags(context); | ||
| window.setTimeout(function() { // async, so data will be available | ||
| var issues = validate(validator); | ||
| expect(issues).to.have.lengthOf(0); | ||
| done(); | ||
| }, 20); | ||
| }); | ||
|
|
||
| it('has no errors on good tags', function(done) { | ||
| createNode({'name': 'Trader Joe', 'not:name': 'Trader Jane'}); | ||
| var validator = iD.validationMutuallyExclusiveTags(context); | ||
| window.setTimeout(function() { // async, so data will be available | ||
| var issues = validate(validator); | ||
| expect(issues).to.have.lengthOf(0); | ||
| done(); | ||
| }, 20); | ||
| }); | ||
|
|
||
| it('flags mutually exclusive tags', function(done) { | ||
| createNode({'name': 'Trader Joe', 'noname': 'yes'}); | ||
| var validator = iD.validationMutuallyExclusiveTags(context); | ||
| window.setTimeout(function() { // async, so data will be available | ||
| var issues = validate(validator); | ||
| expect(issues).to.have.lengthOf(1); | ||
| var issue = issues[0]; | ||
| expect(issue.type).to.eql('mutually_exclusive_tags'); | ||
| expect(issue.subtype).to.eql('default'); | ||
| expect(issue.severity).to.eql('warning'); | ||
| expect(issue.entityIds).to.have.lengthOf(1); | ||
| expect(issue.entityIds[0]).to.eql('n-1'); | ||
| done(); | ||
| }, 20); | ||
| }); | ||
|
|
||
| it('flags feature with a mutually exclusive `not:name` value', function(done) { | ||
| createNode({ shop: 'supermarket', name: 'Lous', 'not:name': 'Lous' }); | ||
| var validator = iD.validationMutuallyExclusiveTags(context); | ||
| window.setTimeout(function() { // async, so data will be available | ||
| var issues = validate(validator); | ||
| expect(issues).to.have.lengthOf(1); | ||
| var issue = issues[0]; | ||
| expect(issue.type).to.eql('mutually_exclusive_tags'); | ||
| expect(issue.subtype).to.eql('same_value'); | ||
| expect(issue.severity).to.eql('warning'); | ||
| expect(issue.entityIds).to.have.lengthOf(1); | ||
| expect(issue.entityIds[0]).to.eql('n-1'); | ||
| done(); | ||
| }, 20); | ||
| }); | ||
|
|
||
|
|
||
| it('flags feature with a mutually exclusive semicolon-separated `not:name` value', function(done) { | ||
| createNode({ shop: 'supermarket', name: 'Lous', 'not:name': 'Louis\';Lous;Louis\'s' }); | ||
| var validator = iD.validationMutuallyExclusiveTags(context); | ||
| window.setTimeout(function() { // async, so data will be available | ||
| var issues = validate(validator); | ||
| expect(issues).to.have.lengthOf(1); | ||
| var issue = issues[0]; | ||
| expect(issue.type).to.eql('mutually_exclusive_tags'); | ||
| expect(issue.subtype).to.eql('same_value'); | ||
| expect(issue.severity).to.eql('warning'); | ||
| expect(issue.entityIds).to.have.lengthOf(1); | ||
| expect(issue.entityIds[0]).to.eql('n-1'); | ||
| done(); | ||
| }, 20); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.