UNPKG

nestjs-typebox

Version:

This library provides helper utilities for writing and validating NestJS APIs using [TypeBox](https://github.com/sinclairzx81/typebox) as an alternative to class-validator/class-transformer. It also includes a patch for @nestjs/swagger allowing OpenAPI ge

34 lines (27 loc) 1.2 kB
import { Type as NestType } from '@nestjs/common'; import { SchemaObjectFactory } from '@nestjs/swagger/dist/services/schema-object-factory'; import { isTypeboxDto } from './util'; export function patchNestJsSwagger() { // eslint-disable-next-line @typescript-eslint/no-explicit-any if ((SchemaObjectFactory.prototype as any).__primatePatched) return; const defaultExplore = SchemaObjectFactory.prototype.exploreModelSchema; const extendedExplore: SchemaObjectFactory['exploreModelSchema'] = function exploreModelSchema( this: SchemaObjectFactory, type, schemas, schemaRefsStack ) { if (this['isLazyTypeFunc'](type)) { const factory = type as () => NestType<unknown>; type = factory(); } if (!isTypeboxDto(type)) { return defaultExplore.apply(this, [type, schemas, schemaRefsStack]); } schemas[type.name] = type.toJsonSchema(); return type.name; }; SchemaObjectFactory.prototype.exploreModelSchema = extendedExplore; // eslint-disable-next-line @typescript-eslint/no-explicit-any (SchemaObjectFactory.prototype as any).__primatePatched = true; }