Yuki Takei 1 год назад
Родитель
Сommit
5e76ff6f06
1 измененных файлов с 11 добавлено и 1 удалено
  1. 11 1
      packages/core/src/interfaces/common.ts

+ 11 - 1
packages/core/src/interfaces/common.ts

@@ -2,9 +2,10 @@
  * Common types and interfaces
  * Common types and interfaces
  */
  */
 
 
-
 import type { Types } from 'mongoose';
 import type { Types } from 'mongoose';
 
 
+import { isValidObjectId } from '../utils/objectid-utils';
+
 type ObjectId = Types.ObjectId;
 type ObjectId = Types.ObjectId;
 
 
 // Foreign key field
 // Foreign key field
@@ -12,6 +13,15 @@ export type Ref<T> = string | ObjectId | T & { _id: string | ObjectId };
 
 
 export type Nullable<T> = T | null | undefined;
 export type Nullable<T> = T | null | undefined;
 
 
+export const isRef = <T>(obj: unknown): obj is Ref<T> => {
+  return obj != null
+    && (
+      (typeof obj === 'string' && isValidObjectId(obj))
+        || (typeof obj === 'object' && '_bsontype' in obj && obj._bsontype === 'ObjectID')
+        || (typeof obj === 'object' && '_id' in obj)
+    );
+};
+
 export const isPopulated = <T>(ref: Ref<T>): ref is T & { _id: string | ObjectId } => {
 export const isPopulated = <T>(ref: Ref<T>): ref is T & { _id: string | ObjectId } => {
   return ref != null
   return ref != null
     && typeof ref !== 'string'
     && typeof ref !== 'string'