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. Can be configured to patch @nestjs/swagger allowing OpenAPI gene

35 lines (27 loc) 1.22 kB
import { Type as NestType } from '@nestjs/common'; import { SchemaObjectFactory } from '@nestjs/swagger/dist/services/schema-object-factory.js'; import { isSchemaValidator } from './decorators.js'; 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 (!isSchemaValidator(type)) { return defaultExplore.apply(this, [type, schemas, schemaRefsStack]); } schemas[type.name] = type.schema; return type.name; }; SchemaObjectFactory.prototype.exploreModelSchema = extendedExplore; // eslint-disable-next-line @typescript-eslint/no-explicit-any (SchemaObjectFactory.prototype as any).__primatePatched = true; }