Repository navigation
Expand file tree
/
Copy pathvalidate-patterns.js
More file actions
950 lines (818 loc) · 29.3 KB
/
Copy pathvalidate-patterns.js
File metadata and controls
950 lines (818 loc) · 29.3 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
#!/usr/bin/env node
/**
* Pattern Validation Script for EthSystems Map
* Validates pattern and vendor documents against PRD requirements
*/
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
// Initialize JSON Schema validator
const ajv = new Ajv({ allErrors: true, strict: false });
addFormats(ajv);
// Load schemas
const SCHEMAS_DIR = path.join(__dirname, 'schemas');
let patternSchema, vendorSchema, useCaseSchema, jurisdictionSchema;
try {
patternSchema = JSON.parse(fs.readFileSync(path.join(SCHEMAS_DIR, 'pattern.json'), 'utf8'));
vendorSchema = JSON.parse(fs.readFileSync(path.join(SCHEMAS_DIR, 'vendor.json'), 'utf8'));
useCaseSchema = JSON.parse(fs.readFileSync(path.join(SCHEMAS_DIR, 'use-case.json'), 'utf8'));
jurisdictionSchema = JSON.parse(fs.readFileSync(path.join(SCHEMAS_DIR, 'jurisdiction.json'), 'utf8'));
} catch (e) {
console.warn('Warning: Could not load JSON schemas:', e.message);
}
// Compile validators
const validatePatternSchema = patternSchema ? ajv.compile(patternSchema) : null;
const validateVendorSchema = vendorSchema ? ajv.compile(vendorSchema) : null;
const validateUseCaseSchema = useCaseSchema ? ajv.compile(useCaseSchema) : null;
const validateJurisdictionSchema = jurisdictionSchema ? ajv.compile(jurisdictionSchema) : null;
// Configuration
const PATTERNS_DIR = path.join(__dirname, '..', 'patterns');
const VENDORS_DIR = path.join(__dirname, '..', 'vendors');
const APPROACHES_DIR = path.join(__dirname, '..', 'approaches');
const USE_CASES_DIR = path.join(__dirname, '..', 'use-cases');
const ROOT_DIR = path.join(__dirname, '..');
const IS_CI = process.env.CI_MODE === 'strict';
// Word count limits for conciseness
const WORD_LIMITS = {
patterns: { warn: 800, error: 1500 },
vendors: { warn: 1000, error: 1800 },
approaches: { warn: 2000, error: 3000 }
};
// Required frontmatter fields for patterns
const REQUIRED_PATTERN_FRONTMATTER = [
'title',
'status',
'maturity',
'crops_profile'
];
// Recommended v2 frontmatter (warnings only).
const RECOMMENDED_PATTERN_FRONTMATTER = [
'layer',
'last_reviewed'
];
// Required sections in pattern documents.
// `## Protocol` and `## Example` are skipped for `type: meta` patterns
// (handled inline in validatePattern).
const REQUIRED_PATTERN_SECTIONS = [
'## Intent',
'## Components',
'## Protocol',
'## Guarantees & threat model',
'## Trade-offs',
'## Example',
'## See also'
];
const V2_MATURITY_VALUES = ['research', 'concept', 'testnet', 'production'];
// Required sections for vendor documents
const REQUIRED_VENDOR_SECTIONS = [
'## What it is',
'## Fits with patterns',
'## Architecture',
'## Links'
];
// Recommended sections for vendor documents (warnings only)
const RECOMMENDED_VENDOR_SECTIONS = [
'## Not a substitute for',
'## Privacy domains',
'## Enterprise demand',
'## Technical details',
'## Strengths',
'## Risks'
];
// Required sections for use case documents
const REQUIRED_USE_CASE_SECTIONS = [
'## 1) Use Case',
'## 3) Actors',
'## 4) Problems',
'## 5) Recommended Approaches'
];
// Recommended sections for use case documents
const RECOMMENDED_USE_CASE_SECTIONS = [
'## 6) Open Questions',
'## 7) Notes'
];
// Required sections for approach documents (v2 schema per issue #151).
// Six sections, fixed order. No recommended extras: ## See also is not part of v2.
const REQUIRED_APPROACH_SECTIONS = [
'## Problem framing',
'## Approaches',
'## Comparison',
'## Persona perspectives',
'## Recommendation',
'## Open questions'
];
// Required frontmatter for approach documents (v2 schema per issue #151).
const REQUIRED_APPROACH_FRONTMATTER = [
'title',
'status',
'last_reviewed',
'use_case',
'primary_patterns'
];
// Required sections for jurisdiction documents
// Jurisdiction cards are deliberately high-level (developer orientation, not legal
// advice): a short posture summary, optional things to watch, and links.
const REQUIRED_JURISDICTION_SECTIONS = [
'## At a Glance'
];
// Recommended sections for jurisdiction documents
const RECOMMENDED_JURISDICTION_SECTIONS = [
'## What to Watch',
'## See also'
];
// Required frontmatter for vendors
const REQUIRED_VENDOR_FRONTMATTER = ['title', 'status'];
// Required frontmatter for use cases
const REQUIRED_USE_CASE_FRONTMATTER = ['title', 'primary_domain'];
// Required frontmatter for jurisdictions
const REQUIRED_JURISDICTION_FRONTMATTER = ['title', 'status', 'region'];
// Validation results
let errors = [];
let warnings = [];
/**
* Parse markdown file and extract frontmatter and content
*/
function parseMarkdown(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
let frontmatter = {};
let markdownContent = '';
let inFrontmatter = false;
let frontmatterLines = [];
for (let i = 0; i < lines.length; i++) {
if (i === 0 && lines[i] === '---') {
inFrontmatter = true;
continue;
}
if (inFrontmatter && lines[i] === '---') {
inFrontmatter = false;
try {
frontmatter = yaml.load(frontmatterLines.join('\n')) || {};
// Convert Date objects to strings for schema validation
for (const key of Object.keys(frontmatter)) {
if (frontmatter[key] instanceof Date) {
frontmatter[key] = frontmatter[key].toISOString().split('T')[0];
}
}
} catch (e) {
errors.push(`${filePath}: Invalid YAML frontmatter - ${e.message}`);
}
continue;
}
if (inFrontmatter) {
frontmatterLines.push(lines[i]);
} else if (!inFrontmatter && lines[i] !== '---') {
markdownContent += lines[i] + '\n';
}
}
return { frontmatter, content: markdownContent };
}
/**
* Extract and validate internal markdown links
*/
function validateInternalLinks(filePath, content) {
const linkErrors = [];
const dir = path.dirname(filePath);
// Match markdown links: [text](path)
const linkRegex = /\[([^\]]*)\]\(([^)]+)\)/g;
let match;
while ((match = linkRegex.exec(content)) !== null) {
const linkPath = match[2];
// Skip external links, anchors, and special protocols
if (linkPath.startsWith('http://') ||
linkPath.startsWith('https://') ||
linkPath.startsWith('#') ||
linkPath.startsWith('mailto:')) {
continue;
}
// Handle relative .md links
if (linkPath.endsWith('.md') || linkPath.includes('.md#')) {
const mdPath = linkPath.split('#')[0]; // Remove anchor
const resolvedPath = path.resolve(dir, mdPath);
if (!fs.existsSync(resolvedPath)) {
linkErrors.push(`Broken internal link: ${linkPath}`);
}
}
}
return linkErrors;
}
/**
* Count words in content (excluding frontmatter and code blocks)
*/
function countWords(content) {
// Remove code blocks
let text = content.replace(/```[\s\S]*?```/g, '');
// Remove inline code
text = text.replace(/`[^`]+`/g, '');
// Remove links but keep text
text = text.replace(/\[([^\]]*)\]\([^)]+\)/g, '$1');
// Remove markdown formatting
text = text.replace(/[#*_\[\]]/g, ' ');
// Split and count
const words = text.split(/\s+/).filter(w => w.length > 0);
return words.length;
}
/**
* Validate pattern file naming convention
*/
function validatePatternFileName(fileName) {
if (!fileName.startsWith('pattern-')) {
return 'Pattern files must start with "pattern-"';
}
if (!fileName.endsWith('.md')) {
return 'Pattern files must end with ".md"';
}
const nameWithoutExtension = fileName.slice(0, -3);
if (!/^pattern-[a-z0-9-]+$/.test(nameWithoutExtension)) {
return 'Pattern names must use kebab-case (lowercase letters, numbers, and hyphens only)';
}
// Check for vendor-specific names
const vendorSpecificPatterns = ['flashbots', 'shutter', 'suave', 'aztec', 'polygon'];
for (const vendor of vendorSpecificPatterns) {
if (fileName.includes(vendor)) {
return `Pattern name contains vendor-specific term "${vendor}" - use generic names instead`;
}
}
return null;
}
/**
* Validate v2-specific frontmatter fields. Strict mode: these are errors.
*/
function validateV2Fields(frontmatter, fileErrors) {
// context: both requires context_differentiation with both slots filled.
if (frontmatter.context === 'both') {
const cd = frontmatter.context_differentiation;
if (!cd) {
fileErrors.push(`context is 'both' but context_differentiation is missing. Add both i2i and i2u strings.`);
} else {
if (!cd.i2i || String(cd.i2i).trim() === '') {
fileErrors.push(`context_differentiation.i2i is empty. Required when context: both.`);
}
if (!cd.i2u || String(cd.i2u).trim() === '') {
fileErrors.push(`context_differentiation.i2u is empty. Required when context: both.`);
}
}
}
// type: meta requires sub_patterns; conversely, only meta patterns may set it.
if (frontmatter.type === 'meta') {
if (!Array.isArray(frontmatter.sub_patterns) || frontmatter.sub_patterns.length === 0) {
fileErrors.push(`type is 'meta' but sub_patterns is empty.`);
}
} else if (Array.isArray(frontmatter.sub_patterns) && frontmatter.sub_patterns.length > 0) {
fileErrors.push(`sub_patterns is populated but type is not 'meta'. Remove sub_patterns or set type: meta.`);
}
// related_patterns slugs must resolve to an existing pattern file.
const rp = frontmatter.related_patterns;
if (rp && typeof rp === 'object') {
for (const slot of ['requires', 'composes_with', 'alternative_to', 'see_also']) {
const list = rp[slot];
if (!Array.isArray(list)) continue;
for (const slug of list) {
const target = path.join(PATTERNS_DIR, `${slug}.md`);
if (!fs.existsSync(target)) {
fileErrors.push(`related_patterns.${slot} references missing file ${slug}.md`);
}
}
}
}
// sub_patterns slugs must resolve to an existing pattern file.
if (Array.isArray(frontmatter.sub_patterns)) {
for (const sp of frontmatter.sub_patterns) {
if (!sp || !sp.pattern) continue;
const target = path.join(PATTERNS_DIR, `${sp.pattern}.md`);
if (!fs.existsSync(target)) {
fileErrors.push(`sub_patterns references missing file ${sp.pattern}.md`);
}
}
}
}
/**
* Validate a single pattern file
*/
function validatePattern(filePath) {
const fileName = path.basename(filePath);
const { frontmatter, content } = parseMarkdown(filePath);
const fileErrors = [];
const fileWarnings = [];
// Skip template and README files
if (fileName === '_template.md' || fileName === 'README.md') {
return;
}
// Validate file naming
const namingError = validatePatternFileName(fileName);
if (namingError) {
fileErrors.push(namingError);
}
// Validate required frontmatter
for (const field of REQUIRED_PATTERN_FRONTMATTER) {
if (!frontmatter[field]) {
fileErrors.push(`Missing required frontmatter field: ${field}`);
}
}
// Validate crops_profile sub-fields (skip if explicitly opted out with "n/a").
// CROPS fields use single-letter keys matching the acronym:
// cr (Censorship resistance), o (Openness), p (Privacy), s (Security).
if (frontmatter.crops_profile && frontmatter.crops_profile !== 'n/a') {
const cp = frontmatter.crops_profile;
// Targeted detection of v1 alias keys for a clearer migration hint than
// the generic schema additionalProperties error.
const v1Aliases = { os: 'o', privacy: 'p', security: 's' };
for (const [legacy, replacement] of Object.entries(v1Aliases)) {
if (cp[legacy] !== undefined) {
fileErrors.push(`crops_profile uses v1 key '${legacy}'; rename to '${replacement}' (strict v2).`);
}
}
const cropsFields = {
cr: { allowed: ['high', 'medium', 'low', 'none'], label: 'Censorship resistance' },
o: { allowed: ['yes', 'partial', 'no'], label: 'Openness' },
p: { allowed: ['full', 'partial', 'none'], label: 'Privacy' },
s: { allowed: ['high', 'medium', 'low'], label: 'Security' }
};
for (const [field, spec] of Object.entries(cropsFields)) {
const value = cp[field];
if (value === undefined) {
fileErrors.push(`crops_profile missing required field: ${field} (${spec.label})`);
} else if (!spec.allowed.includes(value)) {
fileErrors.push(`crops_profile.${field} has invalid value '${value}'. Allowed: ${spec.allowed.join(', ')}`);
}
}
}
// Check recommended frontmatter (warnings only)
for (const field of RECOMMENDED_PATTERN_FRONTMATTER) {
if (!frontmatter[field]) {
fileWarnings.push(`Missing recommended frontmatter field: ${field}`);
}
}
// JSON Schema validation (if available)
if (validatePatternSchema && Object.keys(frontmatter).length > 0) {
const valid = validatePatternSchema(frontmatter);
if (!valid) {
for (const error of validatePatternSchema.errors) {
const errorPath = error.instancePath || 'frontmatter';
const msg = error.message;
// Schema violations on required fields are errors, not warnings
const isRequiredField = error.keyword === 'required' ||
errorPath.startsWith('/crops_profile');
if (error.keyword === 'additionalProperties') {
if (errorPath.startsWith('/crops_profile')) {
fileErrors.push(`Schema: unexpected field '${error.params.additionalProperty}' in crops_profile`);
} else {
fileErrors.push(`Schema: unexpected field '${error.params.additionalProperty}' in frontmatter`);
}
} else if (isRequiredField) {
fileErrors.push(`Schema: ${errorPath} ${msg}`);
} else if (error.keyword === 'enum') {
fileWarnings.push(`Schema: ${errorPath} ${msg}. Allowed: ${error.params.allowedValues.join(', ')}`);
} else {
fileWarnings.push(`Schema: ${errorPath} ${msg}`);
}
}
}
}
// Validate frontmatter values
if (frontmatter.status && !['draft', 'ready'].includes(frontmatter.status)) {
fileErrors.push(`Invalid status value: ${frontmatter.status} (must be 'draft' or 'ready')`);
}
if (frontmatter.maturity) {
const m = frontmatter.maturity;
if (!V2_MATURITY_VALUES.includes(m)) {
fileErrors.push(`Invalid maturity value '${m}'. Must be one of: ${V2_MATURITY_VALUES.join(' | ')}.`);
}
}
const isMeta = frontmatter.type === 'meta';
// Validate required sections. Meta-patterns may omit Protocol / Example.
for (const section of REQUIRED_PATTERN_SECTIONS) {
if (isMeta && (section === '## Protocol' || section === '## Example')) {
continue;
}
if (!content.includes(section)) {
fileErrors.push(`Missing required section: ${section}`);
}
}
validateV2Fields(frontmatter, fileErrors);
// Validate internal links (warning only - enable blocking after fixing existing issues)
const linkErrors = validateInternalLinks(filePath, content);
if (linkErrors.length > 0) {
fileWarnings.push(...linkErrors);
}
// Check word count for conciseness
const wordCount = countWords(content);
const limits = WORD_LIMITS.patterns;
if (wordCount > limits.error) {
fileErrors.push(`Content too long: ${wordCount} words (max ${limits.error})`);
} else if (wordCount > limits.warn) {
fileWarnings.push(`Content length: ${wordCount} words (recommended max ${limits.warn})`);
}
// Report errors and warnings
if (fileErrors.length > 0) {
errors.push(`\n❌ ${fileName}:\n ${fileErrors.join('\n ')}`);
}
if (fileWarnings.length > 0) {
warnings.push(`\n⚠️ ${fileName}:\n ${fileWarnings.join('\n ')}`);
}
}
/**
* Validate a single vendor file
*/
function validateVendor(filePath) {
const fileName = path.basename(filePath);
const { frontmatter, content } = parseMarkdown(filePath);
const fileErrors = [];
const fileWarnings = [];
// Skip template and README
if (fileName === '_template.md' || fileName === 'README.md') {
return;
}
// Validate required frontmatter
for (const field of REQUIRED_VENDOR_FRONTMATTER) {
if (!frontmatter[field]) {
fileErrors.push(`Missing required frontmatter field: ${field}`);
}
}
// JSON Schema validation (if available)
if (validateVendorSchema && Object.keys(frontmatter).length > 0) {
const valid = validateVendorSchema(frontmatter);
if (!valid) {
for (const error of validateVendorSchema.errors) {
const path = error.instancePath || 'frontmatter';
if (error.keyword === 'additionalProperties') {
fileWarnings.push(`Schema: unexpected field '${error.params.additionalProperty}'`);
} else if (error.keyword === 'enum') {
fileWarnings.push(`Schema: ${path} ${error.message}. Allowed: ${error.params.allowedValues.join(', ')}`);
} else {
fileWarnings.push(`Schema: ${path} ${error.message}`);
}
}
}
}
// Validate required sections
for (const section of REQUIRED_VENDOR_SECTIONS) {
if (!content.includes(section)) {
fileWarnings.push(`Missing required section: ${section}`);
}
}
// Check recommended sections
for (const section of RECOMMENDED_VENDOR_SECTIONS) {
// Match partial section names (e.g., "## Risks" matches "## Risks and open questions")
const sectionStart = section.replace('##', '').trim();
const hasSection = content.split('\n').some(line =>
line.startsWith('## ') && line.toLowerCase().includes(sectionStart.toLowerCase())
);
if (!hasSection) {
fileWarnings.push(`Consider adding section: ${section}`);
}
}
// Check word count
const wordCount = countWords(content);
const limits = WORD_LIMITS.vendors;
if (wordCount > limits.error) {
fileErrors.push(`Content too long: ${wordCount} words (max ${limits.error})`);
} else if (wordCount > limits.warn) {
fileWarnings.push(`Content length: ${wordCount} words (recommended max ${limits.warn})`);
}
// Validate internal links
const linkErrors = validateInternalLinks(filePath, content);
if (linkErrors.length > 0) {
fileWarnings.push(...linkErrors);
}
// Report errors and warnings
if (fileErrors.length > 0) {
errors.push(`\n❌ ${fileName}:\n ${fileErrors.join('\n ')}`);
}
if (fileWarnings.length > 0) {
warnings.push(`\n⚠️ ${fileName}:\n ${fileWarnings.join('\n ')}`);
}
}
/**
* Validate a single use case file
*/
function validateUseCase(filePath) {
const fileName = path.basename(filePath);
const { frontmatter, content } = parseMarkdown(filePath);
const fileErrors = [];
const fileWarnings = [];
// Skip template and README
if (fileName === '_template.md' || fileName === 'README.md') {
return;
}
// Validate required frontmatter
for (const field of REQUIRED_USE_CASE_FRONTMATTER) {
if (!frontmatter[field]) {
fileWarnings.push(`Missing recommended frontmatter field: ${field}`);
}
}
// JSON Schema validation (if available)
if (validateUseCaseSchema && Object.keys(frontmatter).length > 0) {
const valid = validateUseCaseSchema(frontmatter);
if (!valid) {
for (const error of validateUseCaseSchema.errors) {
const path = error.instancePath || 'frontmatter';
if (error.keyword === 'additionalProperties') {
fileWarnings.push(`Schema: unexpected field '${error.params.additionalProperty}'`);
} else if (error.keyword === 'enum') {
fileWarnings.push(`Schema: ${path} ${error.message}. Allowed: ${error.params.allowedValues.join(', ')}`);
} else {
fileWarnings.push(`Schema: ${path} ${error.message}`);
}
}
}
}
// Validate required sections
for (const section of REQUIRED_USE_CASE_SECTIONS) {
if (!content.includes(section)) {
fileWarnings.push(`Missing required section: ${section}`);
}
}
// Check recommended sections
for (const section of RECOMMENDED_USE_CASE_SECTIONS) {
const sectionStart = section.replace('##', '').trim();
const hasSection = content.split('\n').some(line =>
line.startsWith('## ') && line.toLowerCase().includes(sectionStart.toLowerCase())
);
if (!hasSection) {
fileWarnings.push(`Consider adding section: ${section}`);
}
}
// Validate internal links
const linkErrors = validateInternalLinks(filePath, content);
if (linkErrors.length > 0) {
fileWarnings.push(...linkErrors);
}
// Report errors and warnings
if (fileErrors.length > 0) {
errors.push(`\n❌ ${fileName}:\n ${fileErrors.join('\n ')}`);
}
if (fileWarnings.length > 0) {
warnings.push(`\n⚠️ ${fileName}:\n ${fileWarnings.join('\n ')}`);
}
}
/**
* Validate a single approach file
*/
function validateApproach(filePath) {
const fileName = path.basename(filePath);
const { frontmatter, content } = parseMarkdown(filePath);
const fileErrors = [];
const fileWarnings = [];
// Skip template and README
if (fileName === '_template.md' || fileName === 'README.md') {
return;
}
// Validate approach file naming
if (!fileName.startsWith('approach-')) {
fileWarnings.push('Approach files should start with "approach-"');
}
// Validate required frontmatter (v2 schema per issue #151).
// Empty arrays must be flagged (e.g. template default `primary_patterns: []`).
for (const field of REQUIRED_APPROACH_FRONTMATTER) {
const value = frontmatter[field];
const isEmptyArray = Array.isArray(value) && value.length === 0;
if (!value || isEmptyArray) {
fileWarnings.push(`Missing required frontmatter field: ${field}`);
}
}
// Title prefix check (v2: title must start with "Approach: ")
if (frontmatter.title && !/^Approach:\s/.test(frontmatter.title)) {
fileWarnings.push(`Title should start with "Approach: " (got "${frontmatter.title}")`);
}
// Validate required sections
for (const section of REQUIRED_APPROACH_SECTIONS) {
if (!content.includes(section)) {
fileWarnings.push(`Missing required section: ${section}`);
}
}
// Check word count
const wordCount = countWords(content);
const limits = WORD_LIMITS.approaches;
if (wordCount > limits.error) {
fileErrors.push(`Content too long: ${wordCount} words (max ${limits.error})`);
} else if (wordCount > limits.warn) {
fileWarnings.push(`Content length: ${wordCount} words (recommended max ${limits.warn})`);
}
// Validate internal links
const linkErrors = validateInternalLinks(filePath, content);
if (linkErrors.length > 0) {
fileWarnings.push(...linkErrors);
}
// Report errors and warnings
if (fileErrors.length > 0) {
errors.push(`\n❌ ${fileName}:\n ${fileErrors.join('\n ')}`);
}
if (fileWarnings.length > 0) {
warnings.push(`\n⚠️ ${fileName}:\n ${fileWarnings.join('\n ')}`);
}
}
/**
* Validate a single jurisdiction file
*/
function validateJurisdiction(filePath) {
const fileName = path.basename(filePath);
const { frontmatter, content } = parseMarkdown(filePath);
const fileErrors = [];
const fileWarnings = [];
// Skip template and README
if (fileName === '_template.md' || fileName === 'README.md') {
return;
}
// Validate required frontmatter
for (const field of REQUIRED_JURISDICTION_FRONTMATTER) {
if (!frontmatter[field]) {
fileWarnings.push(`Missing recommended frontmatter field: ${field}`);
}
}
// JSON Schema validation (if available)
if (validateJurisdictionSchema && Object.keys(frontmatter).length > 0) {
const valid = validateJurisdictionSchema(frontmatter);
if (!valid) {
for (const error of validateJurisdictionSchema.errors) {
const path = error.instancePath || 'frontmatter';
if (error.keyword === 'additionalProperties') {
fileWarnings.push(`Schema: unexpected field '${error.params.additionalProperty}'`);
} else if (error.keyword === 'enum') {
fileWarnings.push(`Schema: ${path} ${error.message}. Allowed: ${error.params.allowedValues.join(', ')}`);
} else {
fileWarnings.push(`Schema: ${path} ${error.message}`);
}
}
}
}
// Validate required sections
for (const section of REQUIRED_JURISDICTION_SECTIONS) {
if (!content.includes(section)) {
fileWarnings.push(`Missing required section: ${section}`);
}
}
// Check recommended sections
for (const section of RECOMMENDED_JURISDICTION_SECTIONS) {
const sectionStart = section.replace('##', '').trim();
const hasSection = content.split('\n').some(line =>
line.startsWith('## ') && line.toLowerCase().includes(sectionStart.toLowerCase())
);
if (!hasSection) {
fileWarnings.push(`Consider adding section: ${section}`);
}
}
// Validate internal links
const linkErrors = validateInternalLinks(filePath, content);
if (linkErrors.length > 0) {
fileWarnings.push(...linkErrors);
}
// Report errors and warnings
if (fileErrors.length > 0) {
errors.push(`\n❌ ${fileName}:\n ${fileErrors.join('\n ')}`);
}
if (fileWarnings.length > 0) {
warnings.push(`\n⚠️ ${fileName}:\n ${fileWarnings.join('\n ')}`);
}
}
/**
* Validate all patterns in the patterns directory
*/
function validateAllPatterns() {
console.log('🔍 Validating pattern documents...\n');
const files = fs.readdirSync(PATTERNS_DIR);
const patternFiles = files.filter(f => f.endsWith('.md'));
for (const file of patternFiles) {
const filePath = path.join(PATTERNS_DIR, file);
validatePattern(filePath);
}
}
/**
* Validate all vendors
*/
function validateAllVendors() {
console.log('🔍 Validating vendor documents...\n');
if (!fs.existsSync(VENDORS_DIR)) return;
const files = fs.readdirSync(VENDORS_DIR);
const vendorFiles = files.filter(f => f.endsWith('.md'));
for (const file of vendorFiles) {
const filePath = path.join(VENDORS_DIR, file);
validateVendor(filePath);
}
}
/**
* Validate all use cases
*/
function validateAllUseCases() {
console.log('🔍 Validating use case documents...\n');
if (!fs.existsSync(USE_CASES_DIR)) return;
const files = fs.readdirSync(USE_CASES_DIR);
const useCaseFiles = files.filter(f => f.endsWith('.md'));
for (const file of useCaseFiles) {
const filePath = path.join(USE_CASES_DIR, file);
validateUseCase(filePath);
}
}
/**
* Validate all approaches
*/
function validateAllApproaches() {
console.log('🔍 Validating approach documents...\n');
if (!fs.existsSync(APPROACHES_DIR)) return;
const files = fs.readdirSync(APPROACHES_DIR);
const approachFiles = files.filter(f => f.endsWith('.md'));
for (const file of approachFiles) {
const filePath = path.join(APPROACHES_DIR, file);
validateApproach(filePath);
}
}
/**
* Validate all jurisdictions
*/
function validateAllJurisdictions() {
console.log('🔍 Validating jurisdiction documents...\n');
const jurisdictionsDir = path.join(ROOT_DIR, 'jurisdictions');
if (!fs.existsSync(jurisdictionsDir)) return;
const files = fs.readdirSync(jurisdictionsDir);
const jurisdictionFiles = files.filter(f => f.endsWith('.md'));
for (const file of jurisdictionFiles) {
const filePath = path.join(jurisdictionsDir, file);
validateJurisdiction(filePath);
}
}
/**
* Generate validation report
*/
function generateReport() {
const report = [];
report.push('### Pattern Validation Report\n');
if (errors.length === 0 && warnings.length === 0) {
report.push('✅ All patterns pass validation!\n');
} else {
if (errors.length > 0) {
report.push(`#### Errors (${errors.length})`);
report.push(...errors);
report.push('');
}
if (warnings.length > 0) {
report.push(`#### Warnings (${warnings.length})`);
report.push(...warnings);
report.push('');
}
}
// Write report for GitHub Actions summary
fs.writeFileSync('validation-report.md', report.join('\n'));
// Console output
console.log(report.join('\n'));
}
/**
* Validate specific files passed as arguments
*/
function validateSpecificFiles(files) {
for (const filePath of files) {
const absPath = path.resolve(filePath);
if (!fs.existsSync(absPath)) {
console.warn(`Warning: File not found: ${filePath}`);
continue;
}
// Determine file type by directory
if (absPath.includes('/patterns/')) {
validatePattern(absPath);
} else if (absPath.includes('/vendors/')) {
validateVendor(absPath);
} else if (absPath.includes('/use-cases/')) {
validateUseCase(absPath);
} else if (absPath.includes('/approaches/')) {
validateApproach(absPath);
} else if (absPath.includes('/jurisdictions/')) {
validateJurisdiction(absPath);
} else {
console.log(`Skipping ${filePath} (not in a content directory)`);
}
}
}
/**
* Main execution
*/
function main() {
try {
// Check for file arguments (used by lint-staged)
const args = process.argv.slice(2);
const fileArgs = args.filter(arg => arg.endsWith('.md') && !arg.startsWith('--'));
const hasFileFlag = args.includes('--file') || args.includes('-f');
if (fileArgs.length > 0 || hasFileFlag) {
// Validate only specified files
console.log(`🔍 Validating ${fileArgs.length} file(s)...\n`);
validateSpecificFiles(fileArgs);
} else {
// Validate all files in all directories
validateAllPatterns();
validateAllVendors();
validateAllUseCases();
validateAllApproaches();
validateAllJurisdictions();
}
generateReport();
// Exit with error if there are validation errors in CI mode
if (IS_CI && errors.length > 0) {
process.exit(1);
}
} catch (error) {
console.error('Validation script error:', error);
process.exit(1);
}
}
// Run validation
main();