Fix list rows like links skipping validation in forms

This commit is contained in:
Kalle
2026-10-20 07:42:36 +03:00
parent 401c85ef29
commit 22a4ffdca5
3 changed files with 37 additions and 4 deletions

View File

@@ -1,7 +1,13 @@
import * as v from "valibot";
import { describe, expect, test } from "vitest";
import { preprocess } from "~/utils/schema";
import { getNestedSchema, getNestedValue, setNestedValue } from "./utils";
import { array, textField } from "./fields";
import {
getNestedSchema,
getNestedValue,
setNestedValue,
validateField,
} from "./utils";
describe("getNestedValue", () => {
test("returns value at simple path", () => {
@@ -156,3 +162,27 @@ describe("getNestedSchema", () => {
expect(result?.type).toBe("string");
});
});
describe("validateField", () => {
const linksSchema = v.object({
links: array({
label: "labels.urls",
max: 10,
field: textField({ maxLength: 150, validate: "url" }),
}),
});
test.each([
{
why: "invalid url",
value: "twitter.com/me",
expected: "forms:errors.invalidUrl",
},
{ why: "empty", value: "", expected: "forms:errors.required" },
])(
"validates a row of a list of plain values ($why)",
({ value, expected }) => {
expect(validateField(linksSchema, "links[0]", value)).toBe(expected);
},
);
});

View File

@@ -208,9 +208,7 @@ export function validateField(
name: string,
value: unknown,
): string | undefined {
const fieldSchema = name.includes(".")
? getNestedSchema(schema, name)
: (schema.entries[name] as AnySyncSchema | undefined);
const fieldSchema = getNestedSchema(schema, name);
if (!fieldSchema) return undefined;
const result = v.safeParse(fieldSchema, value);

View File

@@ -0,0 +1,5 @@
---
navItem: [u, art]
type: bug
---
Fix invalid links on the links widget showing no error and Save silently doing nothing, and unclear errors for empty linked user rows on art