-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrong-entity-is-header.ts
88 lines (72 loc) · 3 KB
/
wrong-entity-is-header.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { includes } from 'lodash';
import * as Levenshtein from 'levenshtein';
import { WRONG_ENTITY_IS_HEADER } from '../registry';
import { DdfDataSet } from '../../ddf-definitions/ddf-data-set';
import { Issue } from '../issue';
import { CONCEPT_TYPE_ENTITY_SET, fieldIsPrefix, looksLikeIsField } from '../../utils/ddf-things';
import { DOMAIN_ID } from "../../ddf-definitions/constants";
const SUGGEST_TOLERANCE = 5;
const getInformationAboutNonConcept = (ddfDataSet, actualHeaderDetail, headerDetail) =>
includes(ddfDataSet.getConcept().getIds(), actualHeaderDetail) ? null : {
message: 'Not a concept',
headerDetail
};
const getInformationAboutWrongNonEntityConcept = (ddfDataSet, actualHeaderDetail, headerDetail) => {
const conceptRecord = ddfDataSet.getConcept().getRecordByKey(actualHeaderDetail);
return !conceptRecord || conceptRecord.concept_type !== CONCEPT_TYPE_ENTITY_SET ? {
message: 'Wrong concept type',
headerDetail
} : null;
};
const getInformationForbiddenDomain = (ddfDataSet, actualHeader, fileDescriptor) => {
const domainTypeHash = ddfDataSet.getConcept().getDictionary(null, DOMAIN_ID);
const primaryKeyDomain = domainTypeHash[fileDescriptor.primaryKey] || fileDescriptor.primaryKey;
const currentDomain = domainTypeHash[actualHeader];
const isCurrentDomainExists = includes(ddfDataSet.getConcept().getAllData().map(record => record.concept), currentDomain);
if (primaryKeyDomain === domainTypeHash[actualHeader] || !isCurrentDomainExists) {
return null;
}
return {
message: 'Forbidden domain for the entity set',
currentEntitySet: actualHeader,
currentDomain,
primaryKeyDomain
};
};
const getSuggestions = (ddfDataSet, actualHeader) => {
return ddfDataSet.getConcept().getDataIdsByType(CONCEPT_TYPE_ENTITY_SET)
.map(concept => {
const levenshtein = new Levenshtein(concept, actualHeader);
return {
concept,
distance: levenshtein.distance
};
})
.filter(suggest => suggest.distance < SUGGEST_TOLERANCE)
.map(suggest => suggest.concept);
};
export const rule = {
rule: (ddfDataSet: DdfDataSet) => {
const result = [];
ddfDataSet.getEntity().fileDescriptors.forEach(fileDescriptor => {
fileDescriptor.headers.forEach(header => {
let actualHeader = header;
if (looksLikeIsField(header)) {
actualHeader = header.replace(fieldIsPrefix, '');
const data = getInformationAboutNonConcept(ddfDataSet, actualHeader, header) ||
getInformationAboutWrongNonEntityConcept(ddfDataSet, actualHeader, header) ||
getInformationForbiddenDomain(ddfDataSet, actualHeader, fileDescriptor);
if (data) {
const suggestions = getSuggestions(ddfDataSet, actualHeader);
const issue = new Issue(WRONG_ENTITY_IS_HEADER)
.setPath(fileDescriptor.fullPath)
.setData(data)
.setSuggestions(suggestions);
result.push(issue);
}
}
});
});
return result;
}
};