-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVPValidate.js
88 lines (69 loc) · 2.98 KB
/
SVPValidate.js
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
const SVPValidate = (geojson) => {
let problematic_record = [];
try {
// Rule: features must exist
if(!geojson.hasOwnProperty('features')) {
throw "features property must exist!"
}
// Rule: features must not be empty
if (geojson.features.length < 1 ) {
throw "features property must contain at leat one element"
}
geojson.features.forEach(f => {
// Geometry must exixt
if (!f.geometry || f.geometry === '') {
throw "Geomety is required"
}
const prop = f.properties;
// Rule: place is required
if (!prop.place || prop.place === '') {
throw "place property is required"
}
// Rule. reconstr must be null, 1, or 2
if (prop.reconstr !== null && prop.reconstr !== 0 && prop.reconstr !== 1 && prop.reconstr !== 2){
problematic_record.push(f);
throw `Invalid value for reconstr: ${prop.reconstr}. Valid values are null, 0, 1 or 2`;
}
// Rule. part nust be one of null, s, u, l, or d
if (prop.part !== null && prop.part !== 's' && prop.part !== 'u' && prop.part !== 'l' && prop.part !== 'd'){
problematic_record.push(f);
throw `Invalid value for part: ${prop.part}. Valid values are null, s, u, l or d`;
}
// Rule: phase must be a number
if (typeof prop.phase !== 'number' && prop.phase !== null) {
problematic_record.push(f);
throw `Invalid value for phase: ${prop.phase}. Only null or numbers are supported`;
}
// Rule: lost must be one of null, 0 or 1
if (prop.lost !== null && prop.lost !== 0 && prop.lost !== 1){
problematic_record.push(f);
throw `Invalid value for lost: ${prop.lost}. Valid values are null, 0 or 1`;
}
// Rule: source is required
if (!prop.source || prop.source === '') {
problematic_record.push(f);
throw "source property is required"
}
// Rule: subsource is required
if (!prop.subsource || prop.subsource === '') {
problematic_record.push(f);
throw "subsource property is required"
}
// Rule: date is requires and should meet the pattern yyyy-mm-dd
if (!prop.date || ! /[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(prop.date)) {
problematic_record.push(f);
throw `Date is required, and should be formatted as yyyy-mm-dd. Invalid ${prop.date}`;
}
});
return {
status: 'success',
text: 'Validation passed!'
};
} catch (error) {
return {
status: 'error',
debug: problematic_record,
text: `Error! Validation failed with message: ${error}`
};
}
};