-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvalidate.js
52 lines (41 loc) · 1.4 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
'use strict';
const { getIssues } = require('@placemarkio/check-geojson');
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 = [];
getIssues(JSON.stringify(gazetteer)).forEach(issue => {
errors.push(issue.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;