@@ -57,55 +57,3 @@ export function validateResponse<T, S>(
5757 throw error ;
5858 }
5959}
60-
61- /**
62- * Helper function to safely validate and extract specific fields from an API response
63- * Use this when you only need specific fields from a larger response
64- *
65- * @param data The data to validate and extract from
66- * @param schema The Zod schema for extraction
67- * @param context Context for error messages
68- * @param serviceIdentifier Optional service identifier
69- * @returns The extracted data based on the schema
70- */
71- export function extractAndValidate < T , S > (
72- data : unknown ,
73- schema : z . ZodType < T , z . ZodTypeDef , S > ,
74- context : string ,
75- serviceIdentifier ?: string ,
76- ) : T {
77- const methodLogger = logger . forMethod ( 'extractAndValidate' ) ;
78- const logPrefix = serviceIdentifier ? `[${ serviceIdentifier } ] ` : '' ;
79-
80- // Skip validation in test environment
81- if ( skipValidation ) {
82- methodLogger . debug (
83- `${ logPrefix } Skipping extraction validation for ${ context } in test environment` ,
84- ) ;
85- return schema . parse ( data ) ;
86- }
87-
88- try {
89- methodLogger . debug (
90- `${ logPrefix } Extracting and validating fields for ${ context } ` ,
91- ) ;
92- return schema . parse ( data ) ;
93- } catch ( error ) {
94- if ( error instanceof z . ZodError ) {
95- methodLogger . error (
96- `${ logPrefix } Field extraction validation failed for ${ context } :` ,
97- error . format ( ) ,
98- ) ;
99- throw createApiError (
100- `API response field extraction failed: Invalid format for ${ context } ` ,
101- 500 ,
102- { zodErrors : error . format ( ) } ,
103- ) ;
104- }
105- methodLogger . error (
106- `${ logPrefix } Non-Zod error during field extraction for ${ context } :` ,
107- error ,
108- ) ;
109- throw error ;
110- }
111- }
0 commit comments