Skip to content

Commit

Permalink
fix: put form meta onto ZodEffect's inner type
Browse files Browse the repository at this point in the history
  • Loading branch information
hanayashiki committed Apr 25, 2023
1 parent 62ccd04 commit b94eb3b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/mobx-zod-form/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@monoid-dev/mobx-zod-form",
"version": "0.1.6",
"version": "0.1.7",
"keywords": [
"form",
"zod",
Expand Down
25 changes: 16 additions & 9 deletions packages/mobx-zod-form/src/zod-extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,22 @@ export function extendZodWithMobxZodForm(zod: typeof z) {
});

zod.ZodType.prototype.formMeta = function (meta: any) {
const o = new (this as any).constructor({
...this._def,
_formMeta: {
...this._formMeta,
...meta,
},
});

return o;
if (this instanceof ZodEffects) {
return new ZodEffects({
...this._def,
schema: this.innerType().formMeta(meta),
});
} else {
const o = new (this as any).constructor({
...this._def,
_formMeta: {
...this._formMeta,
...meta,
},
});

return o;
}
};

zod.ZodType.prototype.getFormMeta = function () {
Expand Down
10 changes: 10 additions & 0 deletions packages/mobx-zod-form/test/formMeta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,15 @@ describe("formMeta tests", () => {
expect(labeledAndEffectsAndLabeled.getFormMeta().label).toBe(
"transformed2",
);

const refinedAndLabeled = z
.string()
.refine((x) => x.length > 1, "length > 1")
.formMeta({ label: "transformed2" });

expect(refinedAndLabeled.getFormMeta().label).toBe("transformed2");
expect(refinedAndLabeled.innerType().getFormMeta().label).toBe(
"transformed2",
);
});
});

0 comments on commit b94eb3b

Please sign in to comment.