diff --git a/validation_test.go b/validation_test.go index 660edc2..2604252 100644 --- a/validation_test.go +++ b/validation_test.go @@ -299,3 +299,74 @@ func TestMaxOccursValidation(t *testing.T) { }) } } + +func TestEmptyElement(t *testing.T) { + xsdBytes := []byte(` + + + + + + + + +`) + + schema, err := ParseXSD(xsdBytes) + if err != nil { + t.Fatalf("Failed to parse XSD: %v", err) + } + + tests := []struct { + name string + xml string + shouldPass bool + errorCheck func(error) bool + }{ + { + name: "Valid length", + xml: `Test`, + shouldPass: true, + }, + { + name: "Too short", + xml: ``, + shouldPass: false, + errorCheck: func(err error) bool { + return strings.Contains(err.Error(), "too short") + }, + }, + { + name: "Can't be empty", + xml: ``, + shouldPass: false, + errorCheck: func(err error) bool { + return strings.Contains(err.Error(), "too short") + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + doc, err := Parse([]byte(tt.xml)) + if err != nil { + t.Fatalf("Failed to parse XML: %v", err) + } + + validationErr := schema.Validate(doc) + if tt.shouldPass { + if validationErr != nil { + t.Errorf("Expected validation to pass, but got error: %v", validationErr) + } + } else { + if validationErr == nil { + t.Error("Expected validation to fail, but it passed") + } else if tt.errorCheck != nil && !tt.errorCheck(validationErr) { + t.Errorf("Error check failed for: %v", validationErr) + } else { + t.Logf("✓ Length constraint validation working: %v", validationErr) + } + } + }) + } +} diff --git a/validations.go b/validations.go index e350fa9..4c6b689 100644 --- a/validations.go +++ b/validations.go @@ -47,7 +47,7 @@ func (s *Schema) validateNode(node *Node, def *Element) []string { var errors []string // Validate text content for leaf nodes - if len(node.Children) == 0 && strings.TrimSpace(node.Content) != "" { + if len(node.Children) == 0 { errors = append(errors, s.validateTextContent(node, def)...) }