skip to Main Content

I’m first time working with SWC , and I’m facing this error only with SWC compiler. Without SWC everything works fine

Error

throw new TypeError(Invalid schema configuration: ${val}` is not ` +
^

TypeError: Invalid schema configuration: admin is not a valid type at path ADMIN.
Blockquote

Seelist of valid schema types.

Description

I have a User schema, In which there’s a property named by role and it’s type is an enum named Role.

enum

export enum Role {
  ADMIN = 'admin',
  SALES = 'sales',
  MARKETING = 'marketing',
}

Code

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { Role } from '../../../enum/role';

@Schema({ timestamps: true })
export class User {
  @Prop({ required: true, default: null })
  email: string;

  @Prop({ required: true, default: null })
  password: string;

  @Prop({ required: true, enum: Role, default: Role.ADMIN })
  role: Role;
}

export const UserSchema = SchemaFactory.createForClass(User);
export type UserDocumnet = User & Document;

The error goes away If I remove Role as a type ,like this

@Prop({ required: true, enum: Role, default: Role.ADMIN })
      role: string;

It throws error for whatever value is present at index 0 of the Role enum. For example If I move SALES uprward inside enum, it shows sales inside error

SWC Config

I installed swc with

npm i --save-dev @swc/cli @swc/core

nest-cli.json

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "builder": "swc",
    "typeCheck": true
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    So I found the Solution with the help of the answer of @Robin Thomas. According to his answer @nestjs/mongoose is not converting enum correctly when using SWC. And when we use TSC comnpiler, it converts the enum correctly. Now it means that it's related to how SWC handles enum convertion. So I searched and found that SWC can not work well with outside enum files. Not just that, SWC does not work well with auto imports, if you try to call one ts file into another ts file, if your imports are starting from src/etc/etc,

    SWC throws error

    CAN NOT FIND MODULE xyz.

    2 SOLUTIONS

    1- I'm used to keep enum files in a seperate folder at root of source or src. But with specially SWC It doesn't work if the enum is imported from another file. It compiles properly when the enum is declared in the same file, though..

    Also you have to import files from outisde like this ../etc/ect, and with src/etc/etc.

    CHECK THIS DISCUSSION FOR ENUM PART https://github.com/swc-project/swc/issues/1160#issuecomment-738320066

    CHECK THIS DISCUSSION FOR IMPORT PASRT https://github.com/swc-project/swc/issues/7603#issuecomment-1692146920

    It is very annoying to import again and again every single file by yourself.

    So I did this and it worked

    export enum Role {
      ADMIN = 'admin',
      SALES = 'sales',
      MARKETING = 'marketing',
    }
    
    @Schema({ timestamps: true })
    export class User {
      @Prop({ required: true, default: null })
      email: string;
    
      @Prop({ required: true, default: null })
      password: string;
    
      @Prop({ required: true, enum: Role, default: Role.ADMIN })
      role: Role;
    }
    

    2-SWC is not fully stable yet, specially for auto improrts, or absolute paths. If you want to keep enums in a seperate file than you can create a .swcrc file at thr root directory. with following content

    {
      "$schema": "https://json.schemastore.org/swcrc",
      "sourceMaps": true,
      "jsc": {
        "parser": {
          "syntax": "typescript",
          "decorators": true,
          "dynamicImport": true
        },
        "baseUrl": "./src",
        "paths": {
          "*": ["*", "src/*"]
        }
      },
      "minify": false
    }
    

    Also you have to provide the same path in tsconfig.json , "baseUrl": "./src",, otherwise you will find errors.. With this workaround you can keep the enum seperate , also you don't have import issues.

    enum

    export enum Role {
      ADMIN = 'admin',
      SALES = 'sales',
      MARKETING = 'marketing',
    }
    

    Code

    import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
    import { Document } from 'mongoose';
    import { Role } from '../../../enum/role';
    
    @Schema({ timestamps: true })
    export class User {
      @Prop({ required: true, default: null })
      email: string;
    
      @Prop({ required: true, default: null })
      password: string;
    
      @Prop({ required: true, enum: Role, default: Role.ADMIN })
      role: Role;
    }
    
    export const UserSchema = SchemaFactory.createForClass(User);
    export type UserDocumnet = User & Document;
    

    You will find the errors in terminal when you'll run the app but the app will work fine.At least for now. But If you'll try to do npm run build, it will fail.


  2. Looks like this a bug in NestJS v10 when built using SWC. I cannot reproduce the issue when using v7 or v8 of nestjs packages (without SWC).

    Regarding the issue itself, it comes from @nestjs/mongoose trying to create a second schema (apart from User entity).

    The schema from the original entity takes the form:

    {
      email: { required: true, default: null, type: [Function: String] },
      password: { required: true, default: null, type: [Function: String] },
      role: {
        required: true,
        enum: { ADMIN: 'admin', SALES: 'sales', MARKETING: 'marketing' },
        default: 'admin',
        type: { ADMIN: 'admin', SALES: 'sales', MARKETING: 'marketing' }
      }
    }
    

    But while processing the above schema, @nestjs/mongoose try to create a second schema, which takes the form:

    { ADMIN: 'admin', SALES: 'sales', MARKETING: 'marketing' }
    

    This is not a valid mongoose schema as per https://mongoosejs.com/docs/guide.html#definition.

    Since this is not a valid schema, and hence the error.


    The issue pops up only while using SWC.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search