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
+2 -2
View File
@@ -6,7 +6,7 @@ import { assert, assertEquals, assertRejects, assertStringIncludes, assertThrows
import { zipSync } from "fflate";
import { ATLAS_BORDER, atlas_layout, extrude_borders } from "$/client/atlas.ts";
import { MAX_TEXTURE_SIZE } from "$/common/constants.ts";
import { read_bmod, write_bmod } from "$/common/bmod.ts";
import { type Bmod, read_bmod, write_bmod } from "$/common/bmod.ts";
import { load_and_check } from "$/tools/check_mods.ts";
import { pack_mod } from "$/tools/pack_mod.ts";
import { create_mod } from "$/tools/new_mod.ts";
@@ -417,7 +417,7 @@ Deno.test({
// write_bmod without anything optional still makes a file read_bmod takes
Deno.test("a mod with only data packs and reads", () => {
const bmod = {
const bmod: Bmod = {
manifest: { format_version: 1, id: "tiny", name: "Tiny", version: "1.0.0" },
data: {
blocks: [],
+6 -6
View File
@@ -61,16 +61,16 @@ Deno.test("a liquid's still texture is its top and bottom, the flowing one its s
Deno.test("check-mods catches liquids that can't work", () => {
assertEquals(validate_block(OIL), []);
const problems = (json: Record<string, unknown>) => validate_block({ ...OIL, ...json });
assertEquals(problems({ liquid: { range: 8 } }), ["liquid.range must be a whole number from 1 to 7"]);
assertEquals(problems({ liquid: { range: 8 } }), ["liquid.range must be at most 7"]);
assertEquals(problems({ liquid: true }), ["liquid must be { range, ticks, renewable }, or {}"]);
assertEquals(problems({ liquid: { ticks: 0 } }), ["liquid.ticks must be a whole number of 1 or more"]);
assertEquals(problems({ liquid: { speed: 2 } }), ["liquid.speed isn't a liquid property"]);
assertEquals(problems({ liquid: { ticks: 0 } }), ["liquid.ticks must be 1 or more"]);
assertEquals(problems({ liquid: { speed: 2 } }), ['liquid can\'t have "speed"']);
assertEquals(problems({ textures: { top: "a:b" } }), [
"a liquid's textures must be a texture id or { still, flowing }",
"textures must be a texture id or { still, flowing } for a liquid",
]);
assertEquals(problems({ model: "engine:cross" }), ["a liquid can't have model, the engine draws it"]);
assertEquals(problems({ model: "engine:cross" }), ["model isn't for liquids, the engine draws them"]);
assertEquals(problems({ states: [{ name: "level", bits: 2, default: 0 }] }), [
'a liquid gets a "level" state of its own',
`states can't have "level", a liquid gets that state of its own`,
]);
});
+12 -11
View File
@@ -26,23 +26,24 @@ Deno.test("a bedrock geo.json becomes an entity model with only what the engine
// per face uvs and parents that come later aren't supported
const per_face = { origin: [0, 0, 0], size: [1, 1, 1], uv: { north: { uv: [0, 0], uv_size: [1, 1] } } };
const bad = entity_model_from_geo({
"minecraft:geometry": [{
description: { texture_width: 16, texture_height: 16 },
bones: [{ name: "leg", parent: "body", cubes: [per_face] }, { name: "body" }],
}],
}, "a:b");
assertEquals(bad.model, undefined);
assert(bad.problems.some((p) => p.includes("parent body must be a bone before it")), String(bad.problems));
assert(bad.problems.some((p) => p.includes("per face uvs aren't supported")), String(bad.problems));
const geo_problems = (bones: unknown[]) => {
const geometry = { description: { texture_width: 16, texture_height: 16 }, bones };
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
assertEquals(model, undefined);
return problems;
};
const late_parent = geo_problems([{ name: "leg", parent: "body" }, { name: "body" }]);
assert(late_parent.some((p) => p.includes("parent body must be a bone before it")), String(late_parent));
const per_face_problems = geo_problems([{ name: "leg", cubes: [per_face] }]);
assert(per_face_problems.some((p) => p.includes("per face uvs aren't supported")), String(per_face_problems));
assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined);
});
Deno.test("an entity needs a texture and a size", () => {
assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0.5, height: 1 }), []);
assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0, height: 1, max_health: -1 }), [
"width must be a number of blocks above 0, at most 16",
"max_health must be a number, 0 or more",
"width must be above 0",
"max_health must be 0 or more",
]);
});
+3 -3
View File
@@ -142,10 +142,10 @@ Deno.test("check-mods finds broken mods", async () => {
has(report.errors, "block broken:x is also defined by broken, broken");
has(report.errors, "items/bad.json: isn't valid json");
has(report.errors, "model broken:no_such_model doesn't exist");
has(report.errors, `"size=1" must be a state of this block`);
has(report.errors, `variants["size=1"] names size, which isn't one of this block's states`);
has(report.errors, "elements[0].from must be [x, y, z]");
has(report.errors, `elements[0].faces: "up" isn't one of`);
has(report.errors, `pattern uses "B" but key doesn't define it`);
has(report.errors, `elements[0].faces can't have "up"`);
has(report.errors, `key needs "B", which pattern uses`);
has(report.errors, "textures/huge.png: is");
has(report.errors, "scripts don't typecheck");
has(report.errors, `depends on "missing_mod", which isn't installed`);
+1 -1
View File
@@ -34,8 +34,8 @@ Deno.test("a model gets its parent's display transform for each place it doesn't
assertEquals(models[1].display, { gui: { scale: [2, 2, 2] }, ground: { scale: [0.25, 0.25, 0.25] } });
assertEquals(validate_model({ id: "a:base", display: models[1].display }), []);
assertEquals(validate_model({ id: "a:base", display: { head: {}, gui: { scale: 2 } } }), [
'display: "head" isn\'t one of gui, firstperson_righthand, ground, fixed',
"display.gui.scale must be [x, y, z]",
'display can\'t have "head"',
]);
});
+4 -4
View File
@@ -6,7 +6,7 @@ import { assert, assertEquals, assertThrows } from "@std/assert";
import { Container, ItemStack } from "$/common/inventory.ts";
import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import type { TagJson } from "$/common/mod_data.ts";
import type { ManifestJson, TagJson } from "$/common/mod_data.ts";
import { resolve_tags, tag_api, type TagSource } from "$/common/tags.ts";
import { can_stand_on } from "$/common/utils.ts";
import { read_bmod, write_bmod } from "$/common/bmod.ts";
@@ -192,10 +192,10 @@ Deno.test("check-mods finds broken tags and tags nobody defines", async () => {
const has = (list: string[], text: string) =>
assert(list.some((e) => e.includes(text)), `expected "${text}" in\n${list.join("\n")}`);
has(report.errors, "tags/items/maple/broken.json: #maple:broken needs maple:no_such_item, which doesn't exist");
has(report.errors, `tags/items/maple/bad.json: values must be an id like "my_mod:thing"`);
has(report.errors, `tags/items/maple/bad.json: values[0] must be an id like "my_mod:thing"`);
has(report.errors, "tags/items/maple/bad.json: replace must be true or false");
has(report.errors, "tags/items/misplaced.json: tag files go in tags/<registry>/<namespace>/");
has(report.errors, "tags/fluids/maple/sap.json: registry must be one of blocks, items, entities");
has(report.errors, `tags/fluids/maple/sap.json: registry must be "blocks", "items", "entities"`);
has(report.errors, "recipes/uses_missing_tag.json: item tag #maple:nothing_here doesn't exist");
has(report.warnings, `uses #farmersdelight:knives but doesn't list "farmersdelight" in dependencies`);
// the shared namespace and optional entries need no dependency
@@ -217,7 +217,7 @@ Deno.test("tags survive packing into a .bmod", () => {
effects: [],
tags: [tag],
};
const manifest = { format_version: 1, id: "x", name: "X", version: "1.0.0" };
const manifest: ManifestJson = { format_version: 1, id: "x", name: "X", version: "1.0.0" };
const bmod = read_bmod(write_bmod({ manifest, data, scripts: {}, textures: new Map() }), "x.bmod");
assertEquals(bmod.data.tags, [tag]);
});