我有一个“用户”架构,其中包含对象类型的“联系人”字段。联系人(地址)字段的字段之一也是一个对象。 “地址”也是对象类型,并且具有一些所需的字段。所以我相应地制作了架构。
有效负载示例:
{
name: 'Clark',
contact: {
phone: 876543212,
address: {
doorNo: 4,
street: '',
city: '',
pincode: 123456
}
}
}
架构:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
@Schema({ _id: false })
class ContactDetails {
@Prop({ type: Number, required: true })
phone: number;
@Prop({ type: Object, required: true })
Address: {
type: {
doorNo: {
type: string;
required: true;
};
street: {
type: string;
required: true;
};
city: {
type: CityEnum;
required: true;
};
pincode: number;
};
};
}
@Schema()
export class User {
@Prop({ type: String, required: true })
name: string;
@Prop({ type: ContactDetails, required: true })
contact: ContactDetails;
}
export const UserSchema = SchemaFactory.createForClass(User);
数据库中已正确创建记录。但是,当我通过不传递“地址”的任何必填字段进行测试时,我没有收到适当的错误(对于必需的)。因此,我认识到上面的“ContactDetails”嵌套模式没有进行模式验证。
因此,我尝试了另一种方法,将地址架构创建为“AddressDetails”,以便我可以直接对地址字段应用 @Prop 验证。但如果缺少任何必需的地址字段,仍然不会引发错误。
@Schema({ _id: false })
class AddressDetails {
@Prop({ type: Number, required: true })
doorNo: number;
@Prop({ type: String, required: true })
street: string;
@Prop({ type: String, enum: CityEnum, required: true })
city: CityEnum;
@Prop({ type: Number })
pincode: number;
}
@Schema()
export class User {
@Prop({ type: String, required: true })
name: string;
@Prop({ type: Object, required: true })
contact: {
type: {
phone: String;
address: { type: AddressDetails, required: true };
};
};
}
export const UserSchema = SchemaFactory.createForClass(User);
我想要一个正常工作的模式,如果任何必需的字段未传递到嵌套模式,它会捕获并抛出错误。
请为我提供上述问题的解决方案,提前致谢!
提出问题后,我自己在 30 分钟内就找到了解决方案。
需要以适当的类型传递@Props。嵌套路径或嵌套模式是否也应该使用适当的类型进行验证以及是否需要。
方法1:包含嵌套路径的嵌套模式。
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
@Schema({
_id: false
})
class ContactDetails {
@Prop({
type: Number,
required: true
})
phone: number;
@Prop({
type: {
doorNo: {
type: String,
required: true
}
street: {
type: String;
required: true;
};
city: {
type: String;
enum: CityEnum;
required: true;
};
pincode: Number;
},
_id: false
})
Address: {
type: {
doorNo: {
type: string;
required: true;
};
street: {
type: string;
required: true;
};
city: {
type: string;
enum: CityEnum;
required: true;
};
pincode: number;
};
};
}
@Schema()
export class User {
@Prop({
type: String,
required: true
})
name: string;
@Prop({
type: ContactDetails,
required: true
})
contact: ContactDetails;
}
export const UserSchema = SchemaFactory.createForClass(User);
与方法 2 类似,应创建一个 Nestjs 猫鼬模式并将其传递给 @Props 中的类型,同时在实际字段的(地址)类型中传递嵌套模式的类。
方法 2:包含嵌套架构的嵌套路径。
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
@Schema({
_id: false
})
class AddressDetails {
@Prop({
type: Number,
required: true
})
doorNo: number;
@Prop({
type: String,
required: true
})
street: string;
@Prop({
type: String,
enum: CityEnum,
required: true
})
city: CityEnum;
@Prop({
type: Number
})
pincode: number;
}
const addressDetailsSchema = SchemaFactory.CreateForClass(AddressDetails);
@Schema()
export class User {
@Prop({
type: String,
required: true
})
name: string;
@Prop({
type: {
phone: Number
address: addressDetailsSchema
},
_id: false
})
contact: {
type: {
phone: number;
address: {
type: AddressDetails,
required: true
};
};
};
}
export const UserSchema = SchemaFactory.createForClass(User);
正如您所见,在上述两种方法中,方法1是可读的,因此更喜欢它。
编码快乐!