mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-27 21:18:30 -05:00
Fix list rows like links skipping validation in forms
This commit is contained in:
@@ -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);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
5
changelog/2026-10-20-list-field-validation.md
Normal file
5
changelog/2026-10-20-list-field-validation.md
Normal 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
|
||||
Reference in New Issue
Block a user