Skip to content

Instantly share code, notes, and snippets.

@Arakade
Created November 13, 2020 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arakade/75c57697a4b6cb6fe322d46a2109c78a to your computer and use it in GitHub Desktop.
Save Arakade/75c57697a4b6cb6fe322d46a2109c78a to your computer and use it in GitHub Desktop.
AJV TypeScript JSONSchemaType requires methods?
/**
* Enumeration of valid commands.
*/
export type Command = 'RegisterGame' | 'GameToController' | 'ControllerToGame';
import {Server} from "./Server";
import {Command} from "./Command";
export default interface IMessage {
readonly command: Command;
// run(server: Server): boolean;
}
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);
};
*/
}
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',
};
}
{
"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"
}
}
import IMessage from "./IMessage";
export class Server {
public doStuff(requestor: IMessage): boolean {
console.log(`server did stuff for ${requestor}`);
return true;
}
}
{
"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