From e395b7013735681bad9a64b64b5fe347edc25d8f Mon Sep 17 00:00:00 2001 From: Reinert Lemmens Date: Wed, 23 Oct 2024 23:13:24 +0200 Subject: [PATCH] Improve top level geojson negative type tests --- tests/geojson.test.ts | 113 ++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 76 deletions(-) diff --git a/tests/geojson.test.ts b/tests/geojson.test.ts index 5fa0bf4..0d1ac7e 100644 --- a/tests/geojson.test.ts +++ b/tests/geojson.test.ts @@ -3,7 +3,17 @@ import { ZodError } from "zod"; import { geoJsonFeaturePolygon2D } from "../examples/feature"; import { multiGeoJsonFeatureCollection2D } from "../examples/feature_collection"; import { geoJsonPoint3D } from "../examples/geometry/point"; -import { GeoJSON, GeoJSON2D, GeoJSON2DSchema, GeoJSON3D, GeoJSON3DSchema, GeoJSONSchema } from "../src"; +import { + GeoJSON, + GeoJSON2D, + GeoJSON2DGeometry, + GeoJSON2DSchema, + GeoJSON3D, + GeoJSON3DGeometry, + GeoJSON3DSchema, + GeoJSONGeometry, + GeoJSONSchema, +} from "../src"; describe("GeoJSONSchema", () => { it("allows a basic geometry", () => { @@ -42,21 +52,22 @@ describe("GeoJSONSchema", () => { /** * Invalid GeoJSON to test types */ +const testGeometry: GeoJSONGeometry = { type: "Point", coordinates: [0.0, 0.0] }; export const invalidGeoJsonPoint: GeoJSON = { type: "Point", // @ts-expect-error -- THIS SHOULD FAIL coordinates: [1.0], // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0, 2.0], - // @ts-expect-error -- THIS SHOULD FAIL +}; +// @ts-expect-error -- THIS SHOULD FAIL +export const invalidGeoJsonPointProperties: GeoJSON = { + type: "Point", + coordinates: [0.0, 0.0], + properties: {}, + geometry: testGeometry, features: [], - // @ts-expect-error -- THIS SHOULD FAIL geometries: [], - // @ts-expect-error -- THIS SHOULD FAIL - properties: {}, - // @ts-expect-error -- THIS SHOULD FAIL - geometry: {}, - otherKey: "allowed", }; export const invalidGeoJsonGeometryCollection: GeoJSON = { type: "GeometryCollection", @@ -70,11 +81,6 @@ export const invalidGeoJsonGeometryCollection: GeoJSON = { ], // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], - // @ts-expect-error -- THIS SHOULD FAIL - features: [], - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL properties: {}, // @ts-expect-error -- THIS SHOULD FAIL geometry: {}, @@ -100,10 +106,6 @@ export const invalidGeoJsonFeature: GeoJSON = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], // @ts-expect-error -- THIS SHOULD FAIL - features: [], - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL geometries: {}, otherKey: "allowed", }; @@ -139,20 +141,12 @@ export const invalidGeoJsonFeatureCollection: GeoJSON = { ], // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], - // @ts-expect-error -- THIS SHOULD FAIL - geometries: [], - // @ts-expect-error -- THIS SHOULD FAIL - geometry: {}, - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL - properties: {}, - otherKey: "allowed", }; /** * Invalid 2D GeoJSON to test types */ +const testGeometry2D: GeoJSON2DGeometry = { type: "Point", coordinates: [0.0, 0.0] }; export const invalidGeoJson2DPoint: GeoJSON2D = { type: "Point", // @ts-expect-error -- THIS SHOULD FAIL @@ -160,14 +154,16 @@ export const invalidGeoJson2DPoint: GeoJSON2D = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0, 2.0], // @ts-expect-error -- THIS SHOULD FAIL + geometry: {}, +}; +// @ts-expect-error -- THIS SHOULD FAIL +export const invalidGeoJson2DPointProperties: GeoJSON2D = { + type: "Point", + coordinates: [0.0, 0.0], + properties: {}, + geometry: testGeometry2D, features: [], - // @ts-expect-error -- THIS SHOULD FAIL geometries: [], - // @ts-expect-error -- THIS SHOULD FAIL - properties: {}, - // @ts-expect-error -- THIS SHOULD FAIL - geometry: {}, - otherKey: "allowed", }; export const invalidGeoJson2DGeometryCollection: GeoJSON2D = { type: "GeometryCollection", @@ -182,14 +178,7 @@ export const invalidGeoJson2DGeometryCollection: GeoJSON2D = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], // @ts-expect-error -- THIS SHOULD FAIL - features: [], - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL - properties: {}, - // @ts-expect-error -- THIS SHOULD FAIL geometry: {}, - otherKey: "allowed", }; export const invalidGeoJson2DFeature: GeoJSON2D = { type: "Feature", @@ -211,12 +200,7 @@ export const invalidGeoJson2DFeature: GeoJSON2D = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], // @ts-expect-error -- THIS SHOULD FAIL - features: [], - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL geometries: {}, - otherKey: "allowed", }; export const invalidGeoJson2DFeatureCollection: GeoJSON2D = { type: "FeatureCollection", @@ -251,19 +235,13 @@ export const invalidGeoJson2DFeatureCollection: GeoJSON2D = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], // @ts-expect-error -- THIS SHOULD FAIL - geometries: [], - // @ts-expect-error -- THIS SHOULD FAIL geometry: {}, - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL - properties: {}, - otherKey: "allowed", }; /** * Invalid 3D GeoJSON to test types */ +const testGeometry3D: GeoJSON3DGeometry = { type: "Point", coordinates: [0.0, 0.0, 0.0] }; export const invalidGeoJson3DPoint: GeoJSON3D = { type: "Point", // @ts-expect-error -- THIS SHOULD FAIL @@ -271,14 +249,16 @@ export const invalidGeoJson3DPoint: GeoJSON3D = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0, 2.0], // @ts-expect-error -- THIS SHOULD FAIL + geometry: {}, +}; +// @ts-expect-error -- THIS SHOULD FAIL +export const invalidGeoJson3DPointProperties: GeoJSON3D = { + type: "Point", + coordinates: [0.0, 0.0, 0.0], + properties: {}, + geometry: testGeometry3D, features: [], - // @ts-expect-error -- THIS SHOULD FAIL geometries: [], - // @ts-expect-error -- THIS SHOULD FAIL - properties: {}, - // @ts-expect-error -- THIS SHOULD FAIL - geometry: {}, - otherKey: "allowed", }; export const invalidGeoJson3DGeometryCollection: GeoJSON3D = { type: "GeometryCollection", @@ -293,14 +273,7 @@ export const invalidGeoJson3DGeometryCollection: GeoJSON3D = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], // @ts-expect-error -- THIS SHOULD FAIL - features: [], - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL - properties: {}, - // @ts-expect-error -- THIS SHOULD FAIL geometry: {}, - otherKey: "allowed", }; export const invalidGeoJson3DFeature: GeoJSON3D = { type: "Feature", @@ -322,12 +295,7 @@ export const invalidGeoJson3DFeature: GeoJSON3D = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], // @ts-expect-error -- THIS SHOULD FAIL - features: [], - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL geometries: {}, - otherKey: "allowed", }; export const invalidGeoJson3DFeatureCollection: GeoJSON3D = { type: "FeatureCollection", @@ -362,12 +330,5 @@ export const invalidGeoJson3DFeatureCollection: GeoJSON3D = { // @ts-expect-error -- THIS SHOULD FAIL bbox: [1.0], // @ts-expect-error -- THIS SHOULD FAIL - geometries: [], - // @ts-expect-error -- THIS SHOULD FAIL geometry: {}, - // @ts-expect-error -- THIS SHOULD FAIL - coordinates: [], - // @ts-expect-error -- THIS SHOULD FAIL - properties: {}, - otherKey: "allowed", };