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
+36 -56
View File
@@ -3,23 +3,27 @@
// animated textures, like minecraft's .png.mcmeta and in the same format: textures/<name>.json next to
// textures/<name>.png says how to cut the png into frames and how long each one shows. see docs/modding/resources/textures.md
import { is_object } from "./mod_data.ts";
import * as z from "zod";
import { find_problems, type Problems } from "./schema.ts";
export interface TextureJson {
animation?: AnimationJson;
}
export interface AnimationJson {
const animation_schema = z.object({
// ticks each frame shows unless the frame says otherwise, 1 if not given
frametime?: number;
frametime: z.int().min(1).optional(),
// blend each frame into the next a tick at a time instead of switching
interpolate?: boolean;
interpolate: z.boolean().optional(),
// the order to show frames in, by index or { index, time }. every frame in order if not given
frames?: (number | { index: number; time: number })[];
frames: z.array(z.union([z.int().min(0), z.object({ index: z.int().min(0), time: z.int().min(1) })])).min(1)
.optional(),
// a frame's size in pixels. frames are square, as wide as the png is or as tall if that's less, if not given
width?: number;
height?: number;
}
width: z.int().min(1).optional(),
height: z.int().min(1).optional(),
});
export type AnimationJson = z.infer<typeof animation_schema>;
const texture_schema = z.object({ animation: animation_schema.optional() }, 'must be { "animation": { ... } }');
export type TextureJson = z.infer<typeof texture_schema>;
// an animation ready to play
export interface Animation {
@@ -29,52 +33,28 @@ export interface Animation {
frames: { x: number; y: number; time: number }[];
}
// what's wrong with a texture's .json, for the png it goes with
export function validate_texture_json(json: unknown, size: { width: number; height: number }): string[] {
if (!is_object(json)) return [`must be { "animation": { ... } }`];
if (json.animation === undefined) return [];
const animation = json.animation;
if (!is_object(animation)) return ["animation must be an object"];
const problems: string[] = [];
const ticks = (value: unknown) => Number.isInteger(value) && (value as number) >= 1;
if (animation.frametime !== undefined && !ticks(animation.frametime)) {
problems.push("animation.frametime must be a whole number of ticks, at least 1");
}
if (animation.interpolate !== undefined && typeof animation.interpolate !== "boolean") {
problems.push("animation.interpolate must be true or false");
}
for (const key of ["width", "height"] as const) {
if (animation[key] !== undefined && !ticks(animation[key])) {
problems.push(`animation.${key} must be a whole number of pixels, at least 1`);
}
}
if (problems.length > 0) return problems;
const { frame_width, frame_height } = frame_size(animation as AnimationJson, size);
if (size.width % frame_width !== 0 || size.height % frame_height !== 0) {
return [
`the png is ${size.width}×${size.height}, which doesn't split into ${frame_width}×${frame_height} frames`,
];
}
const count = (size.width / frame_width) * (size.height / frame_height);
if (animation.frames !== undefined) {
if (!Array.isArray(animation.frames) || animation.frames.length === 0) {
problems.push("animation.frames must be a list of frames");
} else {
animation.frames.forEach((frame, i) => {
const index = is_object(frame) ? frame.index : frame;
if (!Number.isInteger(index) || index < 0 || index >= count) {
problems.push(`animation.frames[${i}]: the png has frames 0 to ${count - 1}`);
}
if (is_object(frame) && !ticks(frame.time)) {
problems.push(`animation.frames[${i}]: time must be a whole number of ticks, at least 1`);
}
// what's wrong with a texture's .json, for the png it goes with: its frames have to be in it
export function validate_texture_json(json: unknown, size: { width: number; height: number }): Problems {
const fits_png = texture_schema.superRefine(({ animation }, ctx) => {
if (!animation) return;
const { frame_width, frame_height } = frame_size(animation, size);
if (size.width % frame_width !== 0 || size.height % frame_height !== 0) {
return ctx.addIssue({
code: "custom",
message:
`the png is ${size.width}×${size.height}, which doesn't split into ${frame_width}×${frame_height} frames`,
});
}
}
return problems;
const count = (size.width / frame_width) * (size.height / frame_height);
animation.frames?.forEach((frame, i) => {
const index = typeof frame === "number" ? frame : frame.index;
if (index >= count) {
const path = ["animation", "frames", i, ...(typeof frame === "number" ? [] : ["index"])];
ctx.addIssue({ code: "custom", path, message: `isn't one of the png's frames, 0 to ${count - 1}` });
}
});
});
return find_problems(fits_png, json);
}
// the animation in a texture's .json, which validate_texture_json passed. undefined if it isn't animated