mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 13:13:45 +01:00
Merge pull request #36842 from overleaf/em-tpds-typescript
Convert third-party-datastore to TypeScript GitOrigin-RevId: 028172063d0cfe03c0f105f48ab66020c79ea50e
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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: () =>
|
||||
|
||||
Reference in New Issue
Block a user