gts-validator 0.9.0

GTS identifier validator for documentation and configuration files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Markdown file scanner for GTS identifiers.
//!
//! Uses a two-stage approach:
//! 1. Discovery regex finds candidates
//! 2. `normalize_candidate()` → `validate_candidate()` validates them

use std::collections::HashSet;
use std::path::Path;
use std::sync::LazyLock;

use regex::Regex;

use crate::error::ValidationError;
use crate::normalize::normalize_candidate;
use crate::validator::{is_bad_example_context, is_wildcard_context, validate_candidate};

/// Markdown parsing state for code block tracking
#[derive(Debug, Clone, PartialEq, Eq)]
enum MarkdownState {
    Prose,
    FencedBlock {
        skip: bool,
        fence_char: char,
        opening_fence_len: usize,
    },
}

fn parse_fence(trimmed_line: &str) -> Option<(char, usize)> {
    let fence_char = match trimmed_line.as_bytes().first() {
        Some(b'`') => '`',
        Some(b'~') => '~',
        _ => return None,
    };

    let fence_len = trimmed_line
        .chars()
        .take_while(|&c| c == fence_char)
        .count();
    if fence_len >= 3 {
        Some((fence_char, fence_len))
    } else {
        None
    }
}

/// Discovery regex (relaxed): finds strings that LOOK like GTS identifiers.
/// This is intentionally broader than the spec — validation is done by `GtsID::new()`.
///
/// Strategy: Match gts. followed by 4+ dot-separated segments where at least one
/// segment looks like a version (starts with 'v' followed by digit).
/// This catches both valid and malformed IDs for validation (more errors reported).
/// Stops at tilde followed by non-alphanumeric to avoid matching filenames like "id.v1~.schema.json"
static GTS_DISCOVERY_PATTERN_RELAXED: LazyLock<Regex> = LazyLock::new(|| {
    match Regex::new(concat!(
        r"(?:gts://)?",                    // optional URI prefix
        r"\bgts\.", // mandatory gts. prefix (word boundary prevents xgts. match)
        r"(?:[a-z_*][a-z0-9_*.-]*\.){3,}", // at least 3 segments (permissive: allows -, .)
        r"[a-z_*][a-z0-9_*.-]*", // final segment before version
        r"\.v[0-9]+", // version segment (required anchor)
        r"(?:\.[0-9]+)?", // optional minor version
        r"(?:~[a-z_][a-z0-9_.-]*)*", // optional chained segments (permissive)
        r"~?",      // optional trailing tilde (but not if followed by .)
    )) {
        Ok(regex) => regex,
        Err(err) => panic!("Invalid discovery regex: {err}"),
    }
});

/// Discovery regex (well-formed): only matches well-formed GTS identifiers.
/// Requires exactly 5 segments with proper structure (fewer errors reported).
static GTS_DISCOVERY_PATTERN_WELL_FORMED: LazyLock<Regex> = LazyLock::new(|| {
    match Regex::new(concat!(
        r"(?:gts://)?",          // optional URI prefix
        r"\bgts\.",              // mandatory gts. prefix (word boundary prevents xgts. match)
        r"[a-z_*][a-z0-9_*]*\.", // vendor
        r"[a-z_*][a-z0-9_*]*\.", // package
        r"[a-z_*][a-z0-9_*]*\.", // namespace
        r"[a-z_*][a-z0-9_*]*\.", // type
        r"v[0-9]+",              // major version (required)
        r"(?:\.[0-9]+)?",        // optional minor version
        r"(?:~[a-z_][a-z0-9_]*\.[a-z_][a-z0-9_]*\.[a-z_][a-z0-9_]*\.[a-z_][a-z0-9_]*\.v[0-9]+(?:\.[0-9]+)?)*", // chained segments
        r"~?", // optional trailing tilde
    )) {
        Ok(regex) => regex,
        Err(err) => panic!("Invalid discovery regex: {err}"),
    }
});

/// Scan markdown content for GTS identifiers.
pub fn scan_markdown_content(
    content: &str,
    path: &Path,
    vendor: Option<&str>,
    heuristic: bool,
    skip_tokens: &[String],
) -> Vec<ValidationError> {
    let pattern = if heuristic {
        &*GTS_DISCOVERY_PATTERN_RELAXED
    } else {
        &*GTS_DISCOVERY_PATTERN_WELL_FORMED
    };
    let mut errors = Vec::new();
    let mut state = MarkdownState::Prose;
    let mut seen_candidates: HashSet<(usize, String)> = HashSet::new();

    for (line_num, line) in content.lines().enumerate() {
        let line_number = line_num + 1; // 1-indexed

        // Update markdown state for code blocks (``` and ~~~ per CommonMark spec)
        let trimmed_line = line.trim_start();
        if let Some((fence_char, fence_len)) = parse_fence(trimmed_line) {
            match &state {
                MarkdownState::Prose => {
                    // Entering a fenced block
                    let language = trimmed_line[fence_len..].trim().to_lowercase();

                    // Skip grammar/pattern definition blocks
                    let skip = matches!(
                        language.as_str(),
                        "ebnf" | "regex" | "bnf" | "abnf" | "grammar"
                    );

                    state = MarkdownState::FencedBlock {
                        skip,
                        fence_char,
                        opening_fence_len: fence_len,
                    };
                    continue;
                }
                MarkdownState::FencedBlock {
                    fence_char: open_fence_char,
                    opening_fence_len,
                    ..
                } => {
                    // Exiting a fenced block requires matching delimiter with sufficient length.
                    if fence_char == *open_fence_char && fence_len >= *opening_fence_len {
                        state = MarkdownState::Prose;
                        continue;
                    }
                }
            }
        }

        // Skip lines inside skip blocks
        if let MarkdownState::FencedBlock { skip: true, .. } = state {
            continue;
        }

        // Find all GTS candidates on this line
        for mat in pattern.find_iter(line) {
            let candidate_str = mat.as_str();
            let match_start = mat.start();

            // Deduplicate: skip if we've seen this candidate on this line
            if !seen_candidates.insert((line_number, candidate_str.to_owned())) {
                continue;
            }

            // Skip validation if this is a "bad example" context
            if is_bad_example_context(line, mat.start()) {
                continue;
            }

            // Check consumer-provided skip tokens
            if !skip_tokens.is_empty()
                && let Some(before) = line.get(..mat.start())
            {
                let before_lower = before.to_lowercase();
                if skip_tokens
                    .iter()
                    .any(|token| before_lower.contains(&token.to_lowercase()))
                {
                    continue;
                }
            }

            // Normalize the candidate
            let candidate = match normalize_candidate(candidate_str) {
                Ok(c) => c,
                Err(e) => {
                    errors.push(ValidationError {
                        file: path.to_owned(),
                        line: line_number,
                        column: match_start + 1, // 1-indexed
                        json_path: String::new(),
                        raw_value: candidate_str.to_owned(),
                        normalized_id: String::new(),
                        error: e,
                        context: line.to_owned(),
                    });
                    continue;
                }
            };

            // Check if wildcards are allowed in this context
            let allow_wildcards = is_wildcard_context(line, match_start);

            // Validate the candidate
            let validation_errors = validate_candidate(&candidate, vendor, allow_wildcards);
            for err in validation_errors {
                errors.push(ValidationError {
                    file: path.to_owned(),
                    line: line_number,
                    column: match_start + 1, // 1-indexed
                    json_path: String::new(),
                    raw_value: candidate.original.clone(),
                    normalized_id: candidate.gts_id.clone(),
                    error: err,
                    context: line.to_owned(),
                });
            }
        }
    }

    errors
}

/// Scan a markdown file for GTS identifiers (file-based convenience wrapper).
#[cfg(test)]
pub fn scan_markdown_file(
    path: &Path,
    vendor: Option<&str>,
    max_file_size: u64,
    heuristic: bool,
) -> Vec<ValidationError> {
    // Check file size
    if let Ok(metadata) = std::fs::metadata(path)
        && metadata.len() > max_file_size
    {
        return vec![];
    }

    // Read as UTF-8; skip file on encoding error
    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(_e) => return vec![],
    };

    scan_markdown_content(&content, path, vendor, heuristic, &[])
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn create_temp_md(content: &str) -> NamedTempFile {
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        file
    }

    #[test]
    fn test_scan_markdown_valid_id() {
        let file = create_temp_md("The type is gts.x.core.events.type.v1~");
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(errors.is_empty(), "Unexpected errors: {errors:?}");
    }

    #[test]
    fn test_scan_markdown_invalid_id() {
        let file = create_temp_md("The type is gts.x.core.events.type.v1");
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(
            !errors.is_empty(),
            "Single-segment instance ID should be rejected"
        );
    }

    #[test]
    fn test_scan_markdown_skip_ebnf_block() {
        let content = r"
```ebnf
gts.invalid.pattern.here.v1~
```
";
        let file = create_temp_md(content);
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(errors.is_empty(), "EBNF blocks should be skipped");
    }

    #[test]
    fn test_scan_markdown_validate_json_block() {
        let content = r#"
```json
{"$id": "gts://gts.x.core.events.type.v1~"}
```
"#;
        let file = create_temp_md(content);
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(errors.is_empty(), "JSON blocks should be validated");
    }

    #[test]
    fn test_scan_markdown_skip_invalid_context() {
        let file = create_temp_md("\u{274c} gts.invalid.id.here.v1");
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(errors.is_empty(), "Invalid examples should be skipped");
    }

    #[test]
    fn test_scan_markdown_wildcard_in_pattern_context() {
        let file = create_temp_md("pattern: gts.x.core.events.type.v1~");
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(
            errors.is_empty(),
            "Valid IDs in pattern context should be allowed"
        );
    }

    #[test]
    fn test_scan_markdown_wildcard_not_in_pattern_context() {
        let file = create_temp_md("The type is gts.x.core.events.type.v1~");
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(errors.is_empty(), "Valid IDs should pass");
    }

    #[test]
    fn test_scan_markdown_gts_uri() {
        let file = create_temp_md(r#"Use "$id": "gts://gts.x.core.events.type.v1~""#);
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(
            errors.is_empty(),
            "gts:// URIs should be normalized and validated"
        );
    }

    #[test]
    fn test_scan_markdown_vendor_mismatch() {
        let file = create_temp_md("The type is gts.hx.core.events.type.v1~");
        let errors = scan_markdown_file(file.path(), Some("x"), 10_485_760, false);
        assert!(!errors.is_empty());
        assert!(errors[0].error.contains("Vendor mismatch"));
    }

    #[test]
    fn test_scan_markdown_example_vendor_tolerated() {
        let file = create_temp_md("Example: gts.acme.core.events.type.v1~");
        let errors = scan_markdown_file(file.path(), Some("x"), 10_485_760, false);
        assert!(errors.is_empty(), "Example vendors should be tolerated");
    }

    #[test]
    fn test_scan_markdown_deduplication() {
        // Use an invalid ID (wrong vendor) twice on the same line — dedup should produce exactly 1 error
        let file = create_temp_md(
            "gts.wrongvendor.core.events.type.v1~ and gts.wrongvendor.core.events.type.v1~ again",
        );
        let errors = scan_markdown_file(file.path(), Some("x"), 10_485_760, false);
        assert_eq!(
            errors.len(),
            1,
            "Duplicate invalid ID on same line should produce exactly 1 error, got: {errors:?}"
        );
    }

    #[test]
    fn test_scan_markdown_error_after_gts_id() {
        let file = create_temp_md("gts.x.core.events.type.v1~ handles error cases");
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(
            errors.is_empty(),
            "Valid ID should not be suppressed by 'error' appearing after it"
        );
    }

    #[test]
    fn test_scan_markdown_invalid_before_gts_id() {
        let file = create_temp_md("invalid: gts.bad.format.here.v1");
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(errors.is_empty(), "Invalid examples should be skipped");
    }

    #[test]
    fn test_scan_markdown_heuristic_mode_catches_malformed() {
        let file = create_temp_md("The type is gts.my-vendor.core.events.type.v1~");
        let errors_heuristic = scan_markdown_file(file.path(), None, 10_485_760, true);
        let errors_normal = scan_markdown_file(file.path(), None, 10_485_760, false);

        assert!(
            !errors_heuristic.is_empty(),
            "Heuristic mode should catch malformed ID with hyphens"
        );
        assert!(
            errors_normal.is_empty(),
            "Normal mode won't match malformed pattern"
        );
    }

    #[test]
    fn test_scan_markdown_heuristic_mode_catches_extra_dots() {
        let file = create_temp_md("The type is gts.x.core.events.type.name.v1~");
        let errors_heuristic = scan_markdown_file(file.path(), None, 10_485_760, true);

        assert!(
            !errors_heuristic.is_empty(),
            "Heuristic mode should catch ID with extra segments"
        );
    }

    #[test]
    fn test_scan_markdown_normal_mode_well_formed_only() {
        let file = create_temp_md("Valid: gts.x.core.events.type.v1~ and malformed: gts.bad-id.v1");
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);

        assert!(
            errors.is_empty(),
            "Normal mode should only validate well-formed patterns"
        );
    }

    #[test]
    fn test_scan_markdown_skip_tokens() {
        // skip_tokens should suppress validation when the token appears before the candidate
        let content = "**given** gts.bad.format.here.v1~";
        let errors = scan_markdown_content(
            content,
            Path::new("test.md"),
            None,
            true, // heuristic mode to ensure the relaxed regex would catch it
            &["**given**".to_owned()],
        );
        assert!(
            errors.is_empty(),
            "skip_tokens should suppress validation: {errors:?}"
        );

        // Same content without skip_tokens should produce errors (vendor mismatch)
        let content_mismatch = "**given** gts.y.core.pkg.mytype.v1~ is registered";
        let errors_no_skip = scan_markdown_content(
            content_mismatch,
            Path::new("test.md"),
            Some("x"),
            false,
            &[],
        );
        assert!(
            !errors_no_skip.is_empty(),
            "Without skip_tokens, vendor mismatch should be reported"
        );

        // With skip_tokens, the same content should be suppressed
        let errors_with_skip = scan_markdown_content(
            content_mismatch,
            Path::new("test.md"),
            Some("x"),
            false,
            &["**given**".to_owned()],
        );
        assert!(
            errors_with_skip.is_empty(),
            "With skip_tokens, vendor mismatch should be suppressed: {errors_with_skip:?}"
        );
    }

    #[test]
    fn test_scan_markdown_tilde_fence() {
        // ~~~ fences should be handled the same as ``` fences
        let content = "~~~ebnf\ngts.invalid.pattern.here.v1~\n~~~\n";
        let file = create_temp_md(content);
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(
            errors.is_empty(),
            "~~~ EBNF blocks should be skipped: {errors:?}"
        );
    }

    #[test]
    fn test_scan_markdown_tilde_fence_json_validated() {
        // ~~~json blocks should be validated (same as ```json)
        let content = "~~~json\n{\"$id\": \"gts://gts.x.core.events.type.v1~\"}\n~~~\n";
        let file = create_temp_md(content);
        let errors = scan_markdown_file(file.path(), None, 10_485_760, false);
        assert!(
            errors.is_empty(),
            "~~~json blocks should be validated and pass: {errors:?}"
        );
    }

    #[test]
    fn test_scan_markdown_mismatched_fence_does_not_close_block() {
        // A ~~~ line must not close a ``` block.
        let content = "```ebnf\n~~~\ngts.bad.format.here.v1~\n```\n";
        let file = create_temp_md(content);
        let errors = scan_markdown_file(file.path(), None, 10_485_760, true);
        assert!(
            errors.is_empty(),
            "Mismatched fence should not close block; content inside ebnf block must be skipped: {errors:?}"
        );
    }

    #[test]
    fn test_scan_markdown_word_boundary() {
        // Regex should NOT match "xgts.x.core.events.type.v1~" (no word boundary)
        let content = "The identifier xgts.x.core.events.type.v1~ is wrong";
        let errors = scan_markdown_content(content, Path::new("test.md"), None, false, &[]);
        assert!(
            errors.is_empty(),
            "Word boundary should prevent matching xgts.*: {errors:?}"
        );
    }
}