-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgeospatial.ts
More file actions
93 lines (83 loc) · 2.24 KB
/
geospatial.ts
File metadata and controls
93 lines (83 loc) · 2.24 KB
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
89
90
91
92
93
import Bourne from '@hapi/bourne'
import JoiBase from 'joi'
import {
type Coordinates,
type Feature,
type FeatureProperties,
type Geometry
} from '~/src/server/plugins/engine/types.js'
const Joi = JoiBase.extend({
type: 'array',
base: JoiBase.array(),
messages: {
'object.invalidjson': '{{#label}} must be a valid json array string'
},
coerce: {
from: 'string',
method(value, helpers) {
if (typeof value === 'string') {
if (value.trim() === '') {
return {
value: undefined
}
}
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
return { value: Bourne.parse(value) }
} catch {
const result = {
value,
errors: [helpers.error('object.invalidjson')]
}
return result
}
} else {
return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
value
}
}
}
}
}) as JoiBase.Root
const coordinatesSchema = Joi.array<Coordinates[]>()
.items(Joi.number().required(), Joi.number().required())
.required()
const featurePropertiesSchema = Joi.object<FeatureProperties>()
.keys({
description: Joi.string().required(),
coordinateGridReference: Joi.string().required(),
centroidGridReference: Joi.string().required()
})
.required()
const featureGeometrySchema = Joi.object<Geometry>().keys({
type: Joi.string().valid('Point', 'LineString', 'Polygon').required(),
coordinates: Joi.array()
.when('type', {
switch: [
{ is: 'Point', then: coordinatesSchema },
{
is: 'LineString',
then: Joi.array().items(coordinatesSchema).min(2)
},
{
is: 'Polygon',
then: Joi.array().items(Joi.array().items(coordinatesSchema).min(3))
}
]
})
.required()
})
const featureSchema = Joi.object<Feature>().keys({
id: Joi.string().required(),
type: Joi.string().valid('Feature').required(),
properties: featurePropertiesSchema,
geometry: featureGeometrySchema
})
export const geospatialSchema = Joi.array<Feature[]>()
.items(featureSchema)
.unique('id')
.required()
/**
* @import { CustomHelpers } from 'joi'
*/