-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvalidate.js
63 lines (52 loc) · 1.45 KB
/
validate.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
'use strict';
const geojsonhint = require('@mapbox/geojsonhint');
const Joi = require('@hapi/joi');
const rootLevelSchema = Joi.object().keys({
type: Joi.string()
.valid('FeatureCollection')
.required(),
name: Joi.string().required(),
features: Joi.array().required()
});
const propertiesSchema = Joi.object()
.keys({
place_name: Joi.string().required(),
place_description: Joi.string(),
zoom: Joi.number()
.min(0)
.max(22)
.required(),
pitch: Joi.number()
.integer()
.min(0)
.max(60),
bearing: Joi.number()
.integer()
.min(-179)
.max(180),
tags: Joi.array().items(Joi.string())
})
.unknown();
function validate(gazetteer) {
const errors = [];
geojsonhint.hint(gazetteer).forEach(err => {
errors.push(err.message);
});
// Return early if errors were caught from geojsonhint.
if (errors.length) return errors;
const validateRoot = Joi.validate(gazetteer, rootLevelSchema);
if (validateRoot.error && validateRoot.error.details.length) {
return validateRoot.error.details.map(e => e.message);
}
gazetteer.features.forEach(feature => {
const validateProperties = Joi.validate(
feature.properties,
propertiesSchema
);
if (validateProperties.error && validateProperties.error.details.length) {
validateProperties.error.details.forEach(e => errors.push(e.message));
}
});
return errors;
}
module.exports = validate;