Skip to content

Commit

Permalink
Warning if no version in league file
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmatter committed Nov 23, 2023
1 parent c29244e commit a8a8129
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/worker/api/leagueFileUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { helpers, toUI } from "../util";
import { highWaterMark } from "../core/league/createStream";
import type { Conditions } from "../../common/types";
import { toPolyfillReadable, toPolyfillTransform } from "bbgm-polyfills"; // eslint-disable-line
import { DEFAULT_TEAM_COLORS } from "../../common";
import { DEFAULT_TEAM_COLORS, LEAGUE_DATABASE_VERSION } from "../../common";

// These objects (at the root of a league file) should be emitted as a complete object, rather than individual rows from an array
export const CUMULATIVE_OBJECTS = new Set([
Expand Down Expand Up @@ -87,7 +87,13 @@ const makeValidators = () => {
verbose: true,
});

const validators: Record<string, ValidateFunction> = {};
const validators: Record<
string,
{
required: boolean;
validate: ValidateFunction;
}
> = {};

const baseSchema = {
$schema: schema.$schema,
Expand Down Expand Up @@ -115,7 +121,10 @@ const makeValidators = () => {
$id: `${baseSchema.$id}#${part}`,
...subset,
};
validators[part] = ajv.compile(partSchema);
validators[part] = {
required: schema.required.includes(part),

Check failure on line 125 in src/worker/api/leagueFileUpload.ts

View workflow job for this annotation

GitHub Actions / build

Property 'required' does not exist on type '{ $schema: string; $id: string; title: string; description: string; definitions: {}; type: string; properties: {}; }'.
validate: ajv.compile(partSchema),
};
}

return validators;
Expand Down Expand Up @@ -166,6 +175,13 @@ const getBasicInfo = async ({

const reader = await stream.pipeThrough(parseJSON()).getReader();

const requiredPartsNotYetSeen = new Set();
for (const [key, { required }] of Object.entries(validators)) {
if (required) {
requiredPartsNotYetSeen.add(key);
}
}

while (true) {
const { value, done } = (await reader.read()) as any;
if (done) {
Expand All @@ -191,11 +207,15 @@ const getBasicInfo = async ({
}

if (validators[value.key]) {
const validate = validators[value.key];
const { validate, required } = validators[value.key];
validate(value.value);
if (validate.errors) {
schemaErrors.push(...validate.errors);
}

if (required) {
requiredPartsNotYetSeen.delete(value.key);
}
}

if (value.key === "meta" && value.value.name) {
Expand Down Expand Up @@ -245,6 +265,14 @@ const getBasicInfo = async ({
}
}

for (const key of requiredPartsNotYetSeen) {
let message = `"${key}" is required in the root of a JSON file, but is missing.`;
if (key === "version") {
message += ` The latest version is ${LEAGUE_DATABASE_VERSION}, that's probably what you want if this is a new file you're making.`;
}
schemaErrors.push(message);
}

toUI(
"updateLocal",
[
Expand Down
2 changes: 2 additions & 0 deletions tools/lib/generateJSONSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ const generateJSONSchema = (sport /*: string*/) => {

type: "object",

required: ["version"],

properties: {
version: {
type: "integer",
Expand Down

0 comments on commit a8a8129

Please sign in to comment.