-
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): validate enum values when set in datapackage.json
Closes #258
- Loading branch information
Showing
31 changed files
with
131,038 additions
and
50 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
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,24 @@ | ||
import { compact, flattenDeep, includes } from 'lodash'; | ||
import { DATA_POINT_CONSTRAINT_VIOLATION } from '../registry'; | ||
import { cacheFor, IConstraintDescriptor } from './shared'; | ||
import { Issue } from '../issue'; | ||
|
||
export const rule = { | ||
isDataPoint: true, | ||
recordRule: dataPointDescriptor => { | ||
const constraints: IConstraintDescriptor[] = cacheFor.constraintsByFileDescriptor(dataPointDescriptor); | ||
const constraintViolation = (constraint: IConstraintDescriptor) => { | ||
return !includes(constraint.constraints, dataPointDescriptor.record[constraint.fieldName]); | ||
}; | ||
|
||
return compact(flattenDeep( | ||
constraints.filter(constraintViolation).map((constraint: IConstraintDescriptor) => | ||
new Issue(DATA_POINT_CONSTRAINT_VIOLATION).setPath(constraint.fullPath).setData({ | ||
constraints: constraint.constraints, | ||
fieldName: constraint.fieldName, | ||
fieldValue: dataPointDescriptor.record[constraint.fieldName], | ||
line: dataPointDescriptor.line | ||
})) | ||
)); | ||
} | ||
}; |
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,42 @@ | ||
import { compact, flattenDeep, get as getValue } from 'lodash'; | ||
import { UNEXISTING_CONSTRAINT_VALUE } from '../registry'; | ||
import { DdfDataSet } from '../../ddf-definitions/ddf-data-set'; | ||
import { DirectoryDescriptor } from '../../data/directory-descriptor'; | ||
import { DATA_POINT } from '../../ddf-definitions/constants'; | ||
import { Issue } from '../issue'; | ||
|
||
const checkConstraintValue = (ddfDataSet: DdfDataSet, name: string, value: string): boolean => { | ||
const allEntities = ddfDataSet.getEntity().getAllData(); | ||
|
||
for (const record of allEntities) { | ||
if (record[name] === value) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
const forDataPointType = (fileDescriptor: any) => fileDescriptor.type === DATA_POINT; | ||
const constraintsAreExists = (field: any) => field.constraints && field.constraints.enum; | ||
const constraintValueAreNotPresentInEntities = | ||
(ddfDataSet: DdfDataSet, name: string, value: string) => !checkConstraintValue(ddfDataSet, name, value); | ||
const getConstraintsByField = (field: any) => getValue<any | string[]>(field, 'constraints.enum', []); | ||
const getSchemaFields = (fileDescriptor: any) => getValue<any | any[]>(fileDescriptor, 'schema.fields', []); | ||
|
||
export const rule = { | ||
rule: (ddfDataSet: DdfDataSet) => { | ||
const issues = ddfDataSet.ddfRoot.directoryDescriptors.map((directoryDescriptor: DirectoryDescriptor) => | ||
directoryDescriptor.dataPackage.fileDescriptors.filter(forDataPointType).map((fileDescriptor: any) => { | ||
const constrainedSchemaFields = getSchemaFields(fileDescriptor).filter(constraintsAreExists); | ||
|
||
return constrainedSchemaFields.map((field: any) => { | ||
const issuesSource = getConstraintsByField(field).filter(value => constraintValueAreNotPresentInEntities(ddfDataSet, field.name, value)); | ||
|
||
return issuesSource.map(value => | ||
new Issue(UNEXISTING_CONSTRAINT_VALUE).setPath(fileDescriptor.fullPath).setData({constraintEntityValue: value})); | ||
}); | ||
})); | ||
|
||
return compact(flattenDeep(issues)); | ||
} | ||
}; |
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.