是否有任何方法可以获得moongoose模式中使用的验证器列表?



我想获得moongoose模式中使用的验证器列表?就像

const userModel = {
firstName: {
type:String,
required: true
}
}
// is there any method to get validations like
console.log(userModel.getValidators())
Output:
{
firstName: {required: true ....etc},
}

使用SchemaFactory.createForClass方法设置模型后,您可以导出模式,该方法来自文档中显示的带有@Schema装饰器的类。如果您导入模式并访问其obj属性,则可以提取有关该字段的信息。

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
export type CatDocument = Cat & Document;
@Schema()
export class Cat {
@Prop({ required: true })
name: string;
@Prop()
age: number;
@Prop()
breed: string;
}
export const CatSchema = SchemaFactory.createForClass(Cat);
import { CatSchema } from './cat.schema';
console.log(CatSchema.obj.name.required);  // true
console.log(CatSchema.obj.age.type.name);  // 'Number'

最新更新