1
1
import { kebabToCamelCase } from './string_utils' ;
2
2
import { allowedTopLevelFields , sectionChecks , listTypes , stringTypes , directoryTypes } from './context_structure' ;
3
3
4
+ export interface SectionValidationResult {
5
+ isValid : boolean ;
6
+ coveragePercentage : number ;
7
+ coveredFields : number ;
8
+ totalFields : number ;
9
+ missingFields : string [ ] ;
10
+ }
11
+
4
12
export interface ValidationResult {
5
13
isValid : boolean ;
6
14
coveragePercentage : number ;
15
+ coveredFields : number ;
16
+ totalFields : number ;
7
17
missingFields : string [ ] ;
18
+ sections : Record < string , SectionValidationResult > ;
8
19
}
9
20
10
21
export class ContextValidator {
@@ -19,6 +30,7 @@ export class ContextValidator {
19
30
const coveredFields = new Set < string > ( ) ;
20
31
let isValid = true ;
21
32
const allMissingFields : string [ ] = [ ] ;
33
+ const sections : Record < string , SectionValidationResult > = { } ;
22
34
23
35
for ( const [ field , value ] of Object . entries ( data ) ) {
24
36
const normalizedField = isJson ? kebabToCamelCase ( field , this . kebabToCamelCache ) : field ;
@@ -34,20 +46,24 @@ export class ContextValidator {
34
46
35
47
const { coveragePercentage, missingFields } = this . calculateCoverage ( coveredFields , allowedTopLevelFields ) ;
36
48
allMissingFields . push ( ...missingFields ) ;
37
- console . log ( ` Context coverage: ${ coveragePercentage . toFixed ( 2 ) } % (${ coveredFields . size } /${ allowedTopLevelFields . size } fields)` ) ;
38
- if ( missingFields . length > 0 ) {
39
- console . log ( ` Missing top-level fields: ${ missingFields . join ( ', ' ) } ` ) ;
40
- }
41
49
42
50
for ( const section of Object . keys ( sectionChecks ) ) {
43
51
if ( section in data ) {
44
52
const sectionResult = this . validateSectionFields ( section , data [ section ] as Record < string , unknown > , isJson ) ;
45
53
isValid = sectionResult . isValid && isValid ;
46
54
allMissingFields . push ( ...sectionResult . missingFields ) ;
55
+ sections [ section ] = sectionResult ;
47
56
}
48
57
}
49
58
50
- return { isValid, coveragePercentage, missingFields : allMissingFields } ;
59
+ return {
60
+ isValid,
61
+ coveragePercentage,
62
+ coveredFields : coveredFields . size ,
63
+ totalFields : allowedTopLevelFields . size ,
64
+ missingFields : allMissingFields ,
65
+ sections
66
+ } ;
51
67
}
52
68
53
69
private validateField ( field : string , value : unknown , isJson : boolean ) : boolean {
@@ -64,7 +80,7 @@ export class ContextValidator {
64
80
return isValid ;
65
81
}
66
82
67
- private validateSectionFields ( sectionName : string , data : Record < string , unknown > , isJson : boolean ) : ValidationResult {
83
+ private validateSectionFields ( sectionName : string , data : Record < string , unknown > , isJson : boolean ) : SectionValidationResult {
68
84
const checks = sectionChecks [ sectionName ] ;
69
85
const coveredFields = new Set < string > ( ) ;
70
86
let isValid = true ;
@@ -83,15 +99,23 @@ export class ContextValidator {
83
99
}
84
100
85
101
const { coveragePercentage, missingFields } = this . calculateCoverage ( coveredFields , checks ) ;
86
- console . log ( ` ${ sectionName } coverage: ${ coveragePercentage . toFixed ( 2 ) } % (${ coveredFields . size } /${ checks . size } fields)` ) ;
87
- if ( missingFields . length > 0 ) {
88
- console . log ( ` Missing fields in '${ sectionName } ' section: ${ missingFields . join ( ', ' ) } ` ) ;
89
- }
90
102
91
- return { isValid, coveragePercentage, missingFields } ;
103
+ return {
104
+ isValid,
105
+ coveragePercentage,
106
+ coveredFields : coveredFields . size ,
107
+ totalFields : checks . size ,
108
+ missingFields
109
+ } ;
92
110
}
93
111
94
- return { isValid : true , coveragePercentage : 100 , missingFields : [ ] } ;
112
+ return {
113
+ isValid : true ,
114
+ coveragePercentage : 100 ,
115
+ coveredFields : 0 ,
116
+ totalFields : 0 ,
117
+ missingFields : [ ]
118
+ } ;
95
119
}
96
120
97
121
private calculateCoverage ( coveredFields : Set < string > , expectedFields : Set < string > ) : { coveragePercentage : number , missingFields : string [ ] } {
0 commit comments