英文:
Create a Mongoose schema that is not required, of type string, with enum and default null?
问题
可以设置一个满足以下要求的 mongoose 模式:
- 不是必需的
- 类型为 "string"
type: String
- 具有枚举值
enum: ['red', 'green']
- 具有默认值为 null
default: null
英文:
Is it possible to set up a mongoose schema that fulfils the following requirements:
- is NOT required
- is of type "string"
type: String
- has an enum
enum: ['red', 'green']
- has a default set to null
default: null
答案1
得分: 1
我成功找到了答案。当我多读一些并理解定义不同内容时,一切变得清晰。
目标:我想要一个 mongoose 模式属性,它可以是 null
或 string
。如果是 string
,我想要验证并检查该值是否严格等于给定数组中的一个值。
color: {
type: String,
enum: ['red', 'green'],
default: null
}
这不会起作用。有两种情况:
- 如果传入对象上存在
color
属性,则其值将由enum
验证器验证,一切正常。 - 如果传入对象上不存在
color
属性,我希望将null
保存为color
的值。在这种情况下,传入的属性color
为undefined
,因此我们为default
定义的任何内容都将作为color
的值设置,并像第一种情况一样,将由enum
验证器验证。显然,拥有null
将导致错误。为了避免这种情况,我用自定义验证器替换了enum
验证器。
color: {
type: String,
validate: (value: string | null) => {
if (value === null) return true;
return ['red', 'green'].includes(value); // you could use enum here
// return Object.values(MyEnum).includes(value as MyEnum);
},
default: null
}
我们的自定义验证器将始终运行,因为根据Mongoose的文档,验证器不会在未定义的值上运行,而我们在任何情况下都不会有未定义的值。
**注意:**唯一不适用于Mongoose规则的是 required
验证器,它将保证在其他验证器运行时 color
不会是 undefined
。但这不是我的情况。我希望 color
是可选的。我的原因是我不希望前端在业务逻辑不要求时必须确保发送它。
英文:
So, I managed to figure it out. It became clear when I read a bit more and understood what happens when you define different things.
Goal: I want to have a mongoose schema property that's either null
or string
. If it's a string
, I want to validate and check if the value is strictly equal to one of the values in the given array.
color: {
type: String,
enum: ['red', 'green'],
default: null
}
This won't work. There are two scenarios:
- If the
color
property exists on the incoming object, then its
value gets validated by theenum
validator and all is good. - If the
color
property does not exist on the incoming object, I
want to savenull
as a value forcolor
. In this case, the
incoming propertycolor
isundefined
, so whatever we've defined
for thedefault
is taken as the value to be set forcolor
and
like in the first scenario, it will be validated by theenum
validator. Obviously, havingnull
will result in an error. To
avoid that, I replaced theenum
validator with a custom validator.
color: {
type: String,
validate: (value: string | null) => {
if (value === null) return true;
return ['red', 'green'].includes(value); // you could use enum here
// return Object.values(MyEnum).includes(value as MyEnum);
},
default: null
}
Our custom validator will always run, because according to Mongoose's docs, validators are not run on undefined values, which we won't have in any case.
Note: The only exception to Mongoose's rule is the required
validator, which will guarantee that color
won't be undefined
when other validators run. But that is not my case. I want color
to be optional. My reason is I don't want the front-end to make sure and send it if it's not required by business logic.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论