Turf.jsBooleans

turf.booleanValid

What is turf.booleanValid?

turf.booleanValid checks whether a GeoJSON object represents a structurally valid geometry per the OGC Simple Feature Specification. It enforces rules like closed polygon rings, no self-intersections, and minimum coordinate counts.

JavaScript
turf.booleanValid(feature) → boolean

When would you use turf.booleanValid?

Use turf.booleanValid during ingest to reject malformed input before it reaches the rest of your pipeline — importing shapefiles via an external converter, accepting user-drawn polygons from a MapLibre-draw control, or validating GeoJSON coming from a third-party API.

When booleanValid returns false, follow up with turf.cleanCoords, turf.rewind, or manual repairs. Because the check is O(n) over the coordinates, it is cheap enough to run on every ingest.

JavaScript
1if (!turf.booleanValid(userPolygon)) {
2  throw new Error('Drawn polygon is not topologically valid.');
3}

FAQs

How do I install Turf.js to use this function?

Install npm install @turf/boolean-valid and import import { booleanValid } from '@turf/boolean-valid', or use turf.booleanValid from @turf/turf.

What does it check?

OGC Simple Feature rules: closed rings, at least two positions for LineStrings, at least four for Polygon rings, no self-intersecting rings, and so on. It does not check coordinate units or lng/lat ranges.

Is it a substitute for PostGIS's ST_IsValid?

It covers most of the same topology rules and is adequate for client-side validation. PostGIS has a slightly broader repair toolkit — for authoritative validation in a pipeline, mirror the check with ST_IsValid server-side.

How do I fix an invalid geometry?

For closure problems use turf.rewind. For collinear duplicates use turf.cleanCoords. For self-intersections you may need a buffer-by-zero (turf.buffer(poly, 0)) or an external library such as polygon-clipping.