Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CODEBASE: Validate AllGangs and StockMarket after loading with JSON.parse #1783

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 121 additions & 148 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"acorn": "^8.11.3",
"acorn-jsx-walk": "^2.0.0",
"acorn-walk": "^8.3.2",
"ajv": "^8.17.1",
"arg": "^5.0.2",
"bcryptjs": "^2.4.3",
"better-react-mathjax": "^2.0.3",
Expand Down
25 changes: 23 additions & 2 deletions src/Gang/AllGangs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { FactionName } from "@enums";
import { Reviver } from "../utils/JSONReviver";
import { JsonSchemaValidator } from "../JsonSchema/JsonSchemaValidator";
import { dialogBoxCreate } from "../ui/React/DialogBox";

interface GangTerritory {
power: number;
territory: number;
}

function getDefaultAllGangs() {
export function getDefaultAllGangs() {
return {
[FactionName.SlumSnakes]: {
power: 1,
Expand Down Expand Up @@ -46,7 +48,26 @@ export function resetGangs(): void {
}

export function loadAllGangs(saveString: string): void {
AllGangs = JSON.parse(saveString, Reviver);
let allGangsData: unknown;
let validate;
try {
allGangsData = JSON.parse(saveString, Reviver);
validate = JsonSchemaValidator.AllGangs;
if (!validate(allGangsData)) {
console.error("validate.errors:", validate.errors);
// validate.errors is an array of objects, so we need to use JSON.stringify.
throw new Error(JSON.stringify(validate.errors));
}
} catch (error) {
console.error(error);
console.error("Invalid AllGangsSave:", saveString);
resetGangs();
setTimeout(() => {
dialogBoxCreate(`Cannot load data of AllGangs. AllGangs is reset. Error: ${error}.`);
}, 1000);
return;
}
AllGangs = allGangsData;
}

export function getClashWinChance(thisGang: string, otherGang: string): number {
Expand Down
34 changes: 34 additions & 0 deletions src/JsonSchema/Data/AllGangsSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { JSONSchemaType } from "ajv";
import type { AllGangs } from "../../Gang/AllGangs";
import { GangConstants } from "../../Gang/data/Constants";

/**
* If we add/remove gangs, we must change 3 places:
* - src\Gang\AllGangs.ts: getDefaultAllGangs
* - src\Gang\data\Constants.ts: GangConstants.Names
* - src\Gang\data\power.ts: PowerMultiplier
*
catloversg marked this conversation as resolved.
Show resolved Hide resolved
* Gang code assumes that save data contains exactly gangs defined in these places.
*/
export const AllGangsSchema: JSONSchemaType<typeof AllGangs> = {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
patternProperties: {
".*": {
type: "object",
properties: {
power: {
type: "number",
},
territory: {
type: "number",
},
},
required: ["power", "territory"],
},
},
propertyNames: {
enum: GangConstants.Names,
},
required: GangConstants.Names,
};
134 changes: 134 additions & 0 deletions src/JsonSchema/Data/StockMarketSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { OrderType, PositionType } from "@enums";
import { getKeyList } from "../../utils/helpers/getKeyList";
import { Stock } from "../../StockMarket/Stock";
import { Order } from "../../StockMarket/Order";

const stockObjectProperties = getKeyList(Stock);
const orderObjectProperties = getKeyList(Order);

/**
* It's intentional to not use JSONSchemaType here. The data structure of StockMarket is not suitable for the usage of
* JSONSchemaType. These are 2 biggest problems:
* - IStockMarket is an intersection type. In our case, satisfying TS's type-checking for JSONSchemaType is too hard.
* - Stock and Order are classes, not interfaces or types. In those classes, we have functions, but functions are not
* supported by JSON (without ugly hacks). Let's use the "Order" class as an example. The Order class has the toJSON
* function, so ajv forces us to define the "toJSON" property. If we define it, we have to give it a "type". In
* JavaScript, a function is just an object, so the naive solution is to use {type:"object"}. However, the generated
* validation code uses "typeof" to check the data. "typeof functionName" is "function", not "object", so ajv sees it as
* invalid data. There are some ways to work around this problem, but all of them are complicated. In the end,
* JSONSchemaType is only a utility type. If our schema is designed and tested properly, after the validation, we can
* typecast the data.
*/
export const StockMarketSchema = {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
patternProperties: {
/**
* IStockMarket is an intersection type combining:
* - Record<string, Stock> [1]
* - {lastUpdate: number;Orders: IOrderBook;storedCycles: number;ticksUntilCycle: number;} [2]
*
* StockMarketSchema contains:
* - patternProperties: Defines [1]. The following regex matches all properties that are not in [2]. It defines the
* map of "Full stock name -> Stock".
* - properties: Define [2].
*/
catloversg marked this conversation as resolved.
Show resolved Hide resolved
"^(?!(lastUpdate|Orders|storedCycles|ticksUntilCycle))": {
type: "object",
properties: {
b: {
type: "boolean",
},
cap: {
type: "number",
},
lastPrice: {
type: "number",
},
maxShares: {
type: "number",
},
mv: {
type: "number",
},
name: {
type: "string",
},
otlkMag: {
type: "number",
},
otlkMagForecast: {
type: "number",
},
playerAvgPx: {
type: "number",
},
playerAvgShortPx: {
type: "number",
},
playerShares: {
type: "number",
},
playerShortShares: {
type: "number",
},
price: {
type: "number",
},
shareTxForMovement: {
type: "number",
},
shareTxUntilMovement: {
type: "number",
},
spreadPerc: {
type: "number",
},
symbol: {
type: "string",
},
totalShares: {
type: "number",
},
},
required: [...stockObjectProperties],
},
},
properties: {
lastUpdate: { type: "number" },
Orders: {
type: "object",
patternProperties: {
".*": {
type: "array",
items: {
type: "object",
properties: {
pos: {
type: "string",
enum: [PositionType.Long, PositionType.Short],
},
price: {
type: "number",
},
shares: {
type: "number",
},
stockSymbol: {
type: "string",
},
type: {
type: "string",
enum: [OrderType.LimitBuy, OrderType.LimitSell, OrderType.StopBuy, OrderType.StopSell],
},
},
required: [...orderObjectProperties],
},
},
},
},
storedCycles: { type: "number" },
ticksUntilCycle: { type: "number" },
},
required: ["lastUpdate", "Orders", "storedCycles", "ticksUntilCycle"],
};
10 changes: 10 additions & 0 deletions src/JsonSchema/JsonSchemaValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Ajv from "ajv";
import { AllGangsSchema } from "./Data/AllGangsSchema";
import { StockMarketSchema } from "./Data/StockMarketSchema";

const ajv = new Ajv();

export const JsonSchemaValidator = {
AllGangs: ajv.compile(AllGangsSchema),
StockMarket: ajv.compile(StockMarketSchema),
};
58 changes: 38 additions & 20 deletions src/StockMarket/StockMarket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ import { Reviver } from "../utils/JSONReviver";
import { NetscriptContext } from "../Netscript/APIWrapper";
import { helpers } from "../Netscript/NetscriptHelpers";
import { getRandomIntInclusive } from "../utils/helpers/getRandomIntInclusive";
import { JsonSchemaValidator } from "../JsonSchema/JsonSchemaValidator";
import { Player } from "../Player";

export let StockMarket: IStockMarket = {
lastUpdate: 0,
Orders: {},
storedCycles: 0,
ticksUntilCycle: 0,
} as IStockMarket; // Maps full stock name -> Stock object
export function getDefaultEmptyStockMarket(): IStockMarket {
return {
lastUpdate: 0,
Orders: {},
storedCycles: 0,
ticksUntilCycle: 0,
} as IStockMarket;
}

export let StockMarket = getDefaultEmptyStockMarket(); // Maps full stock name -> Stock object
// Gross type, needs to be addressed
export const SymbolToStockMap: Record<string, Stock> = {}; // Maps symbol -> Stock object

Expand Down Expand Up @@ -130,23 +136,35 @@ export function cancelOrder(params: ICancelOrderParams, ctx?: NetscriptContext):
}

export function loadStockMarket(saveString: string): void {
if (saveString === "") {
StockMarket = {
lastUpdate: 0,
Orders: {},
storedCycles: 0,
ticksUntilCycle: 0,
} as IStockMarket;
} else StockMarket = JSON.parse(saveString, Reviver);
let stockMarketData: unknown;
let validate;
try {
stockMarketData = JSON.parse(saveString, Reviver);
validate = JsonSchemaValidator.StockMarket;
if (!validate(stockMarketData)) {
console.error("validate.errors:", validate.errors);
// validate.errors is an array of objects, so we need to use JSON.stringify.
throw new Error(JSON.stringify(validate.errors));
}
} catch (error) {
console.error(error);
console.error("Invalid StockMarketSave:", saveString);
deleteStockMarket();
if (Player.hasWseAccount) {
initStockMarket();
}
const errorMessage = `Cannot load data of StockMarket. StockMarket is reset.`;
setTimeout(() => {
dialogBoxCreate(errorMessage);
}, 1000);
return;
}
// Typecasting here is fine because we validated the loaded data.
StockMarket = stockMarketData as IStockMarket;
}

export function deleteStockMarket(): void {
StockMarket = {
lastUpdate: 0,
Orders: {},
storedCycles: 0,
ticksUntilCycle: 0,
} as IStockMarket;
StockMarket = getDefaultEmptyStockMarket();
}

export function initStockMarket(): void {
Expand Down
41 changes: 41 additions & 0 deletions test/jest/JsonSchema/AllGangsSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getDefaultAllGangs } from "../../../src/Gang/AllGangs";
import { JsonSchemaValidator } from "../../../src/JsonSchema/JsonSchemaValidator";

describe("Success", () => {
test("Default AllGangs", () => {
const defaultAllGangs = getDefaultAllGangs();
expect(JsonSchemaValidator.AllGangs(defaultAllGangs)).toStrictEqual(true);
});
});

describe("Failure", () => {
test("Do not have all gangs", () => {
const defaultAllGangs = getDefaultAllGangs() as Record<string, unknown>;
delete defaultAllGangs["Slum Snakes"];
expect(JsonSchemaValidator.AllGangs(defaultAllGangs)).toStrictEqual(false);
});
test("Have an unexpected gang", () => {
const defaultAllGangs = getDefaultAllGangs() as Record<string, unknown>;
defaultAllGangs["CyberSec"] = {
power: 1,
territory: 1 / 7,
};
expect(JsonSchemaValidator.AllGangs(defaultAllGangs)).toStrictEqual(false);
});
test("Have invalid power", () => {
const defaultAllGangs = getDefaultAllGangs() as Record<string, unknown>;
defaultAllGangs["Slum Snakes"] = {
power: "1",
territory: 1 / 7,
};
expect(JsonSchemaValidator.AllGangs(defaultAllGangs)).toStrictEqual(false);
});
test("Have invalid territory", () => {
const defaultAllGangs = getDefaultAllGangs() as Record<string, unknown>;
defaultAllGangs["Slum Snakes"] = {
power: 1,
territory: "1 / 7",
};
expect(JsonSchemaValidator.AllGangs(defaultAllGangs)).toStrictEqual(false);
});
});
Loading