Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | 'use client'; /** * Response Validation Component * * Displays validation results for API responses against documented schemas, * with mismatch highlighting and coverage metrics. */ import React, { useMemo, useState } from 'react'; import { ApiEndpoint } from '@/types/api-docs'; import { validateResponse, getValidationSummary, ValidationIssue } from '@/lib/api-docs/validator'; import { Icon } from '@/components/ui/icons'; interface ResponseValidationProps { endpoint: ApiEndpoint; status: number; responseBody: unknown; } export default function ResponseValidation({ endpoint, status, responseBody}: ResponseValidationProps) { const [expanded, setExpanded] = useState(false); const validationResult = useMemo( () => validateResponse(endpoint, status, responseBody), [endpoint, status, responseBody] ); const summary = useMemo( () => getValidationSummary(validationResult), [validationResult] ); return ( <div className="border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden"> {/* Summary Header */} <button onClick={() => setExpanded(!expanded)} className={`w-full flex items-center justify-between p-3 transition-colors ${ summary.status === 'error' ? 'bg-red-50 dark:bg-red-900/20 hover:bg-red-100 dark:hover:bg-red-900/30' : summary.status === 'warning' ? 'bg-yellow-50 dark:bg-yellow-900/20 hover:bg-yellow-100 dark:hover:bg-yellow-900/30' : 'bg-green-50 dark:bg-green-900/20 hover:bg-green-100 dark:hover:bg-green-900/30' }`} > <div className="flex items-center gap-3"> {summary.status === 'error' ? ( <Icon name="alert-circle" size={20} className="text-red-600 dark:text-red-400" /> ) : summary.status === 'warning' ? ( <Icon name="alert-triangle" size={20} className="text-yellow-600 dark:text-yellow-400" /> ) : ( <Icon name="check-circle" size={20} className="text-green-600 dark:text-green-400" /> )} <span className={`font-medium ${ summary.status === 'error' ? 'text-red-800 dark:text-red-200' : summary.status === 'warning' ? 'text-yellow-800 dark:text-yellow-200' : 'text-green-800 dark:text-green-200' }`} > Schema Validation: {summary.message} </span> </div> <div className="flex items-center gap-4"> <CoverageBar coverage={validationResult.coverage.percentage} /> <Icon name={expanded ? 'chevron-up' : 'chevron-down'} size={16} className="text-gray-500 dark:text-gray-400" /> </div> </button> {/* Expanded Details */} {expanded && ( <div className="p-4 bg-white dark:bg-gray-800 border-t border-gray-200 dark:border-gray-700"> {/* Coverage Stats */} <div className="grid grid-cols-4 gap-4 mb-4"> <StatCard label="Documented Fields" value={validationResult.coverage.documentedFields} /> <StatCard label="Response Fields" value={validationResult.coverage.responseFields} /> <StatCard label="Matched Fields" value={validationResult.coverage.matchedFields} /> <StatCard label="Coverage" value={`${validationResult.coverage.percentage}%`} highlight={validationResult.coverage.percentage >= 80} /> </div> {/* Issues List */} {validationResult.issues.length > 0 ? ( <div className="space-y-2"> <h4 className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Issues ({validationResult.issues.length}) </h4> {validationResult.issues.map((issue, index) => ( <IssueItem key={index} issue={issue} /> ))} </div> ) : ( <div className="text-center py-4 text-gray-500 dark:text-gray-400"> <Icon name="check" size={24} className="mx-auto mb-2" /> <p>No validation issues found</p> </div> )} {/* Matched Schema Info */} {validationResult.matchedSchema && ( <div className="mt-4 pt-4 border-t border-gray-200 dark:border-gray-700"> <h4 className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Matched Schema </h4> <div className="flex items-center gap-2"> <span className={`px-2 py-1 text-sm font-bold rounded ${ status >= 200 && status < 300 ? 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-300' : status >= 400 ? 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-300' : 'bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-300' }`} > {validationResult.matchedSchema.status} </span> <span className="text-sm text-gray-600 dark:text-gray-400"> {validationResult.matchedSchema.description} </span> </div> </div> )} </div> )} </div> ); } /** * Coverage progress bar */ function CoverageBar({ coverage }: { coverage: number }) { const getColor = () => { if (coverage >= 80) return 'bg-green-500'; if (coverage >= 50) return 'bg-yellow-500'; return 'bg-red-500'; }; return ( <div className="flex items-center gap-2"> <div className="w-24 h-2 bg-gray-200 dark:bg-gray-600 rounded-full overflow-hidden"> <div className={`h-full ${getColor()} transition-all`} style={{ width: `${coverage}%` }} /> </div> <span className="text-xs text-gray-600 dark:text-gray-400 w-10"> {coverage}% </span> </div> ); } /** * Statistics card */ function StatCard({ label, value, highlight = false}: { label: string; value: number | string; highlight?: boolean; }) { return ( <div className={`p-3 rounded-lg ${ highlight ? 'bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800' : 'bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700' }`} > <div className={`text-lg font-bold ${ highlight ? 'text-green-700 dark:text-green-300' : 'text-gray-900 dark:text-gray-100' }`} > {value} </div> <div className="text-xs text-gray-500 dark:text-gray-400">{label}</div> </div> ); } /** * Individual issue item */ function IssueItem({ issue }: { issue: ValidationIssue }) { const getIcon = () => { switch (issue.severity) { case 'error': return ( <Icon name="x-circle" size={16} className="text-red-500 dark:text-red-400" /> ); case 'warning': return ( <Icon name="alert-triangle" size={16} className="text-yellow-500 dark:text-yellow-400" /> ); default: return ( <Icon name="info-circle" size={16} className="text-blue-500 dark:text-blue-400" /> ); } }; const getBgColor = () => { switch (issue.severity) { case 'error': return 'bg-red-50 dark:bg-red-900/10 border-red-200 dark:border-red-800'; case 'warning': return 'bg-yellow-50 dark:bg-yellow-900/10 border-yellow-200 dark:border-yellow-800'; default: return 'bg-blue-50 dark:bg-blue-900/10 border-blue-200 dark:border-blue-800'; } }; return ( <div className={`p-3 rounded border ${getBgColor()}`}> <div className="flex items-start gap-2"> <div className="mt-0.5">{getIcon()}</div> <div className="flex-1"> <div className="flex items-center gap-2"> {issue.path && ( <code className="text-xs bg-gray-200 dark:bg-gray-700 px-1.5 py-0.5 rounded text-gray-800 dark:text-gray-200"> {issue.path} </code> )} <span className="text-sm text-gray-800 dark:text-gray-200"> {issue.message} </span> </div> {(issue.expected || issue.actual) && ( <div className="mt-2 text-xs space-y-1"> {issue.expected && ( <div className="flex items-center gap-2"> <span className="text-gray-500 dark:text-gray-400">Expected:</span> <code className="text-green-700 dark:text-green-400 bg-green-50 dark:bg-green-900/20 px-1 rounded"> {issue.expected} </code> </div> )} {issue.actual && ( <div className="flex items-center gap-2"> <span className="text-gray-500 dark:text-gray-400">Actual:</span> <code className="text-red-700 dark:text-red-400 bg-red-50 dark:bg-red-900/20 px-1 rounded"> {issue.actual} </code> </div> )} </div> )} </div> </div> </div> ); } export { ResponseValidation }; |