11import Helper from '@codeceptjs/helper'
22import assert from 'assert'
3- import joi from 'joi '
3+ import { z } from 'zod '
44
55/**
66 * This helper allows performing assertions on JSON responses paired with following helpers:
@@ -87,16 +87,7 @@ class JSONResponse extends Helper {
8787 this . response = null
8888 }
8989
90- static _checkRequirements ( ) {
91- try {
92- // In ESM, joi is already imported at the top
93- // The import will fail at module load time if joi is missing
94- return null
95- } catch ( e ) {
96- return [ 'joi' ]
97- }
98- }
99-
90+
10091 /**
10192 * Checks that response code is equal to the provided one
10293 *
@@ -308,28 +299,28 @@ class JSONResponse extends Helper {
308299 }
309300
310301 /**
311- * Validates JSON structure of response using [joi library](https://joi .dev).
312- * See [joi API](https://joi .dev/api /) for complete reference on usage.
302+ * Validates JSON structure of response using [Zod library](https://zod .dev).
303+ * See [Zod API](https://zod .dev/) for complete reference on usage.
313304 *
314- * Use pre-initialized joi instance by passing function callback:
305+ * Use pre-initialized Zod instance by passing function callback:
315306 *
316307 * ```js
317308 * // response.data is { name: 'jon', id: 1 }
318309 *
319- * I.seeResponseMatchesJsonSchema(joi => {
320- * return joi .object({
321- * name: joi .string(),
322- * id: joi .number()
310+ * I.seeResponseMatchesJsonSchema(z => {
311+ * return z .object({
312+ * name: z .string(),
313+ * id: z .number()
323314 * })
324315 * });
325316 *
326317 * // or pass a valid schema
327- * const joi = require('joi') ;
318+ * import { z } from 'zod' ;
328319 *
329- * I.seeResponseMatchesJsonSchema(joi .object({
330- * name: joi .string(),
331- * id: joi .number();
332- * });
320+ * I.seeResponseMatchesJsonSchema(z .object({
321+ * name: z .string(),
322+ * id: z .number()
323+ * })) ;
333324 * ```
334325 *
335326 * @param {any } fnOrSchema
@@ -338,14 +329,17 @@ class JSONResponse extends Helper {
338329 this . _checkResponseReady ( )
339330 let schema = fnOrSchema
340331 if ( typeof fnOrSchema === 'function' ) {
341- schema = fnOrSchema ( joi )
332+ schema = fnOrSchema ( z )
342333 const body = fnOrSchema . toString ( )
343334 fnOrSchema . toString = ( ) => `${ body . split ( '\n' ) [ 1 ] } ...`
344335 }
345- if ( ! schema ) throw new Error ( 'Empty Joi schema provided, see https://joi.dev/ for details' )
346- if ( ! joi . isSchema ( schema ) ) throw new Error ( 'Invalid Joi schema provided, see https://joi.dev/ for details' )
347- schema . toString = ( ) => schema . describe ( )
348- joi . assert ( this . response . data , schema )
336+ if ( ! schema ) throw new Error ( 'Empty Zod schema provided, see https://zod.dev/ for details' )
337+ if ( ! ( schema instanceof z . ZodType ) ) throw new Error ( 'Invalid Zod schema provided, see https://zod.dev/ for details' )
338+ schema . toString = ( ) => schema . _def . description || JSON . stringify ( schema . _def )
339+ const result = schema . parse ( this . response . data )
340+ if ( ! result ) {
341+ throw new Error ( 'Schema validation failed' )
342+ }
349343 }
350344
351345 _checkResponseReady ( ) {
0 commit comments