MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / seeResponseMatchesJsonSchema

Method seeResponseMatchesJsonSchema

lib/helper/JSONResponse.js:323–338  ·  view source on GitHub ↗

* Validates JSON structure of response using [Zod library](https://zod.dev). * See [Zod API](https://zod.dev/) for complete reference on usage. * * Use pre-initialized Zod instance by passing function callback: * * ```js * // response.data is { name: 'jon', id: 1 } * * I.seeR

(fnOrSchema)

Source from the content-addressed store, hash-verified

321 * @param {any} fnOrSchema
322 */
323 seeResponseMatchesJsonSchema(fnOrSchema) {
324 this._checkResponseReady()
325 let schema = fnOrSchema
326 if (typeof fnOrSchema === 'function') {
327 schema = fnOrSchema(z)
328 const body = fnOrSchema.toString()
329 fnOrSchema.toString = () => `${body.split('\n')[1]}...`
330 }
331 if (!schema) throw new Error('Empty Zod schema provided, see https://zod.dev/ for details')
332 if (!(schema instanceof z.ZodType)) throw new Error('Invalid Zod schema provided, see https://zod.dev/ for details')
333 schema.toString = () => schema._def.description || JSON.stringify(schema._def)
334 const result = schema.parse(this.response.data)
335 if (!result) {
336 throw new Error('Schema validation failed')
337 }
338 }
339
340 _checkResponseReady() {
341 if (!this.response) throw new Error('Response is not available')

Callers 1

Calls 2

_checkResponseReadyMethod · 0.95
toStringMethod · 0.45

Tested by

no test coverage detected