(schema, positiveTests, negativeTests)
| 501 | * @param {Map<string, unknown>} negativeTests |
| 502 | */ |
| 503 | export function checkPatternCoverage(schema, positiveTests, negativeTests) { |
| 504 | const patterns = walkProperties(schema) |
| 505 | .filter( |
| 506 | (p) => typeof p.propSchema.pattern === 'string' && p.propSchema.pattern, |
| 507 | ) |
| 508 | .map((p) => ({ |
| 509 | path: p.path, |
| 510 | name: p.name, |
| 511 | pattern: /** @type {string} */ (p.propSchema.pattern), |
| 512 | })) |
| 513 | |
| 514 | if (patterns.length === 0) { |
| 515 | return { status: 'skip', reason: 'No pattern constraints' } |
| 516 | } |
| 517 | |
| 518 | const issues = [] |
| 519 | for (const { path: pPath, name, pattern } of patterns) { |
| 520 | let regex |
| 521 | try { |
| 522 | regex = new RegExp(pattern) |
| 523 | } catch { |
| 524 | issues.push({ path: pPath, type: 'invalid_regex', pattern }) |
| 525 | continue |
| 526 | } |
| 527 | |
| 528 | // Positive: at least one value matches (use path-aware collection) |
| 529 | let hasMatch = false |
| 530 | const testedPosFiles = [] |
| 531 | for (const [fname, data] of positiveTests) { |
| 532 | const vals = collectValuesByPath(data, pPath) |
| 533 | if (vals.length > 0) { |
| 534 | testedPosFiles.push(fname) |
| 535 | for (const v of vals) { |
| 536 | if (typeof v === 'string' && regex.test(v)) { |
| 537 | hasMatch = true |
| 538 | break |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | if (hasMatch) break |
| 543 | } |
| 544 | if (!hasMatch) { |
| 545 | issues.push({ |
| 546 | path: pPath, |
| 547 | type: 'no_positive_match', |
| 548 | pattern, |
| 549 | testedFiles: [...new Set(testedPosFiles)], |
| 550 | }) |
| 551 | } |
| 552 | |
| 553 | // Negative: at least one value violates |
| 554 | let hasViolation = false |
| 555 | for (const data of negativeTests.values()) { |
| 556 | for (const v of collectValuesByPath(data, pPath)) { |
| 557 | if (typeof v === 'string' && !regex.test(v)) { |
| 558 | hasViolation = true |
| 559 | break |
| 560 | } |
no test coverage detected