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
}
}
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
.
SOLUTION
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 PART https://github.com/swc-project/swc/issues/7603#issuecomment-1692146920
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;
}