Skip to content

Commit

Permalink
bugfix: #3 Unable to call class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
takagiy committed Jul 28, 2024
1 parent 7d6384b commit cd6c7a7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ export const Validated = <
schema: Schema,
options?: Options,
) => {
const ctor = function Validated(value: z.input<typeof schema>) {
const ctor = function Validated(
this: Record<string, unknown>,
value: z.input<typeof schema>,
) {
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<z.infer<Schema>>
Expand Down Expand Up @@ -74,13 +79,16 @@ export const ValidatedMutable = <
});
};
};
const ctor = function ValidatedMutable(value: z.input<typeof schema>) {
const ctor = function ValidatedMutable(
this: Record<string, unknown>,
value: z.input<typeof schema>,
) {
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) {
Expand All @@ -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<z.infer<Schema>>
Expand Down
30 changes: 26 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -26,13 +34,15 @@ 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",
age: 30,
});
t.is(person2.value.name, "John");
t.is(person2.value.age, 30);
t.is(person2.greet(), "Hi, It's John!");
});

test("validate error", (t) => {
Expand Down Expand Up @@ -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({
Expand All @@ -89,28 +103,36 @@ class PersonMutable2 extends ValidatedMutable(
{
wrapValue: true,
},
) {}
) {
greet() {
return `Hi, It's ${this.value.name}!`;
}
}

test("validate mutable", (t) => {
const person = new PersonMutable({
name: "John",
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);
});

Expand Down

0 comments on commit cd6c7a7

Please sign in to comment.