diff --git a/packages/input/src/Type/ObjectType.ts b/packages/input/src/Type/ObjectType.ts index fb8813d9..327489a1 100644 --- a/packages/input/src/Type/ObjectType.ts +++ b/packages/input/src/Type/ObjectType.ts @@ -1,6 +1,6 @@ import {entriesReverse, isFunction, isInstanceOf, isObject, MayNullable} from "@typesafeunit/util"; import {AssertionObjectError, AssertionTypeError, IReadableTypeError} from "../Assertion"; -import {ObjectFields} from "../interfaces"; +import {ObjectFields, ObjectTypeMerge} from "../interfaces"; import {TypeAbstract} from "../TypeAbstract"; export class ObjectType> extends TypeAbstract> { @@ -17,6 +17,22 @@ export class ObjectType> extends TypeAbstract return this.#name; } + public get fields(): ObjectFields { + return this.#fields; + } + + public merge>(from: ObjectTypeMerge): ObjectType { + if (isInstanceOf(from, ObjectType)) { + return new ObjectType( + Object.assign({}, this.fields, from) as any, + ); + } + + return new ObjectType( + Object.assign({}, this.fields, from.fields) as any, + ); + } + public async validate(payload: MayNullable>): Promise { this.assert(isObject(payload), `Wrong payload: ${this.name} expected`, payload); diff --git a/packages/input/src/interfaces.ts b/packages/input/src/interfaces.ts index 0665fd70..bd35ca7e 100644 --- a/packages/input/src/interfaces.ts +++ b/packages/input/src/interfaces.ts @@ -31,3 +31,5 @@ export type FieldSelectType = T extends Record : T extends Array ? List : TypeAbstract; + +export type ObjectTypeMerge> = ObjectType | ObjectFields;