mirror of
https://github.com/Vikeo/LifeTrinket.git
synced 2025-11-15 15:27:59 +00:00
add zod validation
This commit is contained in:
@@ -22,7 +22,8 @@
|
|||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"react-dom": "^18.2.0",
|
"react-dom": "^18.2.0",
|
||||||
"react-screen-wake-lock": "^3.0.2",
|
"react-screen-wake-lock": "^3.0.2",
|
||||||
"styled-components": "^6.0.7"
|
"styled-components": "^6.0.7",
|
||||||
|
"zod": "^3.22.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@emotion/react": "^11.11.1",
|
"@emotion/react": "^11.11.1",
|
||||||
|
|||||||
@@ -5,7 +5,13 @@ import {
|
|||||||
GlobalSettingsContextType,
|
GlobalSettingsContextType,
|
||||||
} from '../Contexts/GlobalSettingsContext';
|
} from '../Contexts/GlobalSettingsContext';
|
||||||
import { useAnalytics } from '../Hooks/useAnalytics';
|
import { useAnalytics } from '../Hooks/useAnalytics';
|
||||||
import { InitialGameSettings, Orientation, Settings } from '../Types/Settings';
|
import {
|
||||||
|
GameFormat,
|
||||||
|
InitialGameSettings,
|
||||||
|
InitialGameSettingsSchema,
|
||||||
|
Orientation,
|
||||||
|
Settings,
|
||||||
|
} from '../Types/Settings';
|
||||||
|
|
||||||
export const GlobalSettingsProvider = ({
|
export const GlobalSettingsProvider = ({
|
||||||
children,
|
children,
|
||||||
@@ -33,7 +39,7 @@ export const GlobalSettingsProvider = ({
|
|||||||
startingLifeTotal: 40,
|
startingLifeTotal: 40,
|
||||||
useCommanderDamage: true,
|
useCommanderDamage: true,
|
||||||
orientation: Orientation.Landscape,
|
orientation: Orientation.Landscape,
|
||||||
gameFormat: 'commander',
|
gameFormat: GameFormat.Commander,
|
||||||
};
|
};
|
||||||
setInitialSettings({ ...defaultSettings, ...initialGameSettings });
|
setInitialSettings({ ...defaultSettings, ...initialGameSettings });
|
||||||
};
|
};
|
||||||
@@ -45,9 +51,18 @@ export const GlobalSettingsProvider = ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
//parse existing game settings with zod schema
|
||||||
|
const parsedInitialGameSettings =
|
||||||
|
InitialGameSettingsSchema.safeParse(initialGameSettings);
|
||||||
|
|
||||||
|
if (!parsedInitialGameSettings.success) {
|
||||||
|
localStorage.setItem('initialGameSettings', '');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
'initialGameSettings',
|
'initialGameSettings',
|
||||||
JSON.stringify(initialGameSettings)
|
JSON.stringify(parsedInitialGameSettings.data)
|
||||||
);
|
);
|
||||||
}, [initialGameSettings]);
|
}, [initialGameSettings]);
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
export enum Orientation {
|
export enum Orientation {
|
||||||
OppositeLandscape = 'opposite-landscape',
|
OppositeLandscape = 'opposite-landscape',
|
||||||
Landscape = 'landscape',
|
Landscape = 'landscape',
|
||||||
Portrait = 'portrait',
|
Portrait = 'portrait',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum GameFormat {
|
||||||
|
Commander = 'commander',
|
||||||
|
Standard = 'standard',
|
||||||
|
TwoHeadedGiant = 'two-headed-giant',
|
||||||
|
}
|
||||||
|
|
||||||
export type Settings = {
|
export type Settings = {
|
||||||
keepAwake: boolean;
|
keepAwake: boolean;
|
||||||
showStartingPlayer: boolean;
|
showStartingPlayer: boolean;
|
||||||
@@ -18,4 +26,10 @@ export type InitialGameSettings = {
|
|||||||
orientation: Orientation;
|
orientation: Orientation;
|
||||||
};
|
};
|
||||||
|
|
||||||
type GameFormat = 'commander' | 'standard' | 'two-headed-giant';
|
export const InitialGameSettingsSchema = z.object({
|
||||||
|
startingLifeTotal: z.number().min(1).max(200).default(20),
|
||||||
|
useCommanderDamage: z.boolean().default(false),
|
||||||
|
gameFormat: z.nativeEnum(GameFormat).optional(),
|
||||||
|
numberOfPlayers: z.number().min(1).max(6).default(2),
|
||||||
|
orientation: z.nativeEnum(Orientation).default(Orientation.Landscape),
|
||||||
|
});
|
||||||
|
|||||||
@@ -6988,3 +6988,8 @@ zip-stream@^4.1.0:
|
|||||||
archiver-utils "^3.0.4"
|
archiver-utils "^3.0.4"
|
||||||
compress-commons "^4.1.2"
|
compress-commons "^4.1.2"
|
||||||
readable-stream "^3.6.0"
|
readable-stream "^3.6.0"
|
||||||
|
|
||||||
|
zod@^3.22.4:
|
||||||
|
version "3.22.4"
|
||||||
|
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"
|
||||||
|
integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==
|
||||||
|
|||||||
Reference in New Issue
Block a user