-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1364 from pelias/require-gid-in_id-field
Require GID in Elasticsearch `_id` field
- Loading branch information
Showing
7 changed files
with
120 additions
and
73 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const _ = require('lodash'); | ||
|
||
// helper method to decode a GID from a string | ||
// for additional validation see sanitizer/_ids.js | ||
|
||
function decodeGID(gid) { | ||
const parts = gid.split(':'); | ||
|
||
if ( parts.length < 3 ) { | ||
return; | ||
} | ||
|
||
// empty strings and other invalid values are expected to be handled by the caller | ||
return { | ||
source: parts[0], | ||
layer: parts[1], | ||
id: parts.slice(2).join(':'), | ||
}; | ||
} | ||
|
||
module.exports = decodeGID; |
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,51 @@ | ||
const decode_gid = require('../../../helper/decode_gid'); | ||
|
||
module.exports.tests = {}; | ||
|
||
module.exports.tests.valid_gid = function(test, common) { | ||
test('standard GID is decoded correctly', function(t) { | ||
const gid = decode_gid('whosonfirst:locality:123'); | ||
|
||
t.equal(gid.source, 'whosonfirst'); | ||
t.equal(gid.layer, 'locality'); | ||
t.equal(gid.id, '123'); | ||
t.end(); | ||
}); | ||
|
||
test('GID with colons in ID decoded correctly', function(t) { | ||
const gid = decode_gid('openaddresses:address:city_of_new_york:123'); | ||
|
||
t.equal(gid.source, 'openaddresses'); | ||
t.equal(gid.layer, 'address'); | ||
t.equal(gid.id, 'city_of_new_york:123'); | ||
t.end(); | ||
}); | ||
}; | ||
|
||
module.exports.tests.invalid_gid = function(test, common) { | ||
test('gid without 3 parts returns undefined', function(t) { | ||
const gid = decode_gid('whosonfirst:locality'); | ||
|
||
t.equal(gid, undefined); | ||
t.end(); | ||
}); | ||
|
||
test('gid without 3 full parts returns empty strings', function(t) { | ||
const gid = decode_gid('whosonfirst:locality:'); | ||
|
||
t.equal(gid.source, 'whosonfirst'); | ||
t.equal(gid.layer, 'locality'); | ||
t.equal(gid.id, ''); | ||
t.end(); | ||
}); | ||
}; | ||
|
||
module.exports.all = (tape, common) => { | ||
function test(name, testFunction) { | ||
return tape(`decode GID: ${name}`, testFunction); | ||
} | ||
|
||
for( var testCase in module.exports.tests ){ | ||
module.exports.tests[testCase](test, common); | ||
} | ||
}; |
Oops, something went wrong.