zod-geojson 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +107 -0
- package/lib/base.d.ts +9 -0
- package/lib/base.d.ts.map +1 -0
- package/lib/base.js +8 -0
- package/lib/bbox.d.ts +4 -0
- package/lib/bbox.d.ts.map +1 -0
- package/lib/bbox.js +9 -0
- package/lib/feature.d.ts +634 -0
- package/lib/feature.d.ts.map +1 -0
- package/lib/feature.js +37 -0
- package/lib/feature_collection.d.ts +3174 -0
- package/lib/feature_collection.d.ts.map +1 -0
- package/lib/feature_collection.js +61 -0
- package/lib/geometry/_simple.d.ts +122 -0
- package/lib/geometry/_simple.d.ts.map +1 -0
- package/lib/geometry/_simple.js +14 -0
- package/lib/geometry/geometry_collection.d.ts +18 -0
- package/lib/geometry/geometry_collection.d.ts.map +1 -0
- package/lib/geometry/geometry_collection.js +60 -0
- package/lib/geometry/index.d.ts +131 -0
- package/lib/geometry/index.d.ts.map +1 -0
- package/lib/geometry/index.js +27 -0
- package/lib/geometry/line_string.d.ts +25 -0
- package/lib/geometry/line_string.d.ts.map +1 -0
- package/lib/geometry/line_string.js +33 -0
- package/lib/geometry/multi_line_string.d.ts +24 -0
- package/lib/geometry/multi_line_string.d.ts.map +1 -0
- package/lib/geometry/multi_line_string.js +32 -0
- package/lib/geometry/multi_point.d.ts +24 -0
- package/lib/geometry/multi_point.d.ts.map +1 -0
- package/lib/geometry/multi_point.js +32 -0
- package/lib/geometry/multi_polygon.d.ts +24 -0
- package/lib/geometry/multi_polygon.d.ts.map +1 -0
- package/lib/geometry/multi_polygon.js +44 -0
- package/lib/geometry/point.d.ts +24 -0
- package/lib/geometry/point.d.ts.map +1 -0
- package/lib/geometry/point.js +25 -0
- package/lib/geometry/polygon.d.ts +28 -0
- package/lib/geometry/polygon.d.ts.map +1 -0
- package/lib/geometry/polygon.js +51 -0
- package/lib/geometry/validation/bbox.d.ts +50 -0
- package/lib/geometry/validation/bbox.d.ts.map +1 -0
- package/lib/geometry/validation/bbox.js +184 -0
- package/lib/geometry/validation/dimension.d.ts +16 -0
- package/lib/geometry/validation/dimension.d.ts.map +1 -0
- package/lib/geometry/validation/dimension.js +54 -0
- package/lib/geometry/validation/keys.d.ts +6 -0
- package/lib/geometry/validation/keys.d.ts.map +1 -0
- package/lib/geometry/validation/keys.js +14 -0
- package/lib/index.d.ts +3930 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +29 -0
- package/lib/position.d.ts +5 -0
- package/lib/position.d.ts.map +1 -0
- package/lib/position.js +8 -0
- package/lib/type.d.ts +6 -0
- package/lib/type.d.ts.map +1 -0
- package/lib/type.js +15 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Reinert Lemmens
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Zod GeoJSON
|
|
2
|
+
|
|
3
|
+
This repository contains GeoJSON schemas for the [Zod](https://github.com/colinhacks/zod) validation library.
|
|
4
|
+
|
|
5
|
+
The schemas are based on the GeoJSON specification found in this RFC: https://datatracker.ietf.org/doc/html/rfc7946. The
|
|
6
|
+
schemas do not only validate the basic structure of the GeoJSON objects but also the validity of the geometries and
|
|
7
|
+
bounding boxes.
|
|
8
|
+
|
|
9
|
+
## Getting Started
|
|
10
|
+
|
|
11
|
+
To use these schemas, you can install the package from npm:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# With NPM
|
|
15
|
+
npm install zod-geojson
|
|
16
|
+
# With Yarn
|
|
17
|
+
yarn add zod-geojson
|
|
18
|
+
# With PNPM
|
|
19
|
+
pnpm add zod-geojson
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage Example
|
|
23
|
+
|
|
24
|
+
Then you can use the schemas in your code:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { GeoJSONSchema } from "zod-geojson";
|
|
28
|
+
|
|
29
|
+
const schema = GeoJSONSchema.parse({
|
|
30
|
+
type: "Feature",
|
|
31
|
+
geometry: {
|
|
32
|
+
type: "Point",
|
|
33
|
+
coordinates: [0, 0],
|
|
34
|
+
},
|
|
35
|
+
properties: {
|
|
36
|
+
name: "Null Island",
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This library also exposes schemas for the individual GeoJSON types:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import {
|
|
45
|
+
GeoJSONFeatureSchema,
|
|
46
|
+
GeoJSONFeatureCollectionSchema,
|
|
47
|
+
GeoJSONGeometrySchema,
|
|
48
|
+
GeoJSONGeometryCollectionSchema,
|
|
49
|
+
GeoJSONMultiPolygonSchema,
|
|
50
|
+
GeoJSONMultiLineStringSchema,
|
|
51
|
+
GeoJSONMultiPointSchema,
|
|
52
|
+
GeoJSONPolygonSchema,
|
|
53
|
+
GeoJSONLineStringSchema,
|
|
54
|
+
GeoJSONPointSchema,
|
|
55
|
+
} from "zod-geojson";
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
It also exposes the resulting types from the schemas:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import type {
|
|
62
|
+
GeoJSONFeature,
|
|
63
|
+
GeoJSONFeatureCollection,
|
|
64
|
+
GeoJSONGeometry,
|
|
65
|
+
GeoJSONGeometryCollection,
|
|
66
|
+
GeoJSONMultiPolygon,
|
|
67
|
+
GeoJSONMultiLineString,
|
|
68
|
+
GeoJSONMultiPoint,
|
|
69
|
+
GeoJSONPolygon,
|
|
70
|
+
GeoJSONLineString,
|
|
71
|
+
GeoJSONPoint,
|
|
72
|
+
} from "zod-geojson";
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Error Cases
|
|
76
|
+
|
|
77
|
+
This library will throw an error if the GeoJSON object is not valid. For example, where the coordinates type does
|
|
78
|
+
not match the geometry type:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { GeoJSONSchema } from "zod-geojson";
|
|
82
|
+
|
|
83
|
+
const schema = GeoJSONSchema.parse({
|
|
84
|
+
type: "Point",
|
|
85
|
+
coordinates: [[0, 0]],
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
It will also throw an error if the bounding box does not match the geometry:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { GeoJSONSchema } from "zod-geojson";
|
|
93
|
+
|
|
94
|
+
const schema = GeoJSONSchema.parse({
|
|
95
|
+
type: "MultiPoint",
|
|
96
|
+
coordinates: [
|
|
97
|
+
[-2, 0],
|
|
98
|
+
[3, 4],
|
|
99
|
+
[2, -1],
|
|
100
|
+
],
|
|
101
|
+
bbox: [0, 0, 1, 1],
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Contributing
|
|
106
|
+
|
|
107
|
+
If you find any issues with the schemas or want to add new features, feel free to open an issue or a pull request.
|
package/lib/base.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const GeoJSONBaseSchema: z.ZodObject<{
|
|
3
|
+
bbox: z.ZodOptional<z.ZodEffects<z.ZodArray<z.ZodNumber, "many">, number[], number[]>>;
|
|
4
|
+
}, "strip", z.ZodTypeAny, {
|
|
5
|
+
bbox?: number[] | undefined;
|
|
6
|
+
}, {
|
|
7
|
+
bbox?: number[] | undefined;
|
|
8
|
+
}>;
|
|
9
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,eAAO,MAAM,iBAAiB;;;;;;EAE5B,CAAC"}
|
package/lib/base.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeoJSONBaseSchema = void 0;
|
|
4
|
+
var zod_1 = require("zod");
|
|
5
|
+
var bbox_1 = require("./bbox");
|
|
6
|
+
exports.GeoJSONBaseSchema = zod_1.z.object({
|
|
7
|
+
bbox: bbox_1.GeoJSONBBoxSchema.optional(),
|
|
8
|
+
});
|
package/lib/bbox.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bbox.d.ts","sourceRoot":"","sources":["../src/bbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,eAAO,MAAM,iBAAiB,mEAGmE,CAAC;AAClG,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC"}
|
package/lib/bbox.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeoJSONBBoxSchema = void 0;
|
|
4
|
+
var zod_1 = require("zod");
|
|
5
|
+
var position_1 = require("./position");
|
|
6
|
+
exports.GeoJSONBBoxSchema = zod_1.z
|
|
7
|
+
.array(zod_1.z.number())
|
|
8
|
+
.min(position_1.MIN_POSITION * 2)
|
|
9
|
+
.refine(function (bbox) { return bbox.length % 2 === 0; }, "Bounding box must have an even number of elements");
|