我的目标是通过使用JSON模式验证来改善我们MongoDB数据库中的数据质量。我们的项目中使用typescript,并且为所有集合都有接口。
因此,我基本上正在寻找一种有效的方式来:
将此接口转换为;
因此,我基本上正在寻找一种有效的方式来:
将此接口转换为;
import { ObjectId } from 'mongodb';
export interface Category {
_id: ObjectId;
date: Date;
level: string | null;
}
转换为这个 JSON Schema
export const CategoryJSONSchema = {
required: ['_id', 'date', 'level'],
additionalProperties: false,
properties: {
_id: { bsonType: 'objectId' },
date: { bsonType: 'date' },
level: { oneOf: [{ bsonType: 'null' }, { bsonType: 'string' }] }
}
}