import {ObjectId as ObjectIdClass} from 'bson'; import * as x from 'x-value'; export const objectIdSymbol = Symbol(); export const ObjectId = x.atomic(objectIdSymbol, value => x.constraint( typeof value === 'string' && ObjectIdClass.isValid(value), 'Expected an ObjectId hex string.', ), ); /** * Unlike ObjectId, ObjectIdHexString is string in every medium. */ export const ObjectIdHexString = x.string.refined<'object id'>(value => x.refinement( typeof value === 'string' && ObjectIdClass.isValid(value), 'Expected an ObjectId hex string.', ), ); /** * BSON ObjectId in string. */ export type ObjectIdString = x.Nominal<'object id', string>; export type BSONObjectIdConstructor = new ( id?: T, ) => x.TransformNominal; export const BSONObjectId = ObjectIdClass as unknown as BSONObjectIdConstructor; // eslint-disable-next-line @typescript-eslint/consistent-type-definitions export interface BSONObjectId extends ObjectIdClass { toHexString(): x.TransformNominal; }