From cd6c7a7bc1d2180978efa1707b5305617f450999 Mon Sep 17 00:00:00 2001 From: Yuki Takagi Date: Sun, 28 Jul 2024 21:53:26 +0900 Subject: [PATCH] bugfix: #3 Unable to call class methods --- src/index.ts | 22 +++++++++++++++++----- test/index.ts | 30 ++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8f5d82f..e9bf6fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,10 +33,15 @@ export const Validated = < schema: Schema, options?: Options, ) => { - const ctor = function Validated(value: z.input) { + const ctor = function Validated( + this: Record, + value: z.input, + ) { const validatedValue = schema.parse(value); const wrapValue = !isObject(validatedValue) || options?.wrapValue; - return wrapValue ? { value: validatedValue } : validatedValue; + const _this = wrapValue ? { value: validatedValue } : validatedValue; + Object.setPrototypeOf(_this, this); + return _this; } as unknown as ValidatedConstructor< Schema, Options extends { wrapValue: true } ? true : IsPrimitive> @@ -74,13 +79,16 @@ export const ValidatedMutable = < }); }; }; - const ctor = function ValidatedMutable(value: z.input) { + const ctor = function ValidatedMutable( + this: Record, + value: z.input, + ) { const validatedValue = schema.parse(value); if (!isObject(validatedValue) || options?.wrapValue) { const validatedValueProxy = isObject(validatedValue) ? makeValidatedValueProxy(value)(validatedValue) : validatedValue; - return new Proxy( + const _this = new Proxy( { value: validatedValueProxy }, { set(object, propertyName, newValue) { @@ -95,8 +103,12 @@ export const ValidatedMutable = < }, }, ); + Object.setPrototypeOf(_this, this); + return _this; } - return makeValidatedValueProxy(value)(validatedValue); + const _this = makeValidatedValueProxy(value)(validatedValue); + Object.setPrototypeOf(_this, this); + return _this; } as unknown as ValidatedMutableConstructor< Schema, Options extends { wrapValue: true } ? true : IsPrimitive> diff --git a/test/index.ts b/test/index.ts index 5fff54f..1269684 100644 --- a/test/index.ts +++ b/test/index.ts @@ -7,7 +7,11 @@ class Person extends Validated( name: z.string().min(1), age: z.number().nonnegative().int(), }), -) {} +) { + greet() { + return `Hi, It's ${this.name}!`; + } +} class Person2 extends Validated( z.object({ @@ -17,7 +21,11 @@ class Person2 extends Validated( { wrapValue: true, }, -) {} +) { + greet() { + return `Hi, It's ${this.value.name}!`; + } +} test("validate", (t) => { const person = new Person({ @@ -26,6 +34,7 @@ test("validate", (t) => { }); t.is(person.name, "John"); t.is(person.age, 30); + t.is(person.greet(), "Hi, It's John!"); const person2 = new Person2({ name: "John", @@ -33,6 +42,7 @@ test("validate", (t) => { }); t.is(person2.value.name, "John"); t.is(person2.value.age, 30); + t.is(person2.greet(), "Hi, It's John!"); }); test("validate error", (t) => { @@ -79,7 +89,11 @@ class PersonMutable extends ValidatedMutable( name: z.string().min(1), age: z.number().nonnegative().int(), }), -) {} +) { + greet() { + return `Hi, It's ${this.name}!`; + } +} class PersonMutable2 extends ValidatedMutable( z.object({ @@ -89,7 +103,11 @@ class PersonMutable2 extends ValidatedMutable( { wrapValue: true, }, -) {} +) { + greet() { + return `Hi, It's ${this.value.name}!`; + } +} test("validate mutable", (t) => { const person = new PersonMutable({ @@ -97,20 +115,24 @@ test("validate mutable", (t) => { age: 30, }); t.is(person.name, "John"); + t.is(person.greet(), "Hi, It's John!"); t.is(person.age, 30); person.name = "Jane"; person.age = 31; t.is(person.name, "Jane"); + t.is(person.greet(), "Hi, It's Jane!"); t.is(person.age, 31); const person2 = new PersonMutable2({ name: "John", age: 30, }); t.is(person2.value.name, "John"); + t.is(person2.greet(), "Hi, It's John!"); t.is(person2.value.age, 30); person2.value.name = "Jane"; person2.value.age = 31; t.is(person2.value.name, "Jane"); + t.is(person2.greet(), "Hi, It's Jane!"); t.is(person2.value.age, 31); });