forked from b-harvest/somnia-validators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.js
More file actions
198 lines (163 loc) Β· 6.69 KB
/
validate.js
File metadata and controls
198 lines (163 loc) Β· 6.69 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
const fs = require('fs');
const path = require('path');
function validateFileName(fileName) {
// Check if filename (without extension) is all lowercase
const nameWithoutExt = path.basename(fileName, '.json');
return nameWithoutExt === nameWithoutExt.toLowerCase();
}
function validateValidatorProfile(filePath) {
try {
const data = fs.readFileSync(filePath, 'utf8');
const profile = JSON.parse(data);
const errors = [];
// Check filename format
const fileName = path.basename(filePath);
if (!validateFileName(fileName)) {
errors.push(`Filename must be all lowercase: "${fileName}" should be "${fileName.toLowerCase()}"`);
}
// Required fields
if (!profile.moniker || typeof profile.moniker !== 'string') {
errors.push('Missing or invalid moniker');
}
if (!profile.details || typeof profile.details !== 'string') {
errors.push('Missing or invalid details');
}
if (!profile.profile || typeof profile.profile !== 'string') {
errors.push('Missing or invalid profile');
}
// Background is optional but validate if present
if (profile.background && typeof profile.background !== 'string') {
errors.push('Invalid background field (should be a string)');
}
if (!profile.contact || typeof profile.contact !== 'object') {
errors.push('Missing or invalid contact object');
} else {
if (!profile.contact.email || typeof profile.contact.email !== 'string') {
errors.push('Missing or invalid contact.email');
}
if (!profile.contact.website || typeof profile.contact.website !== 'string') {
errors.push('Missing or invalid contact.website');
}
}
// Validate image path format
if (profile.profile && !profile.profile.startsWith('./images/')) {
errors.push('profile should start with "./images/"');
}
if (profile.background && !profile.background.startsWith('./background/')) {
errors.push('background should start with "./background/"');
}
// Check if image files exist
if (profile.profile && profile.profile.startsWith('./images/')) {
const imagePath = path.join(path.dirname(filePath), profile.profile);
if (!fs.existsSync(imagePath)) {
errors.push(`Referenced profile image does not exist: ${profile.profile}`);
}
}
if (profile.background && profile.background.startsWith('./background/')) {
const backgroundPath = path.join(path.dirname(filePath), profile.background);
if (!fs.existsSync(backgroundPath)) {
errors.push(`Referenced background image does not exist: ${profile.background}`);
}
}
return {
valid: errors.length === 0,
errors: errors,
profile: profile
};
} catch (error) {
return {
valid: false,
errors: [`Invalid JSON format: ${error.message}`],
profile: null
};
}
}
function validateDirectory(dirName) {
const dirPath = path.join(__dirname, dirName);
if (!fs.existsSync(dirPath)) {
console.log(`Directory ${dirName} not found, skipping...`);
return { valid: true, results: [] };
}
const files = fs.readdirSync(dirPath)
.filter(file => file.endsWith('.json') && file !== 'validator-template.json');
let dirValid = true;
const results = [];
files.forEach(file => {
const filePath = path.join(dirPath, file);
const result = validateValidatorProfile(filePath);
results.push({
file: file,
valid: result.valid,
errors: result.errors
});
if (!result.valid) {
dirValid = false;
}
});
return {
valid: dirValid,
results: results
};
}
function validateAllProfiles() {
const directories = ['testnet', 'mainnet'];
let allValid = true;
console.log('========================================');
console.log(' Validator Profiles Validation Report');
console.log('========================================\n');
directories.forEach(dir => {
console.log(`\nπ Directory: ${dir}`);
console.log('----------------------------------------');
const dirValidation = validateDirectory(dir);
if (dirValidation.results.length === 0) {
console.log(' No JSON files found or directory does not exist.\n');
} else {
dirValidation.results.forEach(result => {
if (result.valid) {
console.log(` β
${result.file}`);
} else {
console.log(` β ${result.file}`);
result.errors.forEach(error => {
console.log(` ββ ${error}`);
});
}
});
if (!dirValidation.valid) {
allValid = false;
}
// Summary for this directory
const validCount = dirValidation.results.filter(r => r.valid).length;
const totalCount = dirValidation.results.length;
console.log(`\n Summary: ${validCount}/${totalCount} files valid`);
}
});
console.log('\n========================================');
if (allValid) {
console.log('π SUCCESS: All validator profiles are valid!');
console.log('========================================\n');
process.exit(0);
} else {
console.log('β FAILED: Some validator profiles have errors.');
console.log('Please fix the errors listed above.');
console.log('========================================\n');
process.exit(1);
}
}
// CLI argument handling
if (require.main === module) {
const args = process.argv.slice(2);
if (args.length > 0 && (args[0] === '--help' || args[0] === '-h')) {
console.log('Usage: node validate.js [options]');
console.log('\nOptions:');
console.log(' --help, -h Show this help message');
console.log('\nThis script validates all JSON files in testnet/ and mainnet/ directories.');
console.log('It checks for:');
console.log(' - Lowercase filenames');
console.log(' - Required fields (moniker, details, profile, contact)');
console.log(' - Proper image path formats');
console.log(' - Existence of referenced image files');
process.exit(0);
}
validateAllProfiles();
}
module.exports = { validateValidatorProfile, validateDirectory, validateAllProfiles };