输入 Mongoose 验证函数
Typing Mongoose validation functions
我正在帮助使用 MongoDB 来保存数据的打字稿应用程序。我们正在尝试做的其中一件事是摆脱 any
用法。
以下代码用于定义猫鼬模式的一部分:
priceMax: {
max: 10000000,
min: 0,
required: function (this: FeePricing & Document) {
return this.priceMin === undefined;
},
type: Number,
validate: [
{
message: 'Max price cannot be lower than min price',
validator: function (v: number) {
if ((this as any).priceMax === null || (this as any).priceMax === undefined) return true;
return (this as any).priceMin ? v >= (this as any).priceMin : v >= 0;
},
},
{
message: 'Max price cannot be higher than 50000 for this feeType',
validator: function (v: number) {
return !(!feeTypesWithoutMaxLimit.includes((this as any).feeType) && v > 50000);
},
},
],
},
priceMin: {
max: 10000000,
min: 0,
required: function () {
return (this as any).priceMax === undefined;
},
type: Number,
validate: {
message: 'priceMin cannot be higher than priceMax',
validator: function (v: number) {
return (this as any).priceMax ? v <= (this as any).priceMax : v >= 0;
},
},
},
updatedAt: { type: Date },
updatedBy: { type: String },
我有点理解函数的作用,但这里的类型让我感到困惑。
我怎样才能摆脱 this as any
?为什么不直接使用 FeePricing
作为类型——例如 (this as FeePricing)
? FeePricing
似乎只是我的应用程序 [ 中的另一种类型,它具有 priceMin
和 priceMax
] 以及 Document
界面。 ReactJS 中的 Document
在这里有什么帮助?为什么需要它? validate
中的this
是上面定义的类型FeePricing & Document
吗?
谢谢
this
是您的验证配置的上下文。因为 TypeScript 无法推断其类型(因为它可以更改),所以我建议创建您自己的自定义类型,例如 FeePricing
。我不太确定您当前的 FeePricing
包含哪些属性,因为它未包含在示例中,但我希望它如下所示:
interface FeePricing {
priceMin?: mongoose.Schema.Types.Number | null,
priceMax?: mongoose.Schema.Types.Number | null,
feeType?: mongoose.Schema.Types.Number | null,
}
那么你可以这样使用它:
(this as FeePricing).priceMax
之所以属性是可选的,也是空的,是因为我可以看到你的一些逻辑检查它们是 undefined
还是 null
,因此这些类型将反映它们可能不是存在于运行时并帮助您正确验证。此外,如果 FeePricing
类型用于其他用途,您当然可以将此类型名称更改为其他名称。
回答你关于 ReactJs 的问题 Document
,它没有添加任何帮助来推断 mongoose 配置类型并且真的可以被删除。
据我所知,在 Mongoose 中,模式用于定义存储在 MongoDB 中的文档。如果我是正确的,你可以创建一个 model/interface 的 Feepricing 并将其用作类型。
export interface FeePricing {
priceMax: number;
priceMin: number;
}
this
是 FreePricing 对象。
希望对您有所帮助
我正在帮助使用 MongoDB 来保存数据的打字稿应用程序。我们正在尝试做的其中一件事是摆脱 any
用法。
以下代码用于定义猫鼬模式的一部分:
priceMax: {
max: 10000000,
min: 0,
required: function (this: FeePricing & Document) {
return this.priceMin === undefined;
},
type: Number,
validate: [
{
message: 'Max price cannot be lower than min price',
validator: function (v: number) {
if ((this as any).priceMax === null || (this as any).priceMax === undefined) return true;
return (this as any).priceMin ? v >= (this as any).priceMin : v >= 0;
},
},
{
message: 'Max price cannot be higher than 50000 for this feeType',
validator: function (v: number) {
return !(!feeTypesWithoutMaxLimit.includes((this as any).feeType) && v > 50000);
},
},
],
},
priceMin: {
max: 10000000,
min: 0,
required: function () {
return (this as any).priceMax === undefined;
},
type: Number,
validate: {
message: 'priceMin cannot be higher than priceMax',
validator: function (v: number) {
return (this as any).priceMax ? v <= (this as any).priceMax : v >= 0;
},
},
},
updatedAt: { type: Date },
updatedBy: { type: String },
我有点理解函数的作用,但这里的类型让我感到困惑。
我怎样才能摆脱 this as any
?为什么不直接使用 FeePricing
作为类型——例如 (this as FeePricing)
? FeePricing
似乎只是我的应用程序 [ 中的另一种类型,它具有 priceMin
和 priceMax
] 以及 Document
界面。 ReactJS 中的 Document
在这里有什么帮助?为什么需要它? validate
中的this
是上面定义的类型FeePricing & Document
吗?
谢谢
this
是您的验证配置的上下文。因为 TypeScript 无法推断其类型(因为它可以更改),所以我建议创建您自己的自定义类型,例如 FeePricing
。我不太确定您当前的 FeePricing
包含哪些属性,因为它未包含在示例中,但我希望它如下所示:
interface FeePricing {
priceMin?: mongoose.Schema.Types.Number | null,
priceMax?: mongoose.Schema.Types.Number | null,
feeType?: mongoose.Schema.Types.Number | null,
}
那么你可以这样使用它:
(this as FeePricing).priceMax
之所以属性是可选的,也是空的,是因为我可以看到你的一些逻辑检查它们是 undefined
还是 null
,因此这些类型将反映它们可能不是存在于运行时并帮助您正确验证。此外,如果 FeePricing
类型用于其他用途,您当然可以将此类型名称更改为其他名称。
回答你关于 ReactJs 的问题 Document
,它没有添加任何帮助来推断 mongoose 配置类型并且真的可以被删除。
据我所知,在 Mongoose 中,模式用于定义存储在 MongoDB 中的文档。如果我是正确的,你可以创建一个 model/interface 的 Feepricing 并将其用作类型。
export interface FeePricing {
priceMax: number;
priceMin: number;
}
this
是 FreePricing 对象。
希望对您有所帮助