Validators using zod

This commit is contained in:
2026-10-25 12:41:46 -03:00
parent 31ae2973d7
commit 4fad43d9cc
24 changed files with 1242 additions and 1788 deletions
+39 -52
View File
@@ -3,11 +3,12 @@
// deno task check-mods
// validates every mod in mods/: manifests, data files, textures, references between them, and typechecks scripts
import * as z from "zod";
import {
data_file,
entity_model_from_geo,
FORMAT_VERSION,
is_object,
OreJson,
type OreJson,
type SpawnJson,
type TagJson,
validate_block,
@@ -21,6 +22,7 @@ import {
validate_spawn,
validate_tag,
} from "$/common/mod_data.ts";
import { json_object } from "$/common/schema.ts";
import { check_references, load_order, type LoadedMod, type ModReport } from "$/common/mod_check.ts";
import { png_size, texture_size_problem } from "$/common/bmod.ts";
import { type TextureJson, validate_texture_json } from "$/common/texture_animation.ts";
@@ -139,6 +141,18 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
error(`manifest.json: credits ${credits} doesn't exist`);
}
// what a data file holds, under key next to its format_version. example is what that looks like, for the error
const read_data = <T extends z.ZodType>(path: string, key: string, contents: T, example: string) => {
const json = read_json(`${dir}/${path}`, (m) => error(`${path}: ${m}`));
if (json === undefined) return undefined;
const file = data_file(key, contents).safeParse(json);
if (!file.success) {
error(`${path}: must be { "format_version": ${FORMAT_VERSION}, "${key}": ${example} }`);
return undefined;
}
return file.data[key] as z.infer<T>;
};
const load_data = <T>(
folder: string,
key: string,
@@ -148,15 +162,11 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
// models/<name>.geo.json are entity models, not block ones
for (const file of walk(`${dir}/${folder}`, ".json").filter((file) => !file.endsWith(".geo.json"))) {
const path = `${folder}/${file}`;
const wrapper = read_json(`${dir}/${path}`, (m) => error(`${path}: ${m}`));
if (wrapper === undefined) continue;
if (!is_object(wrapper) || wrapper.format_version !== FORMAT_VERSION || !(key in wrapper)) {
error(`${path}: must be { "format_version": ${FORMAT_VERSION}, "${key}": { ... } }`);
continue;
}
const problems = validate(wrapper[key]);
const json = read_data(path, key, json_object, "{ ... }");
if (json === undefined) continue;
const problems = validate(json);
for (const problem of problems) error(`${path}: ${problem}`);
if (problems.length === 0) into.push({ file: path, json: wrapper[key] as T });
if (problems.length === 0) into.push({ file: path, json: json as T });
}
};
load_data("blocks", "block", validate_block, mod.blocks);
@@ -187,18 +197,14 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
// tags/<registry>/<namespace>/<path>.json is the tag <namespace>:<path>, in any namespace: tags are shared
for (const file of walk(`${dir}/tags`, ".json")) {
const path = `tags/${file}`;
const wrapper = read_json(`${dir}/${path}`, (m) => error(`${path}: ${m}`));
if (wrapper === undefined) continue;
const [registry, namespace, ...rest] = file.replace(/\.json$/, "").split("/");
if (rest.length === 0) {
error(`${path}: tag files go in tags/<registry>/<namespace>/, like tags/items/${id}/things.json`);
continue;
}
if (!is_object(wrapper) || wrapper.format_version !== FORMAT_VERSION || !is_object(wrapper.tag)) {
error(`${path}: must be { "format_version": ${FORMAT_VERSION}, "tag": { "values": [ ... ] } }`);
continue;
}
const json = { ...wrapper.tag, registry, id: `${namespace}:${rest.join("/")}` };
const tag = read_data(path, "tag", json_object, '{ "values": [ ... ] }');
if (tag === undefined) continue;
const json = { ...tag, registry, id: `${namespace}:${rest.join("/")}` };
const problems = validate_tag(json);
for (const problem of problems) error(`${path}: ${problem}`);
if (problems.length === 0) mod.tags.push({ file: path, json: json as TagJson });
@@ -207,53 +213,34 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
// loot_tables/<path>.json is the loot table <mod id>:<path>, like minecraft's
for (const file of walk(`${dir}/loot_tables`, ".json")) {
const path = `loot_tables/${file}`;
const wrapper = read_json(`${dir}/${path}`, (m) => error(`${path}: ${m}`));
if (wrapper === undefined) continue;
if (!is_object(wrapper) || wrapper.format_version !== FORMAT_VERSION || !is_object(wrapper.loot_table)) {
error(`${path}: must be { "format_version": ${FORMAT_VERSION}, "loot_table": { "pools": [ ... ] } }`);
continue;
}
const json = { ...wrapper.loot_table, id: `${id}:${file.replace(/\.json$/, "")}` };
const loot_table = read_data(path, "loot_table", json_object, '{ "pools": [ ... ] }');
if (loot_table === undefined) continue;
const json = { ...loot_table, id: `${id}:${file.replace(/\.json$/, "")}` };
const problems = validate_loot_table(json);
for (const problem of problems) error(`${path}: ${problem}`);
if (problems.length === 0) mod.loot_tables.push({ file: path, json: json as LootTableJson });
}
if (exists(`${dir}/worldgen/ores.json`)) {
const ores = read_json(`${dir}/worldgen/ores.json`, (m) => error(`worldgen/ores.json: ${m}`));
if (!is_object(ores) || ores.format_version !== FORMAT_VERSION || !Array.isArray(ores.ores)) {
error(`worldgen/ores.json: must be { "format_version": ${FORMAT_VERSION}, "ores": [ ... ] }`);
} else {
ores.ores.forEach((ore, i) => {
const problems = validate_ore(ore);
for (const problem of problems) error(`worldgen/ores.json: ore ${i}: ${problem}`);
if (problems.length === 0) mod.ores.push({ file: "worldgen/ores.json", json: ore as OreJson });
});
}
const ores = read_data("worldgen/ores.json", "ores", z.array(z.unknown()), "[ ... ]") ?? [];
ores.forEach((ore, i) => {
const problems = validate_ore(ore);
for (const problem of problems) error(`worldgen/ores.json: ore ${i}: ${problem}`);
if (problems.length === 0) mod.ores.push({ file: "worldgen/ores.json", json: ore as OreJson });
});
}
if (exists(`${dir}/worldgen/spawns.json`)) {
const spawns = read_json(`${dir}/worldgen/spawns.json`, (m) => error(`worldgen/spawns.json: ${m}`));
if (!is_object(spawns) || spawns.format_version !== FORMAT_VERSION || !Array.isArray(spawns.spawns)) {
error(`worldgen/spawns.json: must be { "format_version": ${FORMAT_VERSION}, "spawns": [ ... ] }`);
} else {
spawns.spawns.forEach((spawn, i) => {
const problems = validate_spawn(spawn);
for (const problem of problems) error(`worldgen/spawns.json: spawn ${i}: ${problem}`);
if (problems.length === 0) {
mod.spawns.push({ file: "worldgen/spawns.json", json: spawn as SpawnJson });
}
});
}
const spawns = read_data("worldgen/spawns.json", "spawns", z.array(z.unknown()), "[ ... ]") ?? [];
spawns.forEach((spawn, i) => {
const problems = validate_spawn(spawn);
for (const problem of problems) error(`worldgen/spawns.json: spawn ${i}: ${problem}`);
if (problems.length === 0) mod.spawns.push({ file: "worldgen/spawns.json", json: spawn as SpawnJson });
});
}
if (exists(`${dir}/config.json`)) {
const config = read_json(`${dir}/config.json`, (m) => error(`config.json: ${m}`));
if (!is_object(config) || config.format_version !== FORMAT_VERSION || !is_object(config.config)) {
error(`config.json: must be { "format_version": ${FORMAT_VERSION}, "config": { ... } }`);
} else {
mod.config = config.config;
}
mod.config = read_data("config.json", "config", json_object, "{ ... }");
}
// a mod only registers ids in its own namespace
+2 -9
View File
@@ -5,15 +5,8 @@
// bottom instead of up and down, ids lose their folders, a parent keeps being a parent (bworld merges them when it
// loads, see resolve_models), and what bworld has no use for (blockbench's names, display transforms for places bworld
// doesn't show items, like third person) is left out
import {
DISPLAY_CONTEXTS,
type ModelDisplay,
type ModelElementJson,
type ModelFace,
type ModelFaceJson,
type ModelFileJson,
} from "$/common/block_models.ts";
import { FORMAT_VERSION } from "$/common/mod_data.ts";
import type { ModelDisplay, ModelElementJson, ModelFaceJson, ModelFileJson } from "$/common/block_models.ts";
import { DISPLAY_CONTEXTS, FORMAT_VERSION, type ModelFace } from "$/common/mod_data.ts";
import { join } from "@std/path";
import {
DEFAULT_REMAPS,
+2 -1
View File
@@ -4,7 +4,8 @@
// where things are in a java mod's resources, and what they're called in bworld. a resource location like
// "create:block/cogwheel" (or "block/stone", which is minecraft's) becomes the id "create:block_cogwheel": bworld ids
// have no folders, so the path's folders are joined with _, the same way check_mods names textures in subfolders
import { ID_PATTERN, type TagRegistry } from "$/common/mod_data.ts";
import type { TagRegistry } from "$/common/mod_data.ts";
import { ID_PATTERN } from "$/common/schema.ts";
import { registered_ids } from "$/common/mod_check.ts";
import { load_and_check } from "$/tools/check_mods.ts";
import { dirname, relative, resolve, SEPARATOR } from "@std/path";
+2 -1
View File
@@ -5,7 +5,8 @@
// format is the same: values, replace and remove, with { id, required: false } for entries that can be missing. tag ids
// keep their folders, since bworld's can have them too
import { join } from "@std/path";
import { FORMAT_VERSION, TAG_ID_PATTERN, type TagRegistry } from "$/common/mod_data.ts";
import { FORMAT_VERSION, type TagRegistry } from "$/common/mod_data.ts";
import { TAG_ID_PATTERN } from "$/common/schema.ts";
import {
DEFAULT_REMAPS,
empty_report,
+2 -1
View File
@@ -12,7 +12,8 @@
// the parts lock their textures to the world (uvlock), so every piece shows the part of the texture under it, the
// texture's line running north to south or its line running east to west, and it lines up from one wire to the next
import { model_shape } from "$/common/block_shapes.ts";
import type { ModelElementJson, ModelFace } from "$/common/block_models.ts";
import type { ModelElementJson } from "$/common/block_models.ts";
import type { ModelFace } from "$/common/mod_data.ts";
import type { Face } from "$/common/mod_api/shared.ts";
import { can_cover, ways_out } from "$/mods/bworld/scripts/verdigris/joins.ts";
import { DIRECTIONS, FACE_OFFSETS, FACES } from "$/mods/bworld/scripts/verdigris/sides.ts";