/
index.js
79 lines (72 loc) · 2.09 KB
/
index.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
import types from 'sequelize'
import moment from 'moment-timezone'
import isNumber from 'is-number'
import isObject from 'is-plain-obj'
const getBasicGeoJSONIssues = (v, type) => {
if (!isObject(v)) return 'Not a valid object'
if (v.type !== type) return `Not a valid type value (Expected ${type} not ${v.type})`
}
// test is used to validate and type user-inputted values
// hydrate is used to hydrate db text values to their properly typed values
export const array = {
name: 'List',
items: true,
check: (v) => Array.isArray(v),
// TODO: recursively map the array against the right types
// this treats everything as a text array
// probably need to pass in type and let the db figure out hydrating
hydrate: (txt) => types.fn('fix_jsonb_array', txt)
}
export const object = {
name: 'Map',
check: isObject,
hydrate: (txt) => types.cast(txt, 'jsonb')
}
export const text = {
name: 'Text',
check: (v) => typeof v === 'string',
hydrate: (txt) => txt
}
export const number = {
name: 'Number',
check: (v) => isNumber(v),
hydrate: (txt) => types.cast(txt, 'numeric')
}
export const boolean = {
name: 'True/False',
check: (v) => typeof v === 'boolean',
hydrate: (txt) => types.cast(txt, 'boolean')
}
export const date = {
name: 'Date/Time',
check: (v) => moment(v, moment.ISO_8601).isValid(),
hydrate: (txt) => types.fn('parse_iso', txt)
}
// geo (EPSG:4979 / WGS84)
const geoCast = (txt) =>
types.fn('ST_GeomFromGeoJSON', txt)
export const point = {
name: 'GeoJSON Point',
check: (v) => !getBasicGeoJSONIssues(v, 'Point'),
hydrate: geoCast
}
export const line = {
name: 'GeoJSON LineString',
check: (v) => !getBasicGeoJSONIssues(v, 'LineString'),
hydrate: geoCast
}
export const multiline = {
name: 'GeoJSON MultiLineString',
check: (v) => !getBasicGeoJSONIssues(v, 'MultiLineString'),
hydrate: geoCast
}
export const polygon = {
name: 'GeoJSON Polygon',
check: (v) => !getBasicGeoJSONIssues(v, 'Polygon'),
hydrate: geoCast
}
export const multipolygon = {
name: 'GeoJSON MultiPolygon',
check: (v) => !getBasicGeoJSONIssues(v, 'MultiPolygon'),
hydrate: geoCast
}