Merge pull request #36842 from overleaf/em-tpds-typescript

Convert third-party-datastore to TypeScript

GitOrigin-RevId: 028172063d0cfe03c0f105f48ab66020c79ea50e
This commit is contained in:
Eric Mc Sween
2026-09-17 12:06:14 +00:00
committed by Copybot
parent a9ad734cf4
commit 2024f85941
7 changed files with 159 additions and 2 deletions
@@ -1,10 +1,43 @@
import { zz } from '../../../zodHelpers'
import { describe, expect, it } from 'vitest'
import mongodb from 'mongodb'
import { z } from 'zod'
const { ObjectId } = mongodb
describe('zodHelpers', () => {
describe('optional', () => {
it('parses a present value with the wrapped schema', () => {
const parsed = zz.optional(z.string()).safeParse('a value')
expect(parsed.success).toBe(true)
expect(parsed.data).toBe('a value')
})
it('normalises null to undefined', () => {
const parsed = zz.optional(z.string()).safeParse(null)
expect(parsed.success).toBe(true)
expect(parsed.data).toBe(undefined)
})
it('normalises undefined to undefined', () => {
const parsed = zz.optional(z.string()).safeParse(undefined)
expect(parsed.success).toBe(true)
expect(parsed.data).toBe(undefined)
})
it('leaves a null unparsed by the wrapped schema', () => {
// A coercing schema would otherwise turn null into a value of its own,
// such as the 0 that Number(null) gives.
const parsed = zz.optional(z.coerce.number()).safeParse(null)
expect(parsed.success).toBe(true)
expect(parsed.data).toBe(undefined)
})
it('reports a present value the wrapped schema rejects', () => {
const parsed = zz.optional(z.string()).safeParse(7)
expect(parsed.success).toBe(false)
})
})
describe('objectId', () => {
it('fails to parse when provided with an invalid ObjectId', () => {
const parsed = zz.objectId().safeParse('aa')
+17
View File
@@ -21,6 +21,23 @@ const datetimeSchema = ({ allowNull, allowUndefined, ...zodOptions } = {}) => {
}
const zz = {
/**
* A field whose absence its source represents inconsistently: left out of
* some records and null in others. JSON has no undefined, and Mongo stores a
* $set of undefined as null, so an absent-ish field arrives as null about as
* often as it is missing outright.
*
* Unlike z.optional(), which accepts only undefined, both forms are accepted
* and normalised to undefined, so callers have a single absent value to
* check rather than a `T | null | undefined` to narrow.
*
* The one combinator here: it wraps another schema instead of describing a
* value type of its own.
*
* @template Output
* @param {z.ZodType<Output>} schema
*/
optional: schema => schema.nullish().transform(value => value ?? undefined),
objectId: () =>
z.string().refine(ObjectId.isValid, { message: 'invalid Mongo ObjectId' }),
coercedObjectId: () =>