Skip to content

Commit 37e60af

Browse files
committed
added some more checks in Upload feature related to state ids, row ids, and transition values
1 parent 60b2d19 commit 37e60af

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/features/Upload.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Button, MenuItem } from "@mui/material";
33
import FileUploadRoundedIcon from "@mui/icons-material/FileUploadRounded";
44
import { AutomataData } from "../components/types/AutomataData";
55
import { DataContext } from "../components/Editor";
6-
import { TransitionModel } from "../models";
6+
import { RowModel, TransitionModel } from "../models";
77
import { StyledTransitionLabel } from "./components/playground/StyledTransitionLabel";
88
import { UploadProps } from "./props/UploadProps";
99
import { StateMaxSize, StateMinSize } from "../consts/StateSizes";
@@ -66,6 +66,38 @@ export const Upload = (props: UploadProps) => {
6666
return;
6767
}
6868

69+
const stateIds = data?.states?.map((state) => state?.id);
70+
if (new Set(stateIds)?.size !== stateIds?.length) {
71+
props.setAlertMessage("State ids are not unique. Data is corrupted.");
72+
return;
73+
}
74+
75+
const rowIds = data?.rows?.map((row) => row?.id);
76+
if (new Set(rowIds)?.size !== rowIds?.length) {
77+
props.setAlertMessage("Row ids are not unique. Data is corrupted.");
78+
return;
79+
}
80+
81+
const transitionValues = data?.transitions?.map(
82+
(transition) => transition?.value
83+
);
84+
if (
85+
!transitionValues?.every(
86+
(value) =>
87+
// there should be no empty values
88+
value !== "" &&
89+
// there should be no values that are not in the PossibleTransitionValues
90+
value
91+
?.split("")
92+
?.every((val) => PossibleTransitionValues?.includes(val))
93+
)
94+
) {
95+
props.setAlertMessage(
96+
"Transition values are not valid. Data is corrupted."
97+
);
98+
return;
99+
}
100+
69101
data?.rows?.forEach((row) => {
70102
PossibleTransitionValues?.concat("state")?.forEach((value) => {
71103
row[value === "^" ? "nul" : value] = row[
@@ -78,6 +110,8 @@ export const Upload = (props: UploadProps) => {
78110

79111
data?.states?.forEach((state) => {
80112
state.id = state?.id?.toString()?.trim();
113+
state.x = Math.abs(state?.x);
114+
state.y = Math.abs(state?.y);
81115
});
82116

83117
data?.transitions?.forEach((transition) => {

0 commit comments

Comments
 (0)