orm/src/schema/types/ObjectId.js

/**
 * @module flitter-orm/src/schema/types/ObjectId
 */

const Type = require('../Type')
const { ObjectId } = require('mongodb')

/**
 * Schema type representing a MongoDB ObjectId instance.
 * @extends {module:flitter-orm/src/schema/Type~Type}
 */
class ObjectIdType extends Type {

    /**
     * Determines whether the specified value can be cast to an ObjectId.
     * @param {*} value
     * @returns {boolean}
     */
    static validate(value) {
        try {
            ObjectId(value)
            return true
        } catch (e) {
            return false
        }
    }

    /**
     * Casts the specified value to an ObjectId.
     * @param value
     * @returns {ObjectId}
     */
    static cast(value) {
        return ObjectId(value)
    }
}

module.exports = exports = ObjectIdType