@@ -5,6 +5,7 @@ import { NodeValidationPipeline, NodeValidationPipelineImpl } from "@lib/validat
5
5
import { validateAttributes } from "@lib/validator/pipeline/steps/attributes" ;
6
6
import { validateConstraints } from "@lib/validator/pipeline/steps/constraints" ;
7
7
import { validateOccurrence } from "@lib/validator/pipeline/steps/occurence" ;
8
+ import { validateRequiredChildren } from "@lib/validator/pipeline/steps/requiredChildren" ;
8
9
import { validateType } from "@lib/validator/pipeline/steps/type" ;
9
10
import { XMLParser } from "@lib/xml/parser" ;
10
11
import { XSDParser } from "@lib/xsd/parser" ;
@@ -25,7 +26,8 @@ export class ValidatorImpl implements Validator {
25
26
this . nodePipeline = new NodeValidationPipelineImpl ( )
26
27
. addStep ( validateType )
27
28
. addStep ( validateAttributes )
28
- . addStep ( validateConstraints ) ;
29
+ . addStep ( validateConstraints )
30
+ . addStep ( validateRequiredChildren ) ;
29
31
30
32
// Global pipeline (occurrence checks, etc.)
31
33
this . globalPipeline = new GlobalValidationPipelineImpl ( )
@@ -48,28 +50,35 @@ export class ValidatorImpl implements Validator {
48
50
49
51
private validateNode ( node : Element , schemaElement : XSDElement ) : string [ ] {
50
52
const errors : string [ ] = [ ] ;
51
-
53
+
52
54
// Node-level checks
53
55
errors . push ( ...this . nodePipeline . execute ( node , schemaElement ) ) ;
54
-
56
+
55
57
// If choices exist, validate them
56
58
if ( schemaElement . choices && schemaElement . choices . length > 0 ) {
57
59
// For simplicity, assume 1 XSDChoice
58
60
const [ choiceDef ] = schemaElement . choices ;
59
61
errors . push ( ...this . validateChoice ( node , choiceDef ) ) ;
60
62
}
61
-
62
- // Recursively validate normal children
63
+
64
+ // Recursively validate direct children only
63
65
const childrenErrors = ( schemaElement . children || [ ] ) . flatMap ( ( childSchema ) => {
64
- const childNodes = Array . from ( node . getElementsByTagName ( childSchema . name ) ) ;
66
+ const childNodes = (
67
+ node . children
68
+ ? Array . from ( node . children )
69
+ : Array . from ( node . childNodes ) . filter ( ( child ) : child is Element => child . nodeType === 1 )
70
+ ) . filter ( child => child . tagName === childSchema . name ) ;
71
+
65
72
return [
66
- ...this . globalPipeline . execute ( childNodes , childSchema ) ,
67
- ...childNodes . flatMap ( ( childNode ) => this . validateNode ( childNode , childSchema ) ) ,
73
+ ...this . globalPipeline . execute ( childNodes , childSchema ) , // This throws an error: Argument of type 'ChildNode[]' is not assignable to parameter of type 'Element[]'.
74
+ ...childNodes . flatMap ( ( childNode ) => this . validateNode ( childNode , childSchema ) ) , // This throws an error: Argument of type 'ChildNode' is not assignable to parameter of type 'Element'.
68
75
] ;
69
76
} ) ;
70
-
77
+
71
78
return [ ...errors , ...childrenErrors ] ;
72
79
}
80
+
81
+
73
82
74
83
75
84
private validateChoice ( node : Element , choice : XSDChoice ) : string [ ] {
@@ -89,10 +98,6 @@ export class ValidatorImpl implements Validator {
89
98
] ;
90
99
}
91
100
92
-
93
- /**
94
- * Main entry point: parses XSD, parses XML, and runs pipelines.
95
- */
96
101
async validate ( xml : string , xsd : string ) : Promise < ValidationResult > {
97
102
const schema : XSDSchema = await this . xsdParser . parse ( xsd ) ;
98
103
const xmlDoc = this . xmlParser . parse ( xml ) ;
0 commit comments