Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

compare.ts

35 lines1.2 KB

1import type { ProbeExpect } from './types.ts'
2​
3/** Null when the live call still matches the record. */
4export function observationMatches(
5 expect: ProbeExpect,
6 actual: { status: number; payloads: Array<unknown> },
7): string | null {
8 if (actual.status !== expect.status) {
9 return `status ${actual.status}, expected ${expect.status}`
10 }
11 if (actual.payloads.length === 0) return 'response had no JSON object'
12 for (const payload of actual.payloads) {
13 if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
14 return 'response JSON was not an object'
15 }
16 const missing = expect.jsonKeys.filter(
17 (key) => !Object.keys(payload).includes(key),
18 )
19 if (missing.length > 0) return `missing ${missing.join(', ')}`
20 }
21 return null
23​
24/** Replace one record's verifiedAt. Throws when that id is not in the file. */
25export function bumpVerifiedAt(
26 source: string,
27 id: string,
28 date: string,
29): string {
30 const pattern = new RegExp(
31 `(id: '${id}'[\\s\\S]*?verifiedAt: ')\\d{4}-\\d{2}-\\d{2}(')`,
32 )
33 if (!pattern.test(source)) throw new Error(`probe: no verifiedAt for ${id}`)
34 return source.replace(pattern, `$1${date}$2`)