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
+32 -85
View File
@@ -3,8 +3,9 @@
// biomes as data: worldgen/biomes/<name>.json in a mod, see docs/modding/worldgen/biomes.md. which biome a column
// gets is the generator's biome rules (worldgen/biome_source), these say what a biome looks like once it's picked
import * as z from "zod";
import { EverythingRegistry } from "../everything_registry.ts";
import { ID_PATTERN, is_object } from "../mod_data.ts";
import { find_problems, id, type Problems } from "../schema.ts";
import { farming_day } from "../time.ts";
export const SEASONS = ["spring", "summer", "autumn", "winter"] as const;
@@ -12,31 +13,41 @@ export type Season = typeof SEASONS[number];
// how long each season lasts. day 1 to 49 is spring, 50 to 98 summer and so on, a year is 196 days
export const SEASON_DAYS = 49;
// how a biome covers its ground, like minecraft's surface rules. blocks are ids
export interface SurfaceJson {
top: string;
filler: string;
filler_depth: number;
// how a biome covers its ground, like minecraft's surface rules
const surface_schema = z.object({
top: id,
filler: id,
filler_depth: z.int().min(0).max(32),
// the ground's cover under water, filler when not set
underwater_top?: string;
underwater_filler?: string;
underwater_top: id.optional(),
underwater_filler: id.optional(),
// cliffs show bare stone instead of top
bare_cliffs?: boolean;
}
bare_cliffs: z.boolean().optional(),
});
export interface BiomeJson {
id: string;
export type SurfaceJson = z.infer<typeof surface_schema>;
const color = z.string('must be a color like "#79c05a"').regex(/^#[0-9a-fA-F]{6}$/);
const biome_schema = z.object({
id,
// a season it's always in, like winter for snowy biomes. when not set it follows the calendar, see calendar_season
season?: Season;
surface: SurfaceJson;
// "#rrggbb", minecraft's grass, foliage and water colors, which blocks pick with "tint". plains' when not set
colors?: { grass?: string; foliage?: string; water?: string };
season: z.enum(SEASONS).optional(),
surface: surface_schema,
// minecraft's grass, foliage and water colors, which blocks pick with "tint". plains' when not set
colors: z.strictObject({ grass: color.optional(), foliage: color.optional(), water: color.optional() }).optional(),
// how likely each tree cell is to have a tree, and which kinds grow, picked evenly (list one twice to make it
// twice as likely). wood is the logs and leaves a kind grows here when it isn't its own, like big oaks being dark
// oak in the dark forest. the trees feature reads it (bworld's, in its worldgen script), which says what kinds
// and woods there are
trees?: { chance: number; kinds: string[]; wood?: Record<string, string> };
}
// and woods there are, and checks them when it loads
trees: z.object({
chance: z.number().min(0).max(1),
kinds: z.array(z.string()).min(1),
wood: z.record(z.string(), z.string()).optional(),
}).optional(),
});
export type BiomeJson = z.infer<typeof biome_schema>;
// what's registered in "biomes", with the defaults filled in
export interface Biome {
@@ -97,70 +108,6 @@ export function biome_blocks(json: BiomeJson): string[] {
return [top, filler, underwater_top, underwater_filler].filter((id) => id !== undefined);
}
export function validate_biome(json: unknown): string[] {
if (!is_object(json)) return ["biome must be an object"];
const problems: string[] = [];
const id = (value: unknown, field: string) => {
if (!(typeof value === "string" && ID_PATTERN.test(value))) {
problems.push(`${field} must be an id like "my_mod:thing"`);
}
};
id(json.id, "id");
if (json.season !== undefined && !SEASONS.includes(json.season as Season)) {
problems.push(`season must be one of ${SEASONS.join(", ")}`);
}
const surface = json.surface;
if (!is_object(surface)) {
problems.push("surface must be { top, filler, filler_depth }");
} else {
id(surface.top, "surface.top");
id(surface.filler, "surface.filler");
if (surface.underwater_top !== undefined) id(surface.underwater_top, "surface.underwater_top");
if (surface.underwater_filler !== undefined) id(surface.underwater_filler, "surface.underwater_filler");
const depth = surface.filler_depth;
if (!Number.isInteger(depth) || (depth as number) < 0 || (depth as number) > 32) {
problems.push("surface.filler_depth must be a whole number from 0 to 32");
}
if (surface.bare_cliffs !== undefined && typeof surface.bare_cliffs !== "boolean") {
problems.push("surface.bare_cliffs must be true or false");
}
}
if (json.colors !== undefined) {
if (!is_object(json.colors)) {
problems.push("colors must be { grass, foliage, water }");
} else {
for (const [key, value] of Object.entries(json.colors)) {
if (!["grass", "foliage", "water"].includes(key)) problems.push(`colors.${key} isn't a biome color`);
else if (!(typeof value === "string" && /^#[0-9a-fA-F]{6}$/.test(value))) {
problems.push(`colors.${key} must be a color like "#79c05a"`);
}
}
}
}
const trees = json.trees;
if (trees !== undefined) {
if (!is_object(trees)) {
problems.push("trees must be { chance, kinds }");
} else {
if (typeof trees.chance !== "number" || trees.chance < 0 || trees.chance > 1) {
problems.push("trees.chance must be a number from 0 to 1");
}
// which kinds and woods there are is up to the trees feature, bworld's checks them when it loads
if (
!Array.isArray(trees.kinds) || trees.kinds.length === 0 ||
!trees.kinds.every((kind) => typeof kind === "string")
) {
problems.push("trees.kinds must list at least one kind of tree");
}
if (trees.wood !== undefined) {
if (!is_object(trees.wood) || !Object.values(trees.wood).every((wood) => typeof wood === "string")) {
problems.push("trees.wood must be { <kind>: <wood> }");
}
}
}
}
return problems;
export function validate_biome(json: unknown): Problems {
return find_problems(biome_schema, json);
}