Skip to content

Commit

Permalink
Remove barrel files + export only relevant schemas & types
Browse files Browse the repository at this point in the history
  • Loading branch information
reilem committed Oct 22, 2024
1 parent 7123f5d commit aee3f44
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/feature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod";
import { GeoJSONBaseSchema } from "./base";
import { GeoJSONGeometryGenericSchema } from "./geometry";
import { GeoJSONGeometryGenericSchema } from "./geometry/geometry";
import { INVALID_BBOX_ISSUE } from "./geometry/validation/bbox";
import { GeoJSON2DPositionSchema, GeoJSON3DPositionSchema, GeoJSONPosition, GeoJSONPositionSchema } from "./position";
import { GeoJSONTypeSchema } from "./type";
Expand Down
21 changes: 21 additions & 0 deletions src/geojson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { z } from "zod";
import { GeoJSONFeatureGenericSchema } from "./feature";
import { GeoJSONFeatureCollectionGenericSchema } from "./feature_collection";
import { GeoJSONGeometryGenericSchema } from "./geometry/geometry";
import { GeoJSON2DPositionSchema, GeoJSON3DPositionSchema, GeoJSONPosition, GeoJSONPositionSchema } from "./position";

export const GeoJSONGenericSchema = <P extends GeoJSONPosition>(positionSchema: z.ZodSchema<P>) =>
z.union([
GeoJSONGeometryGenericSchema(positionSchema),
GeoJSONFeatureGenericSchema(positionSchema),
GeoJSONFeatureCollectionGenericSchema(positionSchema),
]);

export const GeoJSONSchema = GeoJSONGenericSchema(GeoJSONPositionSchema);
export type GeoJSON = z.infer<typeof GeoJSONSchema>;

export const GeoJSON2DSchema = GeoJSONGenericSchema(GeoJSON2DPositionSchema);
export type GeoJSON2D = z.infer<typeof GeoJSON2DSchema>;

export const GeoJSON3DSchema = GeoJSONGenericSchema(GeoJSON3DPositionSchema);
export type GeoJSON3D = z.infer<typeof GeoJSON3DSchema>;
9 changes: 0 additions & 9 deletions src/geometry/index.ts → src/geometry/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import {
GeoJSONGeometryCollectionGenericSchemaType,
} from "./geometry_collection";

export * from "./geometry_collection";
export * from "./line_string";
export * from "./multi_line_string";
export * from "./multi_point";
export * from "./multi_polygon";
export * from "./point";
export * from "./polygon";
export * from "./helper/type";

export type GeoJSONGeometryGenericSchemaType<P extends GeoJSONPosition> = z.ZodUnion<
[GeoJSONSimpleGeometryGenericSchemaType<P>, GeoJSONGeometryCollectionGenericSchemaType<P>]
>;
Expand Down
162 changes: 131 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,132 @@
// Derived from the GeoJSON spec: https://datatracker.ietf.org/doc/html/rfc7946
import { z } from "zod";
import { GeoJSONFeatureGenericSchema } from "./feature";
import { GeoJSONFeatureCollectionGenericSchema } from "./feature_collection";
import { GeoJSONGeometryGenericSchema } from "./geometry";
import { GeoJSON2DPositionSchema, GeoJSON3DPositionSchema, GeoJSONPosition, GeoJSONPositionSchema } from "./position";

export * from "./feature";
export * from "./feature_collection";
export * from "./geometry";
export * from "./bbox";
export * from "./position";
export * from "./type";

export const GeoJSONGenericSchema = <P extends GeoJSONPosition>(positionSchema: z.ZodSchema<P>) =>
z.union([
GeoJSONGeometryGenericSchema(positionSchema),
GeoJSONFeatureGenericSchema(positionSchema),
GeoJSONFeatureCollectionGenericSchema(positionSchema),
]);

export const GeoJSONSchema = GeoJSONGenericSchema(GeoJSONPositionSchema);
export type GeoJSON = z.infer<typeof GeoJSONSchema>;

export const GeoJSON2DSchema = GeoJSONGenericSchema(GeoJSON2DPositionSchema);
export type GeoJSON2D = z.infer<typeof GeoJSON2DSchema>;

export const GeoJSON3DSchema = GeoJSONGenericSchema(GeoJSON3DPositionSchema);
export type GeoJSON3D = z.infer<typeof GeoJSON3DSchema>;

// TODO: Do not expose every single schema & inner type, only the useful ones
// TODO: Add negative typing examples for all types (like with point)
// TODO: Add negative typing examples for all types (like in point tests)
// TODO: Make sure each exposed type & schema is tested

export {
GeoJSONGeometryTypeSchema,
type GeoJSONGeometryEnumType,
type GeoJSONGeometryType,
} from "./geometry/helper/type";

export {
GeoJSONGeometryGenericSchema,
GeoJSONGeometrySchema,
type GeoJSONGeometry,
GeoJSON2DGeometrySchema,
type GeoJSON2DGeometry,
GeoJSON3DGeometrySchema,
type GeoJSON3DGeometry,
} from "./geometry/geometry";

export {
GeoJSONGeometryCollectionGenericSchema,
GeoJSONGeometryCollectionSchema,
type GeoJSONGeometryCollection,
GeoJSON2DGeometryCollectionSchema,
type GeoJSON2DGeometryCollection,
GeoJSON3DGeometryCollectionSchema,
type GeoJSON3DGeometryCollection,
} from "./geometry/geometry_collection";

export {
GeoJSONLineStringGenericSchema,
GeoJSONLineStringSchema,
type GeoJSONLineString,
GeoJSON2DLineStringSchema,
type GeoJSON2DLineString,
GeoJSON3DLineStringSchema,
type GeoJSON3DLineString,
} from "./geometry/line_string";

export {
GeoJSONMultiLineStringGenericSchema,
GeoJSONMultiLineStringSchema,
type GeoJSONMultiLineString,
GeoJSON2DMultiLineStringSchema,
type GeoJSON2DMultiLineString,
GeoJSON3DMultiLineStringSchema,
type GeoJSON3DMultiLineString,
} from "./geometry/multi_line_string";

export {
GeoJSONMultiPointGenericSchema,
GeoJSONMultiPointSchema,
type GeoJSONMultiPoint,
GeoJSON2DMultiPointSchema,
type GeoJSON2DMultiPoint,
GeoJSON3DMultiPointSchema,
type GeoJSON3DMultiPoint,
} from "./geometry/multi_point";

export {
GeoJSONMultiPolygonGenericSchema,
GeoJSONMultiPolygonSchema,
type GeoJSONMultiPolygon,
GeoJSON2DMultiPolygonSchema,
type GeoJSON2DMultiPolygon,
GeoJSON3DMultiPolygonSchema,
type GeoJSON3DMultiPolygon,
} from "./geometry/multi_polygon";

export {
GeoJSONPointGenericSchema,
GeoJSONPointSchema,
type GeoJSONPoint,
GeoJSON2DPointSchema,
type GeoJSON2DPoint,
GeoJSON3DPointSchema,
type GeoJSON3DPoint,
} from "./geometry/point";

export {
GeoJSONPolygonGenericSchema,
GeoJSONPolygonSchema,
type GeoJSONPolygon,
GeoJSON2DPolygonSchema,
type GeoJSON2DPolygon,
GeoJSON3DPolygonSchema,
type GeoJSON3DPolygon,
} from "./geometry/polygon";

export { GeoJSONBboxSchema, type GeoJSONBbox } from "./bbox";

export {
GeoJSONFeatureGenericSchema,
GeoJSONFeatureSchema,
type GeoJSONFeature,
GeoJSON2DFeatureSchema,
type GeoJSON2DFeature,
GeoJSON3DFeatureSchema,
type GeoJSON3DFeature,
} from "./feature";

export {
GeoJSONFeatureCollectionGenericSchema,
GeoJSONFeatureCollectionSchema,
type GeoJSONFeatureCollection,
GeoJSON2DFeatureCollectionSchema,
type GeoJSON2DFeatureCollection,
GeoJSON3DFeatureCollectionSchema,
type GeoJSON3DFeatureCollection,
} from "./feature_collection";

export {
GeoJSONGenericSchema,
GeoJSONSchema,
type GeoJSON,
GeoJSON2DSchema,
type GeoJSON2D,
GeoJSON3DSchema,
type GeoJSON3D,
} from "./geojson";

export {
GeoJSONPositionSchema,
type GeoJSONPosition,
GeoJSON2DPositionSchema,
type GeoJSON2DPosition,
GeoJSON3DPositionSchema,
type GeoJSON3DPosition,
} from "./position";

export { GeoJSONTypeSchema, type GeoJSONType } from "./type";
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GeoJSON types and Geometry type (see 1.4)
import { z } from "zod";
import { GeoJSONGeometryTypeSchema } from "./geometry";
import { GeoJSONGeometryTypeSchema } from "./geometry/helper/type";

export const GeoJSONTypeSchema = z.enum(["Feature", "FeatureCollection", ...GeoJSONGeometryTypeSchema.options]);

Expand Down

0 comments on commit aee3f44

Please sign in to comment.