Add build trigger for validation-tools

GitOrigin-RevId: 04299d9ab23c65aa791acecd1c0e63b70df9a8d1
This commit is contained in:
Andrew Rumble
2025-09-19 08:05:18 +00:00
committed by Copybot
parent b30740e71c
commit 7962206e22
11 changed files with 270 additions and 0 deletions
@@ -0,0 +1,85 @@
import { validateReq } from '../../../validateReq'
import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import type { Request } from 'express'
import { zz } from '../../../zodHelpers'
describe('validateReq', () => {
describe('with a request that is valid for the schema', () => {
it('should return the parsed request', () => {
const req = {
params: {
id: '507f1f77bcf86cd799439011',
},
body: {
name: 'Valid Name',
},
} as Request<{ id: string }, any, { name: string }>
const schema = z.object({
params: z.object({
id: zz.objectId(),
}),
body: z.object({
name: z.string(),
}),
})
const result = validateReq(req, schema)
expect(result).toEqual({
params: {
id: '507f1f77bcf86cd799439011',
},
body: {
name: 'Valid Name',
},
})
})
})
describe('with a request that is not valid for the schema', () => {
it('should throw NotFoundError if params are invalid', () => {
const req = {
params: {
id: 'invalid-object-id',
},
} as Request<{ id: string }>
expect(() =>
validateReq(
req,
z.object({
params: z.object({
id: zz.objectId(),
}),
})
)
).toThrowError(expect.objectContaining({ name: 'ParamsError' }))
})
it('should throw an error containing issues if the schema is invalid', () => {
const req = {
body: {
name: 1234,
},
} as Request
expect(() =>
validateReq(
req,
z.object({
body: z.object({
name: z.string(),
}),
})
)
).toThrowError(
expect.objectContaining({
issues: expect.arrayContaining([
expect.objectContaining({ path: ['body', 'name'] }),
]),
})
)
})
})
})
@@ -0,0 +1,44 @@
import { zz } from '../../../zodHelpers'
import { describe, expect, it } from 'vitest'
import mongodb from 'mongodb'
const { ObjectId } = mongodb
describe('zodHelpers', () => {
describe('objectId', () => {
it('fails to parse when provided with an invalid ObjectId', () => {
const parsed = zz.objectId().safeParse('aa')
expect(parsed.success).toBe(false)
expect(parsed.error?.issues).toHaveLength(1)
expect(parsed.error?.issues).toMatchObject([
expect.objectContaining({
message: 'invalid Mongo ObjectId',
}),
])
})
it('parses successfully when provided with a valid ObjectId', () => {
const parsed = zz.objectId().safeParse('507f1f77bcf86cd799439011')
expect(parsed.success).toBe(true)
expect(parsed.data).toBe('507f1f77bcf86cd799439011')
})
})
describe('coercedObjectId', () => {
it('fails to parse when provided with an invalid ObjectId', () => {
const parsed = zz.coercedObjectId().safeParse('aa')
expect(parsed.success).toBe(false)
expect(parsed.error?.issues).toHaveLength(1)
expect(parsed.error?.issues).toMatchObject([
expect.objectContaining({
message: 'invalid Mongo ObjectId',
}),
])
})
it('parses to an ObjectId when provided with a valid ObjectId string', () => {
const parsed = zz.coercedObjectId().safeParse('507f1f77bcf86cd799439011')
expect(parsed.success).toBe(true)
expect(parsed.data).toBeInstanceOf(ObjectId)
expect(parsed.data?.toString()).toBe('507f1f77bcf86cd799439011')
})
})
})