Skip to content

Commit

Permalink
feat: Add exhaustive checks to schema switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Aug 23, 2024
1 parent 2e69b26 commit 1694375
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
import { FormikProps } from "formik";
import { get } from "lodash";
import React from "react";
import { exhaustiveCheck } from "utils";

import { ChecklistFieldInput } from "./ChecklistFieldInput";
import { DateFieldInput } from "./DateFieldInput";
Expand Down Expand Up @@ -37,7 +38,10 @@ export const getFieldProps = <T extends Field>(props: Props<T>) => ({
* Controller to return correct user input for field in schema
*/
export const InputFields: React.FC<Props<Field>> = (props) => {
switch (props.type) {
// Local variable required for TS to correctly infer the type
const type = props.type;

switch (type) {
case "text":
return <TextFieldInput {...props} />;
case "number":
Expand All @@ -54,6 +58,6 @@ export const InputFields: React.FC<Props<Field>> = (props) => {
case "map":
return <MapFieldInput {...props} />;
default:
return null;
return exhaustiveCheck(type);
}
};
3 changes: 3 additions & 0 deletions editor.planx.uk/src/@planx/components/shared/Schema/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Feature } from "geojson";
import { exhaustiveCheck } from "utils";
import { array, BaseSchema, object, ObjectSchema, string } from "yup";

import { checklistValidationSchema } from "../../Checklist/model";
Expand Down Expand Up @@ -155,6 +156,8 @@ const generateValidationSchemaForFields = (
case "map":
fieldSchemas[data.fn] = mapValidationSchema(data);
break;
default:
return exhaustiveCheck(type);
}
});

Expand Down

0 comments on commit 1694375

Please sign in to comment.