Validators using zod
This commit is contained in:
+147
-206
@@ -7,7 +7,18 @@
|
||||
import { EverythingRegistry } from "../everything_registry.ts";
|
||||
import type { BlockRegistry } from "../everything_registry.ts";
|
||||
import { CHUNK_HEIGHT, CHUNK_SIZE } from "../constants.ts";
|
||||
import { ID_PATTERN, is_object, TAG_ID_PATTERN } from "../mod_data.ts";
|
||||
import * as z from "zod";
|
||||
import {
|
||||
add_problems,
|
||||
by_kind,
|
||||
find_problems,
|
||||
id_or_tag,
|
||||
ID_PATTERN,
|
||||
is_object,
|
||||
json_object,
|
||||
type Problems,
|
||||
TAG_ID_PATTERN,
|
||||
} from "../schema.ts";
|
||||
import { list_matches } from "../tags.ts";
|
||||
import { can_stand_on } from "../utils.ts";
|
||||
import { AIR_ID } from "../protocol.ts";
|
||||
@@ -66,56 +77,108 @@ export interface FeatureRefs {
|
||||
custom: Set<string>;
|
||||
}
|
||||
|
||||
export function validate_feature(json: unknown): string[] {
|
||||
const problems: string[] = [];
|
||||
feature_refs(json, { blocks: new Set(), biomes: new Set(), custom: new Set() }, problems);
|
||||
return problems;
|
||||
// a built in name, with its namespace or without, see builtin_name
|
||||
const builtin = (...names: string[]) => z.literal(names.flatMap((name) => [name, `minecraft:${name}`]));
|
||||
|
||||
const block_id = z.string("must be a block's id").regex(ID_PATTERN);
|
||||
const biome_id = z.string("must be a biome's id").regex(ID_PATTERN);
|
||||
|
||||
// a block, see state_id
|
||||
const block_state_schema = z.union(
|
||||
[block_id, z.object({ state: z.object({ Name: block_id }) }), z.object({ Name: block_id })],
|
||||
'must be a block\'s id or { "type": "simple_state_provider", "state": { "Name": ... } }',
|
||||
);
|
||||
|
||||
// a whole number, see int_provider
|
||||
const int_provider_schema: z.ZodType = z.lazy(() =>
|
||||
z.union([
|
||||
z.int(),
|
||||
z.discriminatedUnion("type", [
|
||||
z.object({ type: builtin("constant"), value: z.int() }),
|
||||
z.object({ type: builtin("uniform"), min_inclusive: z.int(), max_inclusive: z.int() }).refine(
|
||||
({ min_inclusive, max_inclusive }) => min_inclusive <= max_inclusive,
|
||||
{ path: ["min_inclusive"], message: "can't be above max_inclusive" },
|
||||
),
|
||||
z.object({
|
||||
type: builtin("weighted_list"),
|
||||
distribution: z.array(z.object({ data: int_provider_schema, weight: z.int().min(1) })).min(1),
|
||||
}),
|
||||
], "must be constant, uniform or weighted_list"),
|
||||
], 'must be a whole number or { "type": "uniform", ... }')
|
||||
);
|
||||
|
||||
// a y, see anchor
|
||||
const anchor_schema = z.union([
|
||||
z.strictObject({ absolute: z.int() }),
|
||||
z.strictObject({ above_bottom: z.int() }),
|
||||
z.strictObject({ below_top: z.int() }),
|
||||
], 'must be one of { "absolute": y }, { "above_bottom": n } or { "below_top": n }');
|
||||
|
||||
// see height_provider
|
||||
const height_schema = z.union([
|
||||
anchor_schema,
|
||||
z.discriminatedUnion("type", [
|
||||
z.object({ type: builtin("constant"), value: anchor_schema }),
|
||||
z.object({ type: builtin("uniform"), min_inclusive: anchor_schema, max_inclusive: anchor_schema }),
|
||||
], "must be constant or uniform"),
|
||||
], 'must be a height, like { "absolute": 10 } or { "type": "uniform", ... }');
|
||||
|
||||
// see block_predicate. offset moves where it looks
|
||||
const predicate_base = z.object({
|
||||
offset: z.tuple([z.int(), z.int(), z.int()], "must be [x, y, z], whole numbers").optional(),
|
||||
});
|
||||
|
||||
const block_predicate_schema: z.ZodType = z.lazy(() =>
|
||||
z.discriminatedUnion("type", [
|
||||
predicate_base.extend({
|
||||
type: builtin("matching_blocks"),
|
||||
blocks: z.union([id_or_tag, z.array(id_or_tag)], "must be a block, a #tag, or a list of them"),
|
||||
}),
|
||||
predicate_base.extend({ type: builtin("solid", "replaceable", "true") }),
|
||||
predicate_base.extend({ type: builtin("would_survive"), state: block_state_schema }),
|
||||
predicate_base.extend({ type: builtin("not"), predicate: block_predicate_schema }),
|
||||
predicate_base.extend({ type: builtin("all_of", "any_of"), predicates: z.array(block_predicate_schema) }),
|
||||
], "must be matching_blocks, solid, replaceable, would_survive, not, all_of, any_of or true")
|
||||
);
|
||||
|
||||
// a mod's own modifier is checked by the worldgen script that registers it
|
||||
const placement_modifier = by_kind<PlacementJson>("type", (type) => {
|
||||
const name = builtin_name(type);
|
||||
if (name === undefined) return json_object;
|
||||
return Object.hasOwn(MODIFIERS, name) ? MODIFIERS[name].schema : undefined;
|
||||
}, "isn't a built in placement modifier");
|
||||
|
||||
const feature_schema: z.ZodType<FeatureJson> = z.object({
|
||||
id: z.string('must be an id like "my_mod:thing" or "my_mod:folder/thing"').regex(TAG_ID_PATTERN),
|
||||
type: z.string(),
|
||||
config: json_object.optional(),
|
||||
placement: z.array(placement_modifier).optional(),
|
||||
biomes: z.union([biome_id, z.array(biome_id)], "must be a biome's id or a list of them").optional(),
|
||||
step: z.enum(FEATURE_STEPS).optional(),
|
||||
}).superRefine((feature, ctx) => {
|
||||
// a built in type's config. a mod's own type is checked by the worldgen script that registers it, like modifiers
|
||||
const name = builtin_name(feature.type);
|
||||
if (name === undefined) return;
|
||||
if (!Object.hasOwn(FEATURE_TYPES, name)) {
|
||||
return ctx.addIssue({ code: "custom", path: ["type"], message: `${feature.type} isn't built in` });
|
||||
}
|
||||
add_problems(ctx, FEATURE_TYPES[name].schema, feature.config ?? {}, ["config"]);
|
||||
});
|
||||
|
||||
export function validate_feature(json: unknown): Problems {
|
||||
return find_problems(feature_schema, json);
|
||||
}
|
||||
|
||||
export function feature_refs(json: unknown, refs: FeatureRefs, problems: string[] = []) {
|
||||
if (!is_object(json)) {
|
||||
problems.push("feature must be an object");
|
||||
return;
|
||||
}
|
||||
if (!(typeof json.id === "string" && TAG_ID_PATTERN.test(json.id))) {
|
||||
problems.push(`id must be an id like "my_mod:thing" or "my_mod:folder/thing"`);
|
||||
}
|
||||
if (json.step !== undefined && !FEATURE_STEPS.includes(json.step as FeatureStep)) {
|
||||
problems.push(`step must be one of ${FEATURE_STEPS.join(", ")}`);
|
||||
}
|
||||
if (json.biomes !== undefined) {
|
||||
const biomes = typeof json.biomes === "string" ? [json.biomes] : json.biomes;
|
||||
if (!Array.isArray(biomes) || !biomes.every((b) => typeof b === "string" && ID_PATTERN.test(b))) {
|
||||
problems.push("biomes must be a biome's id or a list of them");
|
||||
} else {
|
||||
for (const biome of biomes) refs.biomes.add(biome);
|
||||
}
|
||||
}
|
||||
if (json.config !== undefined && !is_object(json.config)) problems.push("config must be an object");
|
||||
if (typeof json.type !== "string") {
|
||||
problems.push("type must be a feature type");
|
||||
} else {
|
||||
const name = builtin_name(json.type);
|
||||
if (name === undefined) refs.custom.add(json.type);
|
||||
else if (!(name in FEATURE_TYPES)) problems.push(`type ${json.type} isn't built in`);
|
||||
else FEATURE_TYPES[name].check((json.config ?? {}) as Record<string, unknown>, "config", refs, problems);
|
||||
}
|
||||
if (json.placement !== undefined) {
|
||||
if (!Array.isArray(json.placement)) {
|
||||
problems.push("placement must be a list of placement modifiers");
|
||||
return;
|
||||
}
|
||||
json.placement.forEach((modifier: unknown, i: number) => {
|
||||
const at = `placement[${i}]`;
|
||||
if (!is_object(modifier) || typeof modifier.type !== "string") {
|
||||
problems.push(`${at}: must be { "type": ... }`);
|
||||
return;
|
||||
}
|
||||
const name = builtin_name(modifier.type);
|
||||
if (name === undefined) refs.custom.add(modifier.type);
|
||||
else if (!(name in MODIFIERS)) problems.push(`${at}: ${modifier.type} isn't a built in placement modifier`);
|
||||
else MODIFIERS[name].check(modifier, at, refs, problems);
|
||||
});
|
||||
// what a feature that passed its checks uses
|
||||
export function feature_refs(json: FeatureJson, refs: FeatureRefs) {
|
||||
for (const biome of [json.biomes ?? []].flat()) refs.biomes.add(biome);
|
||||
const type = builtin_name(json.type);
|
||||
if (type === undefined) refs.custom.add(json.type);
|
||||
else FEATURE_TYPES[type].refs?.(json.config ?? {}, refs);
|
||||
for (const modifier of json.placement ?? []) {
|
||||
const name = builtin_name(modifier.type);
|
||||
if (name === undefined) refs.custom.add(modifier.type);
|
||||
else MODIFIERS[name].refs?.(modifier, refs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,15 +246,19 @@ export function compile_feature(
|
||||
// built in feature types
|
||||
|
||||
interface Builtin<T> {
|
||||
check(json: Record<string, unknown>, at: string, refs: FeatureRefs, problems: string[]): void;
|
||||
// its json: a feature type's config, or the placement modifier
|
||||
schema: z.ZodType;
|
||||
// what it uses, when it uses something
|
||||
refs?(json: Record<string, unknown>, refs: FeatureRefs): void;
|
||||
compile(json: Record<string, unknown>): T;
|
||||
}
|
||||
|
||||
const FEATURE_TYPES: Record<string, Builtin<(chunk: FeatureChunk, origin: Position) => boolean>> = {
|
||||
// one block, where it can stand
|
||||
simple_block: {
|
||||
check(json, at, refs, problems) {
|
||||
check_state(json.to_place, `${at}.to_place`, refs, problems);
|
||||
schema: z.object({ to_place: block_state_schema }),
|
||||
refs(json, refs) {
|
||||
refs.blocks.add(state_id(json.to_place));
|
||||
},
|
||||
compile(json) {
|
||||
const id = state_id(json.to_place);
|
||||
@@ -204,29 +271,15 @@ const FEATURE_TYPES: Record<string, Builtin<(chunk: FeatureChunk, origin: Positi
|
||||
},
|
||||
// layers of blocks stacked up (or down) from the origin, cut short where allowed_placement doesn't hold
|
||||
block_column: {
|
||||
check(json, at, refs, problems) {
|
||||
if (!Array.isArray(json.layers) || json.layers.length === 0) {
|
||||
problems.push(`${at}.layers must be a list of { "height", "provider" }`);
|
||||
} else {
|
||||
json.layers.forEach((layer: unknown, i: number) => {
|
||||
const here = `${at}.layers[${i}]`;
|
||||
if (!is_object(layer)) {
|
||||
problems.push(`${here} must be { "height", "provider" }`);
|
||||
return;
|
||||
}
|
||||
check_int(layer.height, `${here}.height`, problems);
|
||||
check_state(layer.provider, `${here}.provider`, refs, problems);
|
||||
});
|
||||
}
|
||||
if (json.direction !== undefined && json.direction !== "up" && json.direction !== "down") {
|
||||
problems.push(`${at}.direction must be "up" or "down"`);
|
||||
}
|
||||
if (json.allowed_placement !== undefined) {
|
||||
check_predicate(json.allowed_placement, `${at}.allowed_placement`, refs, problems);
|
||||
}
|
||||
if (json.prioritize_tip !== undefined && typeof json.prioritize_tip !== "boolean") {
|
||||
problems.push(`${at}.prioritize_tip must be true or false`);
|
||||
}
|
||||
schema: z.object({
|
||||
layers: z.array(z.object({ height: int_provider_schema, provider: block_state_schema })).min(1),
|
||||
direction: z.enum(["up", "down"]).optional(),
|
||||
allowed_placement: block_predicate_schema.optional(),
|
||||
prioritize_tip: z.boolean().optional(),
|
||||
}),
|
||||
refs(json, refs) {
|
||||
for (const layer of json.layers as { provider: unknown }[]) refs.blocks.add(state_id(layer.provider));
|
||||
if (json.allowed_placement !== undefined) predicate_refs(json.allowed_placement, refs);
|
||||
},
|
||||
compile(json) {
|
||||
const layers = (json.layers as { height: unknown; provider: unknown }[]).map((layer) => ({
|
||||
@@ -259,14 +312,12 @@ const FEATURE_TYPES: Record<string, Builtin<(chunk: FeatureChunk, origin: Positi
|
||||
|
||||
type Modifier = (chunk: FeatureChunk, positions: Position[]) => Position[];
|
||||
|
||||
const HEIGHTMAPS = ["WORLD_SURFACE_WG", "WORLD_SURFACE", "MOTION_BLOCKING", "OCEAN_FLOOR_WG", "OCEAN_FLOOR"];
|
||||
const HEIGHTMAPS = ["WORLD_SURFACE_WG", "WORLD_SURFACE", "MOTION_BLOCKING", "OCEAN_FLOOR_WG", "OCEAN_FLOOR"] as const;
|
||||
|
||||
const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
// each position becomes count of them
|
||||
count: {
|
||||
check(json, at, _refs, problems) {
|
||||
check_int(json.count, `${at}.count`, problems);
|
||||
},
|
||||
schema: z.object({ count: int_provider_schema }),
|
||||
compile(json) {
|
||||
const count = int_provider(json.count);
|
||||
return (chunk, positions) =>
|
||||
@@ -275,11 +326,7 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
},
|
||||
// one in chance positions are kept
|
||||
rarity_filter: {
|
||||
check(json, at, _refs, problems) {
|
||||
if (!(Number.isInteger(json.chance) && (json.chance as number) >= 1)) {
|
||||
problems.push(`${at}.chance must be a whole number, 1 or more`);
|
||||
}
|
||||
},
|
||||
schema: z.object({ chance: z.int().min(1) }),
|
||||
compile(json) {
|
||||
const chance = json.chance as number;
|
||||
return (chunk, positions) => positions.filter(() => chunk.rng.next() < 1 / chance);
|
||||
@@ -287,7 +334,7 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
},
|
||||
// somewhere random in the chunk, across
|
||||
in_square: {
|
||||
check() {},
|
||||
schema: z.object({}),
|
||||
compile() {
|
||||
return (chunk, positions) =>
|
||||
positions.map((p) => {
|
||||
@@ -299,10 +346,7 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
},
|
||||
// moved by up to xz_spread across and y_spread up or down
|
||||
random_offset: {
|
||||
check(json, at, _refs, problems) {
|
||||
check_int(json.xz_spread ?? 0, `${at}.xz_spread`, problems);
|
||||
check_int(json.y_spread ?? 0, `${at}.y_spread`, problems);
|
||||
},
|
||||
schema: z.object({ xz_spread: int_provider_schema.optional(), y_spread: int_provider_schema.optional() }),
|
||||
compile(json) {
|
||||
const xz = int_provider(json.xz_spread ?? 0);
|
||||
const y = int_provider(json.y_spread ?? 0);
|
||||
@@ -312,9 +356,7 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
},
|
||||
// at a height
|
||||
height_range: {
|
||||
check(json, at, _refs, problems) {
|
||||
check_height(json.height, `${at}.height`, problems);
|
||||
},
|
||||
schema: z.object({ height: height_schema }),
|
||||
compile(json) {
|
||||
const height = height_provider(json.height);
|
||||
return (chunk, positions) => positions.map((p) => ({ x: p.x, y: height(chunk), z: p.z }));
|
||||
@@ -323,11 +365,7 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
// on top of the column: WORLD_SURFACE on its highest block, OCEAN_FLOOR on its highest that isn't a liquid.
|
||||
// positions outside the chunk are dropped
|
||||
heightmap: {
|
||||
check(json, at, _refs, problems) {
|
||||
if (!HEIGHTMAPS.includes(json.heightmap as string)) {
|
||||
problems.push(`${at}.heightmap must be one of ${HEIGHTMAPS.join(", ")}`);
|
||||
}
|
||||
},
|
||||
schema: z.object({ heightmap: z.enum(HEIGHTMAPS) }),
|
||||
compile(json) {
|
||||
const floor = String(json.heightmap).startsWith("OCEAN_FLOOR");
|
||||
return (chunk, positions) =>
|
||||
@@ -341,8 +379,9 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
},
|
||||
// where a block predicate holds
|
||||
block_predicate_filter: {
|
||||
check(json, at, refs, problems) {
|
||||
check_predicate(json.predicate, `${at}.predicate`, refs, problems);
|
||||
schema: z.object({ predicate: block_predicate_schema }),
|
||||
refs(json, refs) {
|
||||
predicate_refs(json.predicate, refs);
|
||||
},
|
||||
compile(json) {
|
||||
const predicate = block_predicate(json.predicate);
|
||||
@@ -359,44 +398,22 @@ function counts_for_heightmap(id: string | undefined, floor: boolean) {
|
||||
// block predicates, like minecraft's
|
||||
|
||||
type Predicate = (chunk: FeatureChunk, x: number, y: number, z: number) => boolean;
|
||||
const PREDICATES = ["matching_blocks", "solid", "replaceable", "would_survive", "not", "all_of", "any_of", "true"];
|
||||
|
||||
function check_predicate(json: unknown, at: string, refs: FeatureRefs, problems: string[]) {
|
||||
if (!is_object(json) || typeof json.type !== "string") {
|
||||
problems.push(`${at} must be a block predicate, { "type": ... }`);
|
||||
return;
|
||||
}
|
||||
const name = builtin_name(json.type);
|
||||
if (name === undefined || !PREDICATES.includes(name)) {
|
||||
problems.push(`${at}: ${json.type} isn't one of ${PREDICATES.join(", ")}`);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
json.offset !== undefined &&
|
||||
!(Array.isArray(json.offset) && json.offset.length === 3 && json.offset.every(Number.isInteger))
|
||||
) {
|
||||
problems.push(`${at}.offset must be [x, y, z], whole numbers`);
|
||||
}
|
||||
switch (name) {
|
||||
case "matching_blocks": {
|
||||
const blocks = typeof json.blocks === "string" ? [json.blocks] : json.blocks;
|
||||
if (!Array.isArray(blocks) || !blocks.every((b) => typeof b === "string")) {
|
||||
problems.push(`${at}.blocks must be a block, a #tag, or a list of them`);
|
||||
} else {
|
||||
for (const block of blocks) refs.blocks.add(block);
|
||||
}
|
||||
// the blocks a predicate tests for
|
||||
function predicate_refs(json: unknown, refs: FeatureRefs) {
|
||||
const predicate = json as Record<string, unknown>;
|
||||
switch (builtin_name(predicate.type as string)) {
|
||||
case "matching_blocks":
|
||||
for (const block of [predicate.blocks].flat()) refs.blocks.add(block as string);
|
||||
break;
|
||||
}
|
||||
case "would_survive":
|
||||
check_state(json.state, `${at}.state`, refs, problems);
|
||||
refs.blocks.add(state_id(predicate.state));
|
||||
break;
|
||||
case "not":
|
||||
check_predicate(json.predicate, `${at}.predicate`, refs, problems);
|
||||
predicate_refs(predicate.predicate, refs);
|
||||
break;
|
||||
case "all_of":
|
||||
case "any_of":
|
||||
if (!Array.isArray(json.predicates)) problems.push(`${at}.predicates must be a list`);
|
||||
else json.predicates.forEach((p, i) => check_predicate(p, `${at}.predicates[${i}]`, refs, problems));
|
||||
for (const inner of predicate.predicates as unknown[]) predicate_refs(inner, refs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,18 +473,6 @@ function inside(chunk: FeatureChunk, x: number, z: number) {
|
||||
|
||||
// block states: a block's id, or minecraft's { "type": "simple_state_provider", "state": { "Name": id } }
|
||||
|
||||
function check_state(json: unknown, at: string, refs: FeatureRefs, problems: string[]) {
|
||||
const id = typeof json === "string"
|
||||
? json
|
||||
: is_object(json) && is_object(json.state)
|
||||
? json.state.Name
|
||||
: is_object(json)
|
||||
? json.Name
|
||||
: undefined;
|
||||
if (typeof id === "string" && ID_PATTERN.test(id)) refs.blocks.add(id);
|
||||
else problems.push(`${at} must be a block's id or { "type": "simple_state_provider", "state": { "Name": ... } }`);
|
||||
}
|
||||
|
||||
function state_id(json: unknown): string {
|
||||
if (typeof json === "string") return json;
|
||||
const object = json as Record<string, unknown>;
|
||||
@@ -479,41 +484,6 @@ function state_id(json: unknown): string {
|
||||
|
||||
type IntProvider = (chunk: FeatureChunk) => number;
|
||||
|
||||
function check_int(json: unknown, at: string, problems: string[]) {
|
||||
if (Number.isInteger(json)) return;
|
||||
if (!is_object(json)) {
|
||||
problems.push(`${at} must be a whole number or { "type": "uniform", ... }`);
|
||||
return;
|
||||
}
|
||||
switch (builtin_name(String(json.type))) {
|
||||
case "constant":
|
||||
if (!Number.isInteger(json.value)) problems.push(`${at}.value must be a whole number`);
|
||||
break;
|
||||
case "uniform":
|
||||
if (!Number.isInteger(json.min_inclusive) || !Number.isInteger(json.max_inclusive)) {
|
||||
problems.push(`${at} needs whole numbers min_inclusive and max_inclusive`);
|
||||
} else if ((json.min_inclusive as number) > (json.max_inclusive as number)) {
|
||||
problems.push(`${at}: min_inclusive is above max_inclusive`);
|
||||
}
|
||||
break;
|
||||
case "weighted_list":
|
||||
if (!Array.isArray(json.distribution) || json.distribution.length === 0) {
|
||||
problems.push(`${at}.distribution must be a list of { "data", "weight" }`);
|
||||
} else {
|
||||
json.distribution.forEach((entry: unknown, i: number) => {
|
||||
if (!is_object(entry) || !(Number.isInteger(entry.weight) && (entry.weight as number) > 0)) {
|
||||
problems.push(`${at}.distribution[${i}] needs a whole number weight above 0`);
|
||||
} else {
|
||||
check_int(entry.data, `${at}.distribution[${i}].data`, problems);
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
problems.push(`${at}.type must be constant, uniform or weighted_list`);
|
||||
}
|
||||
}
|
||||
|
||||
function int_provider(json: unknown): IntProvider {
|
||||
if (typeof json === "number") return () => json;
|
||||
const object = json as Record<string, unknown>;
|
||||
@@ -548,35 +518,6 @@ function int_provider(json: unknown): IntProvider {
|
||||
// heights: an anchor ({ "absolute" }, { "above_bottom" } or { "below_top" }), { "type": "constant", "value": anchor }
|
||||
// or { "type": "uniform", "min_inclusive": anchor, "max_inclusive": anchor }
|
||||
|
||||
function check_height(json: unknown, at: string, problems: string[]) {
|
||||
if (!is_object(json)) {
|
||||
problems.push(`${at} must be a height, like { "absolute": 10 } or { "type": "uniform", ... }`);
|
||||
return;
|
||||
}
|
||||
if (json.type === undefined) return check_anchor(json, at, problems);
|
||||
switch (builtin_name(String(json.type))) {
|
||||
case "constant":
|
||||
check_anchor(json.value, `${at}.value`, problems);
|
||||
break;
|
||||
case "uniform":
|
||||
check_anchor(json.min_inclusive, `${at}.min_inclusive`, problems);
|
||||
check_anchor(json.max_inclusive, `${at}.max_inclusive`, problems);
|
||||
break;
|
||||
default:
|
||||
problems.push(`${at}.type must be constant or uniform`);
|
||||
}
|
||||
}
|
||||
|
||||
function check_anchor(json: unknown, at: string, problems: string[]) {
|
||||
const keys = ["absolute", "above_bottom", "below_top"];
|
||||
if (
|
||||
!is_object(json) || Object.keys(json).length !== 1 || !keys.includes(Object.keys(json)[0]) ||
|
||||
!Number.isInteger(Object.values(json)[0])
|
||||
) {
|
||||
problems.push(`${at} must be one of { "absolute": y }, { "above_bottom": n } or { "below_top": n }`);
|
||||
}
|
||||
}
|
||||
|
||||
function anchor(json: unknown): number {
|
||||
const object = json as Record<string, number>;
|
||||
if (object.absolute !== undefined) return object.absolute;
|
||||
|
||||
Reference in New Issue
Block a user