77
88This repository contains GeoJSON schemas for the [ Zod] ( https://github.com/colinhacks/zod ) validation library by [ @colinhacks ] ( https://x.com/colinhacks ) .
99
10- The schemas are based on the GeoJSON specification [ RFC 7946] ( https://datatracker.ietf.org/doc/html/rfc7946 ) . They validate the structure of the GeoJSON objects, types, and the validity of the dimensions, geometries and
11- bounding boxes.
10+ The schemas are based on the GeoJSON specification RFC 7946. They validate the structure of the GeoJSON objects and types, as well as the validity of the dimensions, geometries, and bounding boxes.
1211
1312The schemas and inferred types are designed to be fully compatible with the
1413[ @types/geojson ] ( https://www.npmjs.com/package/@types/geojson ) TypeScript types.
@@ -89,47 +88,46 @@ import type {
8988} from " zod-geojson" ;
9089```
9190
92- ### Dimensionality
91+ ### Strict Position Typing
92+
93+ To maintain "out of the box" compatibility with [ @types/geojson ] ( https://www.npmjs.com/package/@types/geojson ) the
94+ general schemas allow any position dimensionality.
95+
96+ ``` typescript
97+ import { GeoJSONPoint } from " zod-geojson" ;
98+
99+ // These are all valid
100+ const point3D: GeoJSONPoint = { type: " Point" , coordinates: [0 , 0 , 0 ] };
101+ const point1D: GeoJSONPoint = { type: " Point" , coordinates: [0 ] };
102+ const point0D: GeoJSONPoint = { type: " Point" , coordinates: [] };
103+ const point4D: GeoJSONPoint = { type: " Point" , coordinates: [0 , 0 , 0 , 0 ] };
104+ ```
93105
94- This library exports specific schemas for 2D and 3D GeoJSONs, and their accompanying types:
106+ If you want strict position checking at compile time then you can use the provided 2D and 3D schemas/ types.
95107
96108``` typescript
97109import type {
98- // 2D GeoJSON Schemas
110+ // 2D GeoJSON
99111 GeoJSON2DFeatureSchema ,
100112 GeoJSON2DFeature ,
101113 // ...
102- // 2D GeoJSON Types
114+ // 2D GeoJSON Geometries
103115 GeoJSON2DPointSchema ,
104116 GeoJSON2DPoint ,
105117 // ...
106- // 3D GeoJSON Schemas
118+ // 3D GeoJSON
107119 GeoJSON3DFeatureSchema ,
108120 GeoJSON3DFeature ,
109121 // ...
110- // 3D GeoJSON Types
122+ // 3D GeoJSON Geometries
111123 GeoJSON3DPointSchema ,
112124 GeoJSON3DPoint ,
125+ // ...
113126} from " zod-geojson" ;
114127```
115128
116- ### Strict Position Typing
117-
118- To maintain "out of box" compatibility with the ` @types/geojson ` types the general schemas allow any position
119- dimensionality.
120-
121- ``` typescript
122- import { GeoJSONPoint } from " zod-geojson" ;
123-
124- // These are all valid
125- const point3D: GeoJSONPoint = { type: " Point" , coordinates: [0 , 0 , 0 ] };
126- const point1D: GeoJSONPoint = { type: " Point" , coordinates: [0 ] };
127- const point0D: GeoJSONPoint = { type: " Point" , coordinates: [] };
128- const point4D: GeoJSONPoint = { type: " Point" , coordinates: [0 , 0 , 0 , 0 ] };
129- ```
130-
131- If you wish to have strict position typing, you can use the provided 2D and 3D schemas/types. These use strict position
132- typing and will also restrict the bbox field to match the position dimension. For example:
129+ These use zod tuples to ensure positions are checked at compile time. However, to maintain interoperability
130+ with ` @types/geojson ` , bounding box dimensionality is not enforced at the type level, only during runtime validation.
133131
134132``` typescript
135133import { GeoJSON2DPoint } from " zod-geojson" ;
@@ -143,7 +141,7 @@ const point3D: GeoJSON2DPoint = {
143141const point2DWith3DBBox: GeoJSON2DPoint = {
144142 type: " Point" ,
145143 coordinates: [1.0 , 2.0 ],
146- bbox: [0.0 , 0.0 , 3.0 , 4.0 , 0.0 , 0.0 ], // This will fail. BBox has 6 values instead of 4
144+ bbox: [0.0 , 0.0 , 3.0 , 4.0 , 0.0 , 0.0 ], // This is allowed at type level, but will error during validation!
147145};
148146```
149147
@@ -191,7 +189,7 @@ This function takes three parameters:
191189
192190Even if you wish to only customize one of these aspects, you will still need to pass all three parameters to the
193191schema function. For convenience, this library exposes the default schemas which you can use as a base for your
194- custom schemas. There are the following "default" main schemas that you can use if you do not wish to customize
192+ custom schemas. These are the following "default" main schemas that you can use if you do not wish to customize
195193a certain aspect of the schema:
196194
197195- ` GeoJSONPositionSchema ` - The main GeoJSON position schema which allows both 2D and 3D positions
@@ -231,7 +229,19 @@ As discussed above, if you only wish to customize the `properties` field, you wi
231229by this library for this purpose.
232230
233231``` typescript
234- import { GeoJSONFeatureGenericSchema , GeoJSONPositionSchema , GeoJSONGeometrySchema } from " zod-geojson" ;
232+ import {
233+ GeoJSONFeatureGenericSchema ,
234+ GeoJSONPositionSchema ,
235+ GeoJSONPropertiesSchema ,
236+ GeoJSONGeometrySchema ,
237+ } from " zod-geojson" ;
238+
239+ const NonNullPropertiesGeoJSONFeatureSchema = GeoJSONFeatureGenericSchema (
240+ GeoJSONPositionSchema ,
241+ GeoJSONPropertiesSchema .unwrap (),
242+ GeoJSONGeometrySchema ,
243+ );
244+ type NonNullPropertiesGeoJSONFeature = z .infer <typeof NonNullPropertiesGeoJSONFeatureSchema >;
235245
236246const CustomPropertiesSchema = z .object ({
237247 name: z .string (),
0 commit comments