Create a Mongoose schema that is not required, of type string, with enum and default null?

huangapple go评论65阅读模式
英文:

Create a Mongoose schema that is not required, of type string, with enum and default null?

问题

可以设置一个满足以下要求的 mongoose 模式:

  1. 不是必需的
  2. 类型为 "string" type: String
  3. 具有枚举值 enum: ['red', 'green']
  4. 具有默认值为 null default: null
英文:

Is it possible to set up a mongoose schema that fulfils the following requirements:

  1. is NOT required
  2. is of type "string" type: String
  3. has an enum enum: ['red', 'green']
  4. has a default set to null default: null

答案1

得分: 1

我成功找到了答案。当我多读一些并理解定义不同内容时,一切变得清晰。

目标:我想要一个 mongoose 模式属性,它可以是 nullstring。如果是 string,我想要验证并检查该值是否严格等于给定数组中的一个值。

color: {
  type: String,
  enum: ['red', 'green'],
  default: null
}

不会起作用。有两种情况:

  1. 如果传入对象上存在 color 属性,则其值将由 enum 验证器验证,一切正常。
  2. 如果传入对象上不存在 color 属性,我希望将 null 保存为 color 的值。在这种情况下,传入的属性 colorundefined,因此我们为 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:

  1. If the color property exists on the incoming object, then its
    value gets validated by the enum validator and all is good.
  2. If the color property does not exist on the incoming object, I
    want to save null as a value for color. In this case, the
    incoming property color is undefined, so whatever we've defined
    for the default is taken as the value to be set for color and
    like in the first scenario, it will be validated by the enum
    validator. Obviously, having null will result in an error. To
    avoid that, I replaced the enum 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.

huangapple
  • 本文由 发表于 2023年5月22日 15:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定