Skip to content

Commit 0aadfc7

Browse files
committed
fix(fmodata): improve listField validation to handle undefined items
This commit updates the listField function to use a more efficient check for non-string items in the input array. It also adds a test case to ensure that undefined list items are correctly rejected without throwing an error when no itemValidator is provided, enhancing input validation and error handling.
1 parent db2217f commit 0aadfc7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/fmodata/src/orm/field-builders.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ export function listField<TItem = string, TAllowNull extends boolean = false>(
313313
}
314314

315315
if (!itemValidator) {
316-
const nonStringItem = input.find((item) => typeof item !== "string");
317-
if (nonStringItem !== undefined) {
316+
const hasNonStringItem = input.some((item) => typeof item !== "string");
317+
if (hasNonStringItem) {
318318
return { issues: [issue("Expected all list items to be strings without an itemValidator")] };
319319
}
320320
const serialized = input.map((item) => normalizeFileMakerNewlines(item)).join(FILEMAKER_LIST_DELIMITER);

packages/fmodata/tests/orm-api.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ describe("ORM API", () => {
109109
const writeResult = await config.inputValidator?.["~standard"].validate([1, 2, 3]);
110110
expect(writeResult).toEqual({ value: "1\r2\r3" });
111111
});
112+
113+
it("should reject undefined list items without throwing when no itemValidator is provided", async () => {
114+
const field = listField();
115+
const config = field._getConfig();
116+
117+
const writeResult = await config.inputValidator?.["~standard"].validate([
118+
"A",
119+
undefined,
120+
"C",
121+
] as unknown as string[]);
122+
expect(writeResult).toEqual({
123+
issues: [{ message: "Expected all list items to be strings without an itemValidator" }],
124+
});
125+
});
112126
});
113127

114128
describe("Table Definition", () => {

0 commit comments

Comments
 (0)