diff --git a/src/decorators/class.spec.ts b/src/decorators/class.spec.ts index 999f2a7..ef3641a 100644 --- a/src/decorators/class.spec.ts +++ b/src/decorators/class.spec.ts @@ -24,4 +24,16 @@ describe("@DocType() Decorator", function () { description: "test", }); }); + + it("should collect class data correctly with default values", function () { + DocType()(String); + + const target = getTypeStorage(); + + expect(target.classes).toHaveLength(1); + expect(target.classes[0].userData).toStrictEqual({ + name: "String", + description: "This is a type 'String'.", + }); + }); }); diff --git a/src/decorators/class.ts b/src/decorators/class.ts index a014318..5148693 100644 --- a/src/decorators/class.ts +++ b/src/decorators/class.ts @@ -3,12 +3,13 @@ import { ClassData } from "@utils/types"; type DocumentTypeOptions = ClassData["userData"]; -export function DocType(options: DocumentTypeOptions): ClassDecorator { +export function DocType(options?: DocumentTypeOptions): ClassDecorator { return target => { getTypeStorage().collectClassData({ classType: target, userData: { - ...options, + name: options?.name || target.name, + description: options?.description || `This is a type '${target.name}'.`, }, }); }; diff --git a/src/decorators/field.spec.ts b/src/decorators/field.spec.ts index 880a6d4..775ae1c 100644 --- a/src/decorators/field.spec.ts +++ b/src/decorators/field.spec.ts @@ -38,6 +38,26 @@ describe("@DocField() decorator", function () { }); }); + it("should collect field data correctly with default values", function () { + class MockedClass { + @DocField() + public test!: boolean; + } + + const target = getTypeStorage(); + const stringClass = target.classes.find(c => c.classType === MockedClass); + if (!stringClass) { + throw new Error("String class not found!"); + } + + expect(stringClass.fieldMap).toHaveProperty("test"); + expect(Object.keys(stringClass.fieldMap)).toHaveLength(1); + expect(stringClass.fieldMap["test"].userData).toStrictEqual({ + description: "field 'test' with type 'Boolean'.", + nullable: false, + }); + }); + it("should collect field with an array type correctly", () => { class MockedClass { @DocField({ diff --git a/src/decorators/field.ts b/src/decorators/field.ts index 4b54c26..9d997c0 100644 --- a/src/decorators/field.ts +++ b/src/decorators/field.ts @@ -7,7 +7,7 @@ interface DocumentFieldOptions extends FieldUserData { type?: TypeFn; } -export function DocField(fieldData: DocumentFieldOptions): DecoratorType { +export function DocField(fieldData?: DocumentFieldOptions): DecoratorType { return (target: object, propertyKey: string | symbol, descriptor?: TypedPropertyDescriptor) => { if (typeof propertyKey === "symbol") { throw new Error("Symbol keys are not supported yet!"); @@ -20,7 +20,7 @@ export function DocField(fieldData: DocumentFieldOptions): DecoratorType { } const type = Reflect.getMetadata("design:type", target, propertyKey); - const desiredType = fieldData.type?.(); + const desiredType = fieldData?.type?.(); const [targetType, isArray, isCustom] = checkType(type, desiredType, className, propertyKey); getTypeStorage().collectFieldData({ diff --git a/src/utils/types.ts b/src/utils/types.ts index 4a4e3f8..6e318aa 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -31,7 +31,7 @@ export interface ClassData { // user defined userData: { - name: string; - description: string; + name?: string; + description?: string; }; }