forked from openstreetmap/iD
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warning for features without start date (#193)
* Add empty start date validation * Add tests * Fix reference * Review fixes * Update text * Apply suggestions from code review --------- Co-authored-by: Minh Nguyễn <[email protected]>
- Loading branch information
Showing
6 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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,64 @@ | ||
import { t } from '../core/localizer'; | ||
import { utilDisplayLabel } from '../util'; | ||
import { validationIssue, validationIssueFix } from '../core/validation'; | ||
import { osmTimelessFeatureTagValues } from '../osm/tags'; | ||
|
||
export function validationMissingStartDate(context) { | ||
const type = 'missing_start_date'; | ||
|
||
const validation = function checkMissingStartDate(entity, graph) { | ||
// If start_date is not empty, return nothing | ||
if (entity.tags && (entity.tags.start_date || entity.tags['start_date:edtf'])) return []; | ||
// If entity has no tags, return nothing | ||
if (Object.keys(entity.tags).length === 0) return []; | ||
// Rule should be ignored for natural entities and waterways | ||
if (entity.tags && ( | ||
(entity.tags.natural && osmTimelessFeatureTagValues[entity.tags.natural]) || | ||
(entity.tags.waterway && osmTimelessFeatureTagValues[entity.tags.waterway]) || | ||
(entity.tags.water && osmTimelessFeatureTagValues[entity.tags.water]))) return []; | ||
|
||
// If entity is a vertex node | ||
var osm = context.connection(); | ||
var isUnloadedNode = entity.type === 'node' && osm && !osm.isDataLoaded(entity.loc); | ||
|
||
// Should skip this validation if node is unloaded, is a vertex or has parent relations | ||
if (isUnloadedNode || | ||
// allow untagged nodes that are part of ways | ||
entity.geometry(graph) === 'vertex' || | ||
// allow untagged entities that are part of relations | ||
entity.hasParentRelations(graph)) return []; | ||
|
||
const entityID = entity.id; | ||
|
||
function showReference(selection) { | ||
selection.selectAll('.issue-reference') | ||
.data([0]) | ||
.enter() | ||
.append('div') | ||
.attr('class', 'issue-reference') | ||
.call(t.append('issues.missing_start_date.reference')); | ||
} | ||
|
||
return [new validationIssue({ | ||
type: type, | ||
severity: 'warning', | ||
message: (context) => { | ||
const entity = context.hasEntity(entityID); | ||
return entity ? t.append('issues.missing_start_date.feature.message', { | ||
feature: utilDisplayLabel(entity, context.graph()) | ||
}) : ''; | ||
}, | ||
reference: showReference, | ||
entityIds: [entityID], | ||
dynamicFixes: () => { | ||
return [ | ||
new validationIssueFix({ title: t.append('issues.fix.add_start_date.title')}) | ||
]; | ||
} | ||
})]; | ||
}; | ||
|
||
validation.type = type; | ||
|
||
return validation; | ||
} |
This file contains 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,80 @@ | ||
describe('iD.validations.missing_start_date', function () { | ||
var context; | ||
|
||
beforeEach(function() { | ||
context = iD.coreContext().assetPath('../dist/').init(); | ||
}); | ||
|
||
function createWay(tags) { | ||
var n1 = iD.osmNode({id: 'n-1', loc: [4,4]}); | ||
var n2 = iD.osmNode({id: 'n-2', loc: [4,5]}); | ||
var n3 = iD.osmNode({id: 'n-3', loc: [5,5]}); | ||
var w = iD.osmWay({id: 'w-1', nodes: ['n-1', 'n-2', 'n-3'], tags: tags}); | ||
|
||
context.perform( | ||
iD.actionAddEntity(n1), | ||
iD.actionAddEntity(n2), | ||
iD.actionAddEntity(n3), | ||
iD.actionAddEntity(w) | ||
); | ||
} | ||
|
||
function validate() { | ||
var validator = iD.validationMissingStartDate(context); | ||
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 only missing tag errors on init', function() { | ||
var issues = validate(); | ||
expect(issues).to.have.lengthOf(0); | ||
}); | ||
|
||
it('ignores way with no tags', function() { | ||
createWay({}); | ||
var issues = validate(); | ||
expect(issues).to.have.lengthOf(0); | ||
}); | ||
|
||
it('ignores way with start_date tag', function() { | ||
createWay({ start_date: '1950' }); | ||
var issues = validate(); | ||
expect(issues).to.have.lengthOf(0); | ||
}); | ||
|
||
it('ignores way with start_date:edtf tag', function() { | ||
createWay({ 'start_date:edtf': '1950' }); | ||
var issues = validate(); | ||
expect(issues).to.have.lengthOf(0); | ||
}); | ||
|
||
it('ignores way with natural tag', function() { | ||
createWay({ natural: 'wood' }); | ||
var issues = validate(); | ||
expect(issues).to.have.lengthOf(0); | ||
}); | ||
|
||
it('ignores way with waterway tag', function() { | ||
createWay({ waterway: 'river' }); | ||
var issues = validate(); | ||
expect(issues).to.have.lengthOf(0); | ||
}); | ||
|
||
it('ignores way with water tag', function() { | ||
createWay({ water: 'pond' }); | ||
var issues = validate(); | ||
expect(issues).to.have.lengthOf(0); | ||
}); | ||
|
||
it('flags way without start_date tag', function() { | ||
createWay({ amenity: 'cafe' }); | ||
var issues = validate(); | ||
expect(issues).to.have.lengthOf(1); | ||
}); | ||
|
||
}); |