Skip to content

Commit

Permalink
Merge pull request #543 from Gapminder/feat-inconsistent-synonym-key
Browse files Browse the repository at this point in the history
feat(new rule): provide new rule INCONSISTENT_SYNONYM_KEY
  • Loading branch information
buchslava authored Nov 28, 2018
2 parents b89b16b + 7f0ebba commit 0738bf2
Show file tree
Hide file tree
Showing 15 changed files with 3,889 additions and 5 deletions.
31 changes: 31 additions & 0 deletions src/ddf-rules/general-rules/inconsistent-synonym-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { endsWith, isEmpty } from 'lodash';
import { INCONSISTENT_SYNONYM_KEY } from '../registry';
import { DdfDataSet } from '../../ddf-definitions/ddf-data-set';
import { Issue } from '../issue';

export const rule = {
rule: (ddfDataSet: DdfDataSet) => {
const issues = [];
const synonymsData = ddfDataSet.getSynonym().getAllData();

for (const record of synonymsData) {
const resource = ddfDataSet.getDataPackageResources().find(resource => endsWith(record.$$source, resource.path));
const primaryKey = resource.schema.primaryKey;
const undefinedKeyParts = [];

for (const partOfPrimaryKey of primaryKey) {
if (isEmpty(record[partOfPrimaryKey])) {
undefinedKeyParts.push(partOfPrimaryKey);
}
}

if (!isEmpty(undefinedKeyParts)) {
issues.push(new Issue(INCONSISTENT_SYNONYM_KEY)
.setPath(record.$$source)
.setData({line: record.$$lineNumber, undefinedKeyParts}));
}
}

return issues;
}
};
4 changes: 3 additions & 1 deletion src/ddf-rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { rule as nonexistentResource } from './data-package-rules/nonexistent-re
import { rule as nonexistentConcept } from './data-package-rules/nonexistent-concept';
import { rule as inconsistentDatapackage } from './data-package-rules/inconsistent-datapackage';
import { rule as duplicatedSynonymKey } from './general-rules/duplicated-synonym-key';
import { rule as inconsistentSynonymKey} from './general-rules/inconsistent-synonym-key';

// import { rule as measureValueNotNumeric } from './data-point-rules/measure-value-not-numeric';
import { rule as unexpectedEntityValue } from './data-point-rules/unexpected-entity-value';
Expand Down Expand Up @@ -90,5 +91,6 @@ export const allRules = {
[registry.DATA_POINT_CONSTRAINT_VIOLATION]: dataPointConstraintViolation,
[registry.DUPLICATED_DATA_POINT_KEY]: duplicatedDataPointKey,
[registry.ENTITY_VALUE_AS_ENTITY_NAME]: entityValueAsEntityName,
[registry.DUPLICATED_SYNONYM_KEY]: duplicatedSynonymKey
[registry.DUPLICATED_SYNONYM_KEY]: duplicatedSynonymKey,
[registry.INCONSISTENT_SYNONYM_KEY]: inconsistentSynonymKey
};
10 changes: 7 additions & 3 deletions src/ddf-rules/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const INCORRECT_BOOLEAN_ENTITY = Symbol.for('INCORRECT_BOOLEAN_ENTITY');
export const CONCEPT_LOOKS_LIKE_BOOLEAN = Symbol.for('CONCEPT_LOOKS_LIKE_BOOLEAN');
export const ENTITY_VALUE_AS_ENTITY_NAME = Symbol.for('ENTITY_VALUE_AS_ENTITY_NAME');
export const DUPLICATED_SYNONYM_KEY = Symbol.for('DUPLICATED_SYNONYM_KEY');
export const INCONSISTENT_SYNONYM_KEY = Symbol.for('INCONSISTENT_SYNONYM_KEY');

export const WARNING_TAG = Symbol.for('WARNING');
export const FILE_SYSTEM_TAG = Symbol.for('FILE_SYSTEM');
Expand Down Expand Up @@ -96,7 +97,8 @@ export const tags: any = {
[INCORRECT_BOOLEAN_ENTITY]: [],
[CONCEPT_LOOKS_LIKE_BOOLEAN]: [WARNING_TAG],
[ENTITY_VALUE_AS_ENTITY_NAME]: [WAFFLE_SERVER_TAG, WARNING_TAG],
[DUPLICATED_SYNONYM_KEY]: []
[DUPLICATED_SYNONYM_KEY]: [],
[INCONSISTENT_SYNONYM_KEY]: []
};

export const descriptions = {
Expand Down Expand Up @@ -142,7 +144,8 @@ export const descriptions = {
[INCORRECT_BOOLEAN_ENTITY]: 'Boolean entitiy field has an incorrect value.',
[CONCEPT_LOOKS_LIKE_BOOLEAN]: 'Entity contains values that look like boolean, but related entity field has a different type.',
[ENTITY_VALUE_AS_ENTITY_NAME]: 'Entity value should not be equal to entity domain name or entity set name. This rule providing is critical for DDFQL and DDF reader supporting (on WS).',
[DUPLICATED_SYNONYM_KEY]: 'Duplicated synonym key'
[DUPLICATED_SYNONYM_KEY]: 'Duplicated synonym key',
[INCONSISTENT_SYNONYM_KEY]: 'Inconsistent Synonym key'
};

export const howToFix = {
Expand Down Expand Up @@ -188,7 +191,8 @@ export const howToFix = {
[INCORRECT_BOOLEAN_ENTITY]: 'Use only TRUE or FALSE values for concepts of type "boolean"',
[CONCEPT_LOOKS_LIKE_BOOLEAN]: 'Consider changing the concept type to "boolean"',
[ENTITY_VALUE_AS_ENTITY_NAME]: 'Simplest way to fix the issue is entity value renaming, in other case domain or entity set name should be changed.',
[DUPLICATED_SYNONYM_KEY]: 'Remove duplicated key or change it properly'
[DUPLICATED_SYNONYM_KEY]: 'Remove duplicated key or change it properly',
[INCONSISTENT_SYNONYM_KEY]: 'Correct key as (entity and synonym should be defined) in a related ddf--synonyms--xxx.csv file'
};

export const getRulesInformation = () => Object.getOwnPropertySymbols(exports.descriptions)
Expand Down
Loading

0 comments on commit 0738bf2

Please sign in to comment.