Skip to content

Commit

Permalink
feat: normalise validations across the codebase so that they return v…
Browse files Browse the repository at this point in the history
…alid and reason properties (#381)
  • Loading branch information
JamesLMilner authored Dec 17, 2024
1 parent 7dc2160 commit 775315f
Show file tree
Hide file tree
Showing 35 changed files with 350 additions and 233 deletions.
19 changes: 2 additions & 17 deletions development/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,7 @@ const getModes = () => {
circle: {
feature: {
validation: (feature) => {
if (feature.geometry.type !== "Polygon") {
return {
valid: false,
reason: "Feature is not a valid Polygon feature",
};
}

const result = ValidateMinAreaSquareMeters(feature, 1000);
return {
valid: result,
reason: result ? undefined : "Area too small",
};
return ValidateMinAreaSquareMeters(feature, 1000);
},
draggable: true,
coordinates: {
Expand Down Expand Up @@ -189,11 +178,7 @@ const getModes = () => {
snapping: true,
validation: (feature, { updateType }) => {
if (updateType === "finish" || updateType === "commit") {
const valid = ValidateNotSelfIntersecting(feature);
return {
valid,
reason: valid ? undefined : "Polygon must not self-intersect",
};
return ValidateNotSelfIntersecting(feature);
}
return { valid: true };
},
Expand Down
12 changes: 2 additions & 10 deletions e2e/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,12 @@ const example = {
this.config?.includes("validationSuccess") ||
this.config?.includes("validationFailure")
? (feature) => {
const result = ValidateMaxAreaSquareMeters(
return ValidateMaxAreaSquareMeters(
feature,
this.config?.includes("validationFailure")
? 1000000
: 2000000,
);
return {
valid: result,
reason: result ? undefined : "Area too large",
};
}
: undefined,
draggable: true,
Expand Down Expand Up @@ -155,16 +151,12 @@ const example = {
this.config?.includes("validationSuccess") ||
this.config?.includes("validationFailure")
? (feature) => {
const result = ValidateMaxAreaSquareMeters(
return ValidateMaxAreaSquareMeters(
feature,
this.config?.includes("validationFailure")
? 1000000
: 2000000,
);
return {
valid: result,
reason: "Area too large",
};
}
: undefined,
}),
Expand Down
23 changes: 8 additions & 15 deletions src/modes/angled-rectangle/angled-rectangle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
TerraDrawBaseDrawMode,
BaseModeOptions,
CustomStyling,
ModeMismatchValidationFailure,
} from "../base.mode";
import { coordinatesIdentical } from "../../geometry/coordinates-identical";
import { getDefaultStyling } from "../../util/styling";
Expand All @@ -21,10 +20,7 @@ import {
GeoJSONStoreFeatures,
StoreValidation,
} from "../../store/store";
import {
ValidateNonIntersectingPolygonFeature,
ValidatePolygonFeature,
} from "../../validations/polygon.validation";
import { ValidateNonIntersectingPolygonFeature } from "../../validations/polygon.validation";
import { webMercatorDestination } from "../../geometry/measure/destination";
import { webMercatorBearing } from "../../geometry/measure/bearing";
import { midpointCoordinate } from "../../geometry/midpoint-coordinate";
Expand Down Expand Up @@ -253,7 +249,7 @@ export class TerraDrawAngledRectangleMode extends TerraDrawBaseDrawMode<PolygonS
} as Polygon;

if (this.validate) {
const valid = this.validate(
const validationResult = this.validate(
{
type: "Feature",
geometry: updatedGeometry,
Expand All @@ -266,7 +262,7 @@ export class TerraDrawAngledRectangleMode extends TerraDrawBaseDrawMode<PolygonS
},
);

if (!valid.valid) {
if (!validationResult.valid) {
return false;
}
}
Expand Down Expand Up @@ -418,14 +414,11 @@ export class TerraDrawAngledRectangleMode extends TerraDrawBaseDrawMode<PolygonS
}

validateFeature(feature: unknown): StoreValidation {
return this.validateModeFeature(
feature,
(baseValidatedFeature) =>
ValidateNonIntersectingPolygonFeature(
baseValidatedFeature,
this.coordinatePrecision,
),
"Feature is not a valid simple Polygon feature",
return this.validateModeFeature(feature, (baseValidatedFeature) =>
ValidateNonIntersectingPolygonFeature(
baseValidatedFeature,
this.coordinatePrecision,
),
);
}
}
16 changes: 5 additions & 11 deletions src/modes/base.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export enum ModeTypes {
Render = "render",
}

type BaseValidationResult = { valid: boolean; reason?: string };

export type BaseModeOptions<T extends CustomStyling> = {
styles?: Partial<T>;
pointerDistance?: number;
Expand Down Expand Up @@ -157,11 +155,11 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
}
}

validateFeature(feature: unknown): BaseValidationResult {
validateFeature(feature: unknown): ReturnType<Validation> {
return this.performFeatureValidation(feature);
}

private performFeatureValidation(feature: unknown): BaseValidationResult {
private performFeatureValidation(feature: unknown): ReturnType<Validation> {
if (this._state === "unregistered") {
throw new Error("Mode must be registered");
}
Expand Down Expand Up @@ -196,9 +194,8 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {

protected validateModeFeature(
feature: unknown,
modeValidationFn: (feature: GeoJSONStoreFeatures) => boolean,
defaultError: string,
): BaseValidationResult {
modeValidationFn: (feature: GeoJSONStoreFeatures) => ReturnType<Validation>,
): ReturnType<Validation> {
const validation = this.performFeatureValidation(feature);
if (validation.valid) {
const validatedFeature = feature as GeoJSONStoreFeatures;
Expand All @@ -207,10 +204,7 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
return ModeMismatchValidationFailure;
}
const modeValidation = modeValidationFn(validatedFeature);
return {
valid: modeValidation,
reason: modeValidation ? undefined : defaultError,
};
return modeValidation;
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/modes/circle/circle.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ describe("TerraDrawCircleMode", () => {
},
}),
).toEqual({
reason: "Feature is not a valid simple Polygon feature",
reason: "Feature has less than 4 coordinates",
valid: false,
});
});
Expand Down
14 changes: 5 additions & 9 deletions src/modes/circle/circle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { getDefaultStyling } from "../../util/styling";
import {
BaseModeOptions,
CustomStyling,
ModeMismatchValidationFailure,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { ValidateNonIntersectingPolygonFeature } from "../../validations/polygon.validation";
Expand Down Expand Up @@ -280,14 +279,11 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl
}

validateFeature(feature: unknown): StoreValidation {
return this.validateModeFeature(
feature,
(baseValidatedFeature) =>
ValidateNonIntersectingPolygonFeature(
baseValidatedFeature,
this.coordinatePrecision,
),
"Feature is not a valid simple Polygon feature",
return this.validateModeFeature(feature, (baseValidatedFeature) =>
ValidateNonIntersectingPolygonFeature(
baseValidatedFeature,
this.coordinatePrecision,
),
);
}

Expand Down
15 changes: 6 additions & 9 deletions src/modes/freehand/freehand.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygon
if (this.validate && finishedId) {
const currentGeometry = this.store.getGeometryCopy<Polygon>(finishedId);

const valid = this.validate(
const validationResult = this.validate(
{
type: "Feature",
id: finishedId,
Expand All @@ -137,7 +137,7 @@ export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygon
},
);

if (!valid.valid) {
if (!validationResult.valid) {
return;
}
}
Expand Down Expand Up @@ -243,7 +243,7 @@ export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygon
} as Polygon;

if (this.validate) {
const valid = this.validate(
const validationResult = this.validate(
{
type: "Feature",
id: this.currentId,
Expand All @@ -258,7 +258,7 @@ export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygon
},
);

if (!valid.valid) {
if (!validationResult.valid) {
return;
}
}
Expand Down Expand Up @@ -433,11 +433,8 @@ export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygon
}

validateFeature(feature: unknown): StoreValidation {
return this.validateModeFeature(
feature,
(baseValidatedFeature) =>
ValidatePolygonFeature(baseValidatedFeature, this.coordinatePrecision),
"Feature is not a valid Polygon feature",
return this.validateModeFeature(feature, (baseValidatedFeature) =>
ValidatePolygonFeature(baseValidatedFeature, this.coordinatePrecision),
);
}
}
6 changes: 3 additions & 3 deletions src/modes/linestring/linestring.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ describe("TerraDrawLineStringMode", () => {
lineStringMode = new TerraDrawLineStringMode({
validation: (feature, { updateType }) => {
if (updateType === "finish" || updateType === "commit") {
return { valid: ValidateNotSelfIntersecting(feature) };
return ValidateNotSelfIntersecting(feature);
}
return { valid: true };
},
Expand Down Expand Up @@ -385,7 +385,7 @@ describe("TerraDrawLineStringMode", () => {
lineStringMode = new TerraDrawLineStringMode({
validation: (feature, { updateType }) => {
if (updateType === "finish" || updateType === "commit") {
return { valid: ValidateNotSelfIntersecting(feature) };
return ValidateNotSelfIntersecting(feature);
}
return { valid: true };
},
Expand Down Expand Up @@ -828,7 +828,7 @@ describe("TerraDrawLineStringMode", () => {
},
}),
).toEqual({
reason: "Feature is not a valid LineString feature",
reason: "Feature has less than 2 coordinates",
valid: false,
});
});
Expand Down
11 changes: 2 additions & 9 deletions src/modes/linestring/linestring.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { LineString, Point, Position } from "geojson";
import {
BaseModeOptions,
CustomStyling,
ModeMismatchValidationFailure,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { cartesianDistance } from "../../geometry/measure/pixel-distance";
Expand Down Expand Up @@ -574,14 +573,8 @@ export class TerraDrawLineStringMode extends TerraDrawBaseDrawMode<LineStringSty
}

validateFeature(feature: unknown): StoreValidation {
return this.validateModeFeature(
feature,
(baseValidatedFeature) =>
ValidateLineStringFeature(
baseValidatedFeature,
this.coordinatePrecision,
),
"Feature is not a valid LineString feature",
return this.validateModeFeature(feature, (baseValidatedFeature) =>
ValidateLineStringFeature(baseValidatedFeature, this.coordinatePrecision),
);
}
}
12 changes: 4 additions & 8 deletions src/modes/point/point.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { getDefaultStyling } from "../../util/styling";
import {
BaseModeOptions,
CustomStyling,
ModeMismatchValidationFailure,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { ValidatePointFeature } from "../../validations/point.validation";
Expand Down Expand Up @@ -78,7 +77,7 @@ export class TerraDrawPointMode extends TerraDrawBaseDrawMode<PointModeStyling>
const properties = { mode: this.mode };

if (this.validate) {
const valid = this.validate(
const validationResult = this.validate(
{
type: "Feature",
geometry,
Expand All @@ -92,7 +91,7 @@ export class TerraDrawPointMode extends TerraDrawBaseDrawMode<PointModeStyling>
},
);

if (!valid.valid) {
if (!validationResult.valid) {
return;
}
}
Expand Down Expand Up @@ -164,11 +163,8 @@ export class TerraDrawPointMode extends TerraDrawBaseDrawMode<PointModeStyling>
}

validateFeature(feature: unknown): StoreValidation {
return this.validateModeFeature(
feature,
(baseValidatedFeature) =>
ValidatePointFeature(baseValidatedFeature, this.coordinatePrecision),
"Feature is not a valid Point feature",
return this.validateModeFeature(feature, (baseValidatedFeature) =>
ValidatePointFeature(baseValidatedFeature, this.coordinatePrecision),
);
}
}
12 changes: 2 additions & 10 deletions src/modes/polygon/polygon.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,7 @@ describe("TerraDrawPolygonMode", () => {

const validation: Validation = (feature, { updateType }) => {
if (updateType === "finish" || updateType === "commit") {
const validation = ValidateNotSelfIntersecting(feature);
return {
valid: validation,
reason: validation ? undefined : "Self intersecting",
};
return ValidateNotSelfIntersecting(feature);
}
return { valid: true };
};
Expand Down Expand Up @@ -544,11 +540,7 @@ describe("TerraDrawPolygonMode", () => {
polygonMode = new TerraDrawPolygonMode({
validation: (feature, { updateType }) => {
if (updateType === "finish" || updateType === "commit") {
const validation = ValidateNotSelfIntersecting(feature);
return {
valid: validation,
reason: validation ? undefined : "Self intersecting",
};
return ValidateNotSelfIntersecting(feature);
}
return { valid: true };
},
Expand Down
Loading

0 comments on commit 775315f

Please sign in to comment.