-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): Two new datapackage validation rules
Closes #494
- Loading branch information
Showing
45 changed files
with
1,860 additions
and
76 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
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,55 @@ | ||
import { isArray, includes, compact } from 'lodash'; | ||
import { DdfDataSet } from '../../ddf-definitions/ddf-data-set'; | ||
import { Issue } from '../issue'; | ||
import { DATAPACKAGE_NONEXISTENT_CONCEPT } from '../registry'; | ||
import { DATA_PACKAGE_FILE } from '../../data/data-package'; | ||
import * as path from 'path'; | ||
import { DDFRoot } from '../../data/ddf-root'; | ||
|
||
const toArray = value => isArray(value) ? value : [value]; | ||
const fillConceptsSetBySchema = (dataPackageSchema, conceptsSet: Set<string>) => { | ||
[ | ||
...dataPackageSchema.concepts, | ||
...dataPackageSchema.entities, | ||
...dataPackageSchema.datapoints | ||
].forEach(resource => { | ||
for (const pk of toArray(resource.primaryKey)) { | ||
conceptsSet.add(pk); | ||
} | ||
|
||
conceptsSet.add(resource.value); | ||
}); | ||
}; | ||
const fillResources = (ddfRoot: DDFRoot, conceptsSet: Set<string>) => { | ||
ddfRoot.getDataPackageResources().forEach(resource => { | ||
for (const pk of toArray(resource.schema.primaryKey)) { | ||
conceptsSet.add(pk); | ||
} | ||
|
||
for (const field of resource.schema.fields) { | ||
conceptsSet.add(field.name); | ||
} | ||
}); | ||
}; | ||
|
||
export const rule = { | ||
rule: (ddfDataSet: DdfDataSet) => { | ||
const ddfRoot = ddfDataSet.ddfRoot; | ||
const dataPackagePath = path.resolve(ddfRoot.dataPackageDescriptor.rootFolder, DATA_PACKAGE_FILE); | ||
const conceptsSet = new Set<string>(); | ||
const dataPackageSchema = ddfRoot.getDataPackageSchema(); | ||
|
||
if (dataPackageSchema) { | ||
fillConceptsSetBySchema(dataPackageSchema, conceptsSet); | ||
} | ||
|
||
fillResources(ddfRoot, conceptsSet); | ||
|
||
const originalConcepts = ddfDataSet.getConcept().getAllData().map(record => record.concept); | ||
|
||
return compact(Array.from(conceptsSet.values())) | ||
.map(concept => concept.replace(/^is--/, '')) | ||
.filter(concept => concept !== 'concept' && concept !== 'concept_type' && !includes(originalConcepts, concept)) | ||
.map(concept => new Issue(DATAPACKAGE_NONEXISTENT_CONCEPT).setPath(dataPackagePath).setData(concept)) | ||
} | ||
}; |
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,83 @@ | ||
import * as path from 'path'; | ||
import { isEmpty, compact } from 'lodash'; | ||
import { DdfDataSet } from '../../ddf-definitions/ddf-data-set'; | ||
import { Issue } from '../issue'; | ||
import { DATAPACKAGE_NONEXISTENT_RESOURCE } from '../registry'; | ||
import { DATA_PACKAGE_FILE } from '../../data/data-package'; | ||
import { DDFRoot } from '../../data/ddf-root'; | ||
|
||
const getNonexistentResourcesIssues = ( | ||
ddfRoot: DDFRoot, | ||
dataPackagePath: string, | ||
resourcesMap: Map<string, number>): Issue[] => { | ||
if (!ddfRoot.getDataPackageSchema()) { | ||
return []; | ||
} | ||
|
||
return compact([ | ||
...ddfRoot.getDataPackageSchema().concepts, | ||
...ddfRoot.getDataPackageSchema().entities, | ||
...ddfRoot.getDataPackageSchema().datapoints | ||
].map(record => { | ||
const nonexistentResources = record.resources.filter(resource => !resourcesMap.has(resource)); | ||
|
||
if (!isEmpty(nonexistentResources)) { | ||
return new Issue(DATAPACKAGE_NONEXISTENT_RESOURCE) | ||
.setPath(dataPackagePath) | ||
.setData({ | ||
nonexistentResources, record, | ||
specific: 'is NOT found in resources, but found in schema section' | ||
}); | ||
} | ||
|
||
return null; | ||
})); | ||
}; | ||
|
||
const fillResourceMapCounters = (ddfRoot: DDFRoot, resourcesMap: Map<string, number>) => { | ||
if (!ddfRoot.getDataPackageSchema()) { | ||
return []; | ||
} | ||
|
||
[ | ||
...ddfRoot.getDataPackageSchema().concepts, | ||
...ddfRoot.getDataPackageSchema().entities, | ||
...ddfRoot.getDataPackageSchema().datapoints | ||
].forEach(record => { | ||
record.resources.forEach(resource => { | ||
if (resourcesMap.has(resource)) { | ||
resourcesMap.set(resource, resourcesMap.get(resource) + 1); | ||
} | ||
|
||
return resource; | ||
}) | ||
}); | ||
}; | ||
|
||
const getNonexistentSchemaResourcesIssues = (dataPackagePath: string, resourcesMap: Map<string, number>): Issue[] => | ||
Array.from(resourcesMap.keys()) | ||
.filter(resource => resourcesMap.get(resource) === 0) | ||
.map(resource => new Issue(DATAPACKAGE_NONEXISTENT_RESOURCE) | ||
.setPath(dataPackagePath) | ||
.setData({resource, specific: 'is NOT found in ddfSchema schema, but found in resources section'})); | ||
|
||
export const rule = { | ||
rule: (ddfDataSet: DdfDataSet) => { | ||
const ddfRoot = ddfDataSet.ddfRoot; | ||
const dataPackagePath = path.resolve(ddfRoot.dataPackageDescriptor.rootFolder, DATA_PACKAGE_FILE); | ||
const resourcesMap = ddfRoot.getDataPackageResources() | ||
.map(resource => resource.name) | ||
.reduce((mapValue, resourceName) => { | ||
mapValue.set(resourceName, 0); | ||
|
||
return mapValue; | ||
}, new Map<string, number>()); | ||
|
||
fillResourceMapCounters(ddfRoot, resourcesMap); | ||
|
||
return [ | ||
...getNonexistentResourcesIssues(ddfRoot, dataPackagePath, resourcesMap), | ||
...getNonexistentSchemaResourcesIssues(dataPackagePath, resourcesMap) | ||
]; | ||
} | ||
}; |
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
Oops, something went wrong.