Skip to content

Commit

Permalink
Add validated function to DatasetProxy
Browse files Browse the repository at this point in the history
The only thing it does right now is make sure that periods referred to
in related period fields (narrower, broader, derivedFrom) actually exist
in the dataset
  • Loading branch information
ptgolden committed Jul 9, 2020
1 parent d936001 commit dae853c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
43 changes: 43 additions & 0 deletions modules/periodo-app/src/backends/dataset_proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,56 @@ const { getSort } = require('./sort')
, PromiseWorker = require('promise-worker')
, indexItems = require('./index_items')

function copy(obj) {
return JSON.parse(JSON.stringify(obj))
}

const SINGLE_VALUED_RELATED_PERIOD_FIELDS = [ 'derivedFrom' ]
, MULTI_VALUED_RELATED_PERIOD_FIELDS = [ 'narrower', 'broader' ]


module.exports = class DatasetProxy {
constructor(raw) {
this.raw = raw
Object.assign(this, indexItems(raw))
this.sorts = {}
}

get validated() {
const validated = {
type: 'rdf:Bag',
authorities: copy(this.authoritiesByID),
}

const { periods, periodsByID } = indexItems(validated)
, periodInDataset = periodID => periodID in periodsByID

// Filter out related periods that won't exist in the target dataset. This
// will be the case if someone adds local period A, which references local
// period B, but does not add local period B.
for (const period of periods) {
for (const attr of MULTI_VALUED_RELATED_PERIOD_FIELDS) {
const value = period[attr]

if (!value) continue

period[attr] = period[attr].filter(periodInDataset)
}

for (const attr of SINGLE_VALUED_RELATED_PERIOD_FIELDS) {
const value = period[attr]

if (!value) continue

if (!periodInDataset(value)) {
delete period[attr]
}
}
}

return validated
}

getWorker() {
if (this._worker) {
return this._worker
Expand Down
4 changes: 4 additions & 0 deletions modules/periodo-app/src/backends/dataset_proxy/index_items.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ module.exports = function indexItems(rawDataset) {
;[].concat(period[relation] || []).forEach(relatedPeriodID => {
const relatedPeriod = periodsByID[relatedPeriodID]

// It's possible that this related period might not exist, if this is
// a strangely formed dataset.
if (!relatedPeriod) return

period[$$RelatedPeriods][relation][relatedPeriodID] = relatedPeriod

if (inverseRelation) {
Expand Down
53 changes: 53 additions & 0 deletions modules/periodo-app/src/backends/test/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict"

const test = require('blue-tape')
, DatasetProxy = require('../dataset_proxy')

const danglingRelatedPeriodsDataset = {
authorities: {
p0123: {
id: 'p0123',
periods: {
p0123a: {
id: 'p0123a',
derivedFrom: 'p0123d',
broader: [
'p0123b',
'p0123c',
],
},
p0123b: {
id: 'p0123b',
derivedFrom: 'p0123a',
},
},
},
},
}

test('Dataset proxy validation', async t => {
const dataset = new DatasetProxy(danglingRelatedPeriodsDataset)

t.deepEqual(
dataset.validated,
{
type: 'rdf:Bag',
authorities: {
p0123: {
id: 'p0123',
periods: {
p0123a: {
id: 'p0123a',
broader: [
'p0123b',
],
},
p0123b: {
id: 'p0123b',
derivedFrom: 'p0123a',
},
},
},
},
}, 'should filter out related periods that do not exist')
})

0 comments on commit dae853c

Please sign in to comment.