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 pathADMIN
.
BlockquoteSeelist 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
So I found the Solution with the help of the answer of @Robin Thomas. According to his answer
@nestjs/mongoose
is not convertingenum
correctly when usingSWC
. And when we useTSC
comnpiler, it converts theenum
correctly. Now it means that it's related to howSWC
handlesenum
convertion. So I searched and found thatSWC
can not work well with outsideenum
files. Not just that,SWC
does not work well withauto imports
, if you try to call onets file
into anotherts file
, if your imports are starting fromsrc/etc/etc
,SWC
throws error2 SOLUTIONS
1- I'm used to keep enum files in a seperate folder at
root of source or src
. But with speciallySWC
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 withsrc/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
2-
SWC
is not fully stable yet, specially forauto improrts
, orabsolute paths
. If you want to keepenums
in a seperate file than you can create a.swcrc
file at thr root directory. with following contentAlso you have to provide the same path in
tsconfig.json
,"baseUrl": "./src",
, otherwise you will find errors.. With this workaround you can keep theenum
seperate , also you don't have import issues.enum
Code
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.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 fromUser
entity).The schema from the original entity takes the form:
But while processing the above schema,
@nestjs/mongoose
try to create a second schema, which takes the form: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.