Effect CommunityEC
Effect Community3y ago
58 replies
enricopolanski

Defining a Simple Schema for mongo's ObjectId

Is there a way to define a simple Schema with some success/failure like you could do in io-ts?

E.g., in io-ts using Decoder I could define a Decoder<unknown, ObjectId> in such a fashion:
import * as D from "io-ts/Decoder"
import { ObjectId } from "mongodb"

export const string: D.Decoder<unknown, ObjectId> = {
  decode: (u) => {
    try {
      const objectId = new ObjectId(u)
      return D.success(u)
    } catch {
      return D.failure(u, "ObjectId")
    }
  }
}

I'm not sure what would be the equivalent in schema
Was this page helpful?