Skip to content

Commit

Permalink
feat(validation): Add validation to addParent(). (#150)
Browse files Browse the repository at this point in the history
Explicit parent field validation authored by @mansoor.sajjad
With this change, any attempt to add a field with a name not supported by `pelias/schema` will throw an error.

* Adding parentFields validation in addParent.

* Removing 'macrohood' from the parentFields as it's not supported by the pelias schema.

* Removing 'macrohood' from the parentFields in the unit test.

Co-authored-by: mansoor.sajjad <[email protected]>
  • Loading branch information
mansoor-sajjad and mansoor.sajjad authored Aug 1, 2022
1 parent c7b708b commit 83b64de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 4 additions & 3 deletions Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const parentFields = [
'borough',
'locality',
'localadmin',
'macrohood',
'neighbourhood',
'postalcode',
'ocean',
Expand Down Expand Up @@ -313,6 +312,8 @@ Document.prototype.delName = function( prop ){
// parent
Document.prototype.addParent = function( field, name, id, abbr, source ){

validate.property(parentFields, field);

var add = function( prop, value ){

// create new parent array if required
Expand Down Expand Up @@ -377,7 +378,7 @@ Document.prototype.addParent = function( field, name, id, abbr, source ){
return this;
};

// clear all all added values
// clear all added values
Document.prototype.clearParent = function(field) {

// field has never been set
Expand Down Expand Up @@ -436,7 +437,7 @@ Document.prototype.setAddressAlias = function( prop, value ){
this.address_parts[ prop ] = [ stringValue ];
}

// is the array empty? ie. no prior call to setAddress()
// is the array empty? i.e. no prior call to setAddress()
// in this case we will also set element 0 (the element used for display)
if( !this.address_parts[ prop ].length ){
this.setAddress( prop, value );
Expand Down
15 changes: 14 additions & 1 deletion test/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ module.exports.tests.parent_types = (test) => {
'borough',
'locality',
'localadmin',
'macrohood',
'neighbourhood',
'postalcode',
'ocean',
Expand Down Expand Up @@ -140,6 +139,20 @@ module.exports.tests.parent_types = (test) => {

});

test('Invalid parent field name', (t) => {
const doc = new Document('mysource', 'mylayer', 'myid');

t.throws(
() => doc.addParent('someRandomName', 'name 1', 'id 1', 'abbr 1'),
'invalid property: someRandomName, should be one of: ' +
'continent,country,dependency,macroregion,region,macrocounty,county,borough,' +
'locality,localadmin,neighbourhood,postalcode,ocean,marinearea,empire',
'Should fail for invalid parent field.');

t.end();

});

test('non-WOF placetype arguments should return false', (t) => {
const doc = new Document('mysource', 'mylayer', 'myid');

Expand Down

0 comments on commit 83b64de

Please sign in to comment.