- 1. Purpose
- 2. When Validation Occurs
- 3. Supported Formats
- 4. Error Reporting
- 5. Format-Specific Validation
- 6. ValidationError Details
- 7. Error Context
- 8. Handling Validation Errors
- 9. Validation Performance
- 10. Pre-validation
- 11. Common Validation Issues
- 12. Validation vs Comparison
- 13. Configuration
- 14. Debugging Validation Failures
- 15. See Also
1. Purpose
Canon validates all input before performing comparisons to ensure you’re working with well-formed data. Input validation catches syntax errors early and provides clear, actionable error messages with line and column information.
2. When Validation Occurs
Canon validates input at the beginning of any comparison operation, before preprocessing or diff generation:
Input → Validation → Preprocessing → Comparison → Diff Output
↑
Fails here if syntax errors detected
This ensures that:
-
Malformed input is caught early
-
Error messages are clear and specific
-
No time is wasted on invalid data
-
Line numbers in errors match original input
3. Supported Formats
Canon provides validators for all structured formats:
| Format | Validator |
|---|---|
XML |
Nokogiri strict parser with detailed syntax error reporting |
HTML |
Nokogiri HTML parser (lenient for HTML quirks) |
JSON |
Ruby JSON parser with position tracking |
YAML |
Psych YAML parser with error context |
4. Error Reporting
4.1. Error Information
When validation fails, Canon provides:
-
Error message: Clear description of the problem
-
Line number: Where the error occurred
-
Column number: Specific position in the line (when available)
-
Context: Surrounding content to help locate the issue
-
Format: Which format validator detected the error
5. Format-Specific Validation
5.1. XML Validation
XML validation uses Nokogiri’s strict parsing mode.
Detects: * Unclosed tags * Mismatched opening/closing tags * Invalid entity references * Malformed attributes * Invalid character data * Namespace errors
Unclosed tag:
<root>
<item>Value
</root>
Error: Opening and ending tag mismatch: item line 2 and root
Invalid character:
<root>
<item>Value & Stuff</item>
</root>
Error: Entity 'Stuff' not defined
Mismatched tags:
<root>
<item>Value</Item>
</root>
Error: Opening and ending tag mismatch: item line 2 and Item
5.2. HTML Validation
HTML validation is more lenient than XML, as HTML has quirks and browser-compatible handling.
Detects: * Severely malformed structure * Encoding issues * Invalid nesting (in strict mode)
| HTML validation is intentionally lenient to handle real-world HTML that browsers accept. |
5.3. JSON Validation
JSON validation uses Ruby’s JSON parser.
Detects: * Missing commas * Trailing commas * Unquoted keys * Invalid escape sequences * Unclosed strings, arrays, or objects * Invalid Unicode sequences
Missing comma:
{
"name": "John"
"age": 30
}
Error: unexpected token at '"age": 30'
Trailing comma:
{
"name": "John",
"age": 30,
}
Error: unexpected token at '}'
Unquoted key:
{
name: "John"
}
Error: unexpected token
5.4. YAML Validation
YAML validation uses the Psych parser.
Detects: * Invalid indentation * Incorrect list syntax * Malformed block scalars * Invalid anchors/aliases * Encoding issues
Invalid indentation:
root:
item: value
bad_indent: value
Error: mapping values are not allowed in this context
Malformed list:
list:
- item1
-item2
Error: could not find expected ':'
6. ValidationError Details
The Canon::ValidationError exception provides structured error information:
begin
Canon.compare(invalid, valid, format: :xml)
rescue Canon::ValidationError => e
puts "Format: #{e.format}" # :xml
puts "Message: #{e.message}" # Error description
puts "Line: #{e.line}" # Line number
puts "Column: #{e.column}" # Column number
puts "Details: #{e.details}" # Additional context
end
7. Error Context
8. Handling Validation Errors
8.1. In Tests
When validation fails in tests, you get immediate feedback:
RSpec.describe "XML comparison" do
it "compares documents" do
expected = "<root><item>Value</root>" # Missing </item>
actual = "<root><item>Value</item></root>"
expect(actual).to match_xml(expected)
# Fails immediately with:
# Canon::ValidationError: Opening and ending tag mismatch
end
end
9. Validation Performance
10. Pre-validation
11. Common Validation Issues
11.1. XML Issues
| Issue | Example | Solution |
|---|---|---|
Unescaped ampersands |
|
Use |
Mismatched tags |
|
Match case exactly: |
Unclosed tags |
|
Close all tags: |
Invalid namespace |
|
Define namespace: |
12. Validation vs Comparison
Important distinction:
| Validation | Comparison |
|---|---|
Checks syntax |
Checks semantic equivalence |
Ensures well-formed input |
Finds differences in content |
Fails on syntax errors |
Succeeds if semantically equivalent |
Fast (parse only) |
Slower (full comparison) |
Line/column errors |
Semantic difference reports |
13. Configuration
Validation is always enabled and cannot be disabled. This is intentional to:
-
Prevent confusing errors during comparison
-
Ensure data quality
-
Provide clear error messages
-
Catch problems early
14. Debugging Validation Failures
14.1. Identify the Problem Line
Use the line number from the error:
# Show specific line in file
sed -n '15p' file.xml
# Show context around line 15
sed -n '12,18p' file.xml
15. See Also
-
Environment Configuration - Size limits for validation
-
CLI Interface - Command-line error handling
-
Ruby API - Programmatic error handling
-
Understanding Canon - How validation fits in the pipeline