-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gh-112: Turn Game, GameData to interfaces
- Loading branch information
1 parent
d8bb46a
commit f657e38
Showing
4 changed files
with
28 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
import { GameData } from '../types/Game' | ||
import { GameData, isGameData } from '../types/Game' | ||
import { InputError } from '../errors/InputError' | ||
import * as storage from '../storage/Storage' | ||
|
||
const isValidGame = (gameData: GameData): boolean => { | ||
return gameData.name && | ||
gameData.description && | ||
const isValidGameData = (gameData: GameData): boolean => { | ||
return gameData.name !== '' && | ||
gameData.description !== '' && | ||
gameData.name.trim() == gameData.name && | ||
gameData.name.toLowerCase() == gameData.name && | ||
!gameData.name.includes(' ') && | ||
gameData.description.trim() == gameData.description | ||
} | ||
|
||
export const addGame = async (gameData: GameData): Promise<void> => { | ||
if (!isValidGame(gameData)) { | ||
export const addGame = async (data: unknown): Promise<void> => { | ||
if (!isGameData(data)) { | ||
throw new InputError('Data is not valid!') | ||
} | ||
if (!isValidGameData(data)) { | ||
throw new InputError('Game data is not valid!') | ||
} | ||
storage.insertGame(gameData) | ||
storage.insertGame(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
export class Game { | ||
constructor( | ||
readonly id: number, | ||
readonly name: string, | ||
readonly description: string | ||
) {} | ||
export interface GameData { | ||
readonly name: string; | ||
readonly description: string; | ||
} | ||
|
||
export class GameData { | ||
constructor( | ||
readonly name: string, | ||
readonly description: string | ||
) {} | ||
export const isGameData = (data: unknown): data is GameData => { | ||
const test = data as GameData | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
return test.name !== undefined && test.name !== null && | ||
test.description !== null && test.description !== null | ||
} | ||
|
||
export interface Game extends GameData { | ||
readonly id: number; | ||
} |
this is inherently unsafe, since it gives you the illusion you can work with test as if it were GameData.
example: if game data is shaped like this
{a: {b:{c: "somestring"}}
and you test it via typeofa.b.c == "string"
then you might end up with a run time error (calling c of undefined)