You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Classes that use @modelOptions() are not respecting setGlobalOptions. This is particular unobvious when the class only uses @modelOptions because it extends TimeStamps.
Code Example
import{buildSchema,prop,setGlobalOptions,Severity,}from'@typegoose/typegoose';import{TimeStamps}from'@typegoose/typegoose/lib/defaultClasses';setGlobalOptions({options: {allowMixed: Severity.ERROR}});classUserextendsTimeStamps{
@prop()username: string|undefined;// This will result in a Mixed type}buildSchema(User);// Only logs a warning, should throw an error
Do you know why it happens?
@modelOptions() attempts to apply the global options immediately, it doesn't wait for buildSchema to be called. Importing TimeStamps calls @modelOptions(), which happens before the setGlobalOptions call.
Expected Behavior
global options should be applied when buildSchema is called, not when @modelOptions is called.
Work around
Relies on imports loading in a specific order
// globalTypegooseOptions.tsimport{setGlobalOptions,Severity}from'@typegoose/typegoose';setGlobalOptions({options: {allowMixed: Severity.ERROR}});//---// main.tsimport'./globalTypegooseOptions';import{buildSchema,prop}from'@typegoose/typegoose';import{TimeStamps}from'@typegoose/typegoose/lib/defaultClasses';classUserextendsTimeStamps{
@prop()username: string|undefined;// This will result in a Mixed type}buildSchema(User);// Correctly throws an error
The text was updated successfully, but these errors were encountered:
yes, @modelOptions currently directly merges on decorator call (and on first call applies global options).
the only fix to this i can image is re-working how modelOptions works by storing all arguments in a array and on buildSchema apply / merge them in-order, which i would consider it being a breaking change to how it currently works.
workaround for now as pointed out is to put the setGlobalOptions before any class-related imports.
Versions
What is the Problem?
Classes that use
@modelOptions()
are not respectingsetGlobalOptions
. This is particular unobvious when the class only uses@modelOptions
because it extendsTimeStamps
.Code Example
Do you know why it happens?
@modelOptions()
attempts to apply the global options immediately, it doesn't wait forbuildSchema
to be called. ImportingTimeStamps
calls@modelOptions()
, which happens before thesetGlobalOptions
call.Expected Behavior
global options should be applied when
buildSchema
is called, not when@modelOptions
is called.Work around
Relies on imports loading in a specific order
The text was updated successfully, but these errors were encountered: