Commit ad5e8e52 authored by nanahira's avatar nanahira

use defineProperty

parent 99ad6ae1
......@@ -146,17 +146,23 @@ export function SchemaClass<T>(originalClass: ClassType<T>) {
});
Object.setPrototypeOf(newClass, originalClass.prototype);
for (const field of schemaFields) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
newClass[field] = schema[field];
Object.defineProperty(newClass, field, {
configurable: true,
enumerable: true,
writable: true,
value: schema[field],
});
}
for (const functionField of schemaFunctions) {
if (newClass[functionField]) {
continue;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
newClass[functionField] = Schema.prototype[functionField];
Object.defineProperty(newClass, functionField, {
configurable: true,
enumerable: false,
writable: false,
value: Schema.prototype[functionField],
});
}
newClass[kSchema] = true;
......
......@@ -41,4 +41,10 @@ describe('Schema registration', () => {
it('should throw if required field is missing', () => {
expect(() => new ConfigWithRequiredFields({})).toThrow();
});
it('should act functions of schema', () => {
const schema = Config as Schema<Config>;
const newSchema = schema.description('foo');
expect(newSchema.meta.description).toBe('foo');
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment