Created
November 13, 2020 16:24
-
-
Save Arakade/75c57697a4b6cb6fe322d46a2109c78a to your computer and use it in GitHub Desktop.
AJV TypeScript JSONSchemaType requires methods?
This file contains hidden or 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
/** | |
* Enumeration of valid commands. | |
*/ | |
export type Command = 'RegisterGame' | 'GameToController' | 'ControllerToGame'; |
This file contains hidden or 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
import {Server} from "./Server"; | |
import {Command} from "./Command"; | |
export default interface IMessage { | |
readonly command: Command; | |
// run(server: Server): boolean; | |
} |
This file contains hidden or 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
import IMessage from "./IMessage"; | |
import {Server} from "./Server"; | |
export class MessageA implements IMessage { | |
public readonly command = 'RegisterGame'; | |
/** | |
* Code to join the game. | |
* | |
* @minimum 0 | |
* @TJS-type integer | |
*/ | |
public readonly joinCode: number; // TODO: Restore to constructor but keeping Schema specifiers? | |
public constructor(joinCode: number) { | |
this.joinCode = joinCode; | |
} | |
/* | |
public run(server: Server): boolean { | |
return server.doStuff(this); | |
}; | |
*/ | |
} |
This file contains hidden or 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
import {JSONSchemaType} from "ajv"; | |
import {MessageA} from "./MessageA"; | |
import {Server} from "./Server"; | |
export default class MessageASchema { | |
private static readonly SchemaMsgRegisterGame: JSONSchemaType<MessageA> = { | |
$id: 'MsgRegisterGame', | |
additionalProperties: false, | |
properties: { | |
command: { | |
default: 'RegisterGame', | |
enum: ['RegisterGame'], | |
type: 'string', | |
}, | |
joinCode: { | |
description: 'Code to join the game.', | |
minimum: 0, | |
type: 'integer', | |
}, | |
}, | |
required: ['command', 'joinCode'], | |
type: 'object', | |
}; | |
} |
This file contains hidden or 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
{ | |
"name": "ajv-ts-bug-1", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"compile": "tsc", | |
"test": "mocha" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"ajv": "^7.0.0-beta.4" | |
}, | |
"devDependencies": { | |
"typescript": "^4.0.5", | |
"@types/node": "^14.14.7", | |
"ts-node": "^9.0.0", | |
"typescript-json-schema": "^0.43.0" | |
} | |
} |
This file contains hidden or 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
import IMessage from "./IMessage"; | |
export class Server { | |
public doStuff(requestor: IMessage): boolean { | |
console.log(`server did stuff for ${requestor}`); | |
return true; | |
} | |
} |
This file contains hidden or 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
Show hidden characters
{ | |
"compilerOptions": { | |
"module": "commonjs", | |
"target": "es5", | |
"outDir": "./built", | |
"sourceMap": true, | |
"strict": true, | |
"noImplicitAny": true, | |
"alwaysStrict": true, | |
"esModuleInterop": true, | |
"preserveConstEnums": true, | |
"skipLibCheck": true, | |
"forceConsistentCasingInFileNames": true | |
}, | |
"include": ["src-ts/**/*"], | |
"exclude": [ | |
"node_modules" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment