Skip to content

Commit

Permalink
fix: getValibotConstraint to work with nullish schema (#37)
Browse files Browse the repository at this point in the history
* nullish schemas have no required constraint

* fix formatting
  • Loading branch information
mephju authored Oct 6, 2024
1 parent 3453ec9 commit 49a78ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions constraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export function getValibotConstraint<
constraint.required = false;
// @ts-expect-error
updateConstraint(schema.wrapped, data, name);
} else if (schema.type === "nullish") {
constraint.required = false;
// @ts-expect-error
updateConstraint(schema.wrapped, data, name);
} else if (schema.type === "number") {
// @ts-expect-error
const minValue = schema.pipe?.find(
Expand Down
5 changes: 5 additions & 0 deletions tests/constraint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
maxValue,
minLength,
minValue,
nullish,
number,
object,
optional,
Expand Down Expand Up @@ -83,6 +84,7 @@ describe("constraint", () => {
pipe(string(), minLength(3, "min")),
optional(pipe(number(), maxValue(100, "max"))),
]),
nullishString: nullish(string()),
}),
check(() => false, "refine"),
);
Expand Down Expand Up @@ -145,6 +147,9 @@ describe("constraint", () => {
required: false,
max: 100,
},
nullishString: {
required: false,
},
};

expect(getValibotConstraint(schema)).toEqual(constraint);
Expand Down

0 comments on commit 49a78ca

Please sign in to comment.