Skip to content

Commit

Permalink
🐛 Fix all validator not merging primitive errors correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
reckter committed Sep 22, 2022
1 parent 98dd89e commit 2bf9d81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#### 🚀 Enhancement

- :sparkles: Add someOf validator [#12](https://github.com/opencreek/vlad/pull/12) ([@reckter](https://github.com/reckter))
- :sparkles: Add someOf validator
[#12](https://github.com/opencreek/vlad/pull/12)
([@reckter](https://github.com/reckter))

#### Authors: 1

Expand Down
4 changes: 4 additions & 0 deletions src/validators/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export function all(...validators) {
return cur;
}

if (Array.isArray(acc) && Array.isArray(cur)) {
return [...acc, ...cur];
}

return deepMerge(acc, cur);
},
undefined,
Expand Down
13 changes: 13 additions & 0 deletions test/validators/all.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { all } from "../../src/validators/all.ts";
import { object } from "../../src/validators/object.ts";
import {
allItems,
is,
Expand Down Expand Up @@ -56,3 +57,15 @@ Deno.test("should correctly type check with multiple validators", () => {

assertEquals(a, ["Must have at least 2 items"]);
});

Deno.test("should merge complicated errors correctly", () => {
const validator = all(
requiredPrimitive("date required"),
(value: number) => {
return ["other date error"];
},
);

const output = validator(undefined);
assertEquals(output, ["date required", "other date error"]);
});

0 comments on commit 2bf9d81

Please sign in to comment.