Skip to content

Commit feee4ee

Browse files
committed
Fix bug that did not count invalid JSON as invalid file, add verbose option, more condensed formatting
1 parent 3714fa5 commit feee4ee

File tree

2 files changed

+61
-45
lines changed

2 files changed

+61
-45
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Further options to add to the commands above:
3737

3838
- To validate against schemas in a local STAC folder (e.g. `dev` branch): `--schemas /path/to/stac/folder`
3939
- To not verify SSL/TLS certificates: `--ignoreCerts`
40+
- Add `--verbose` to get a more detailed output
4041
- To lint local JSON files: `--lint` (add `--verbose` to get a diff with the changes required)
4142
- To format / pretty-print local JSON files: `--format` (Attention: this will override the source files without warning!)
4243

index.js

+60-45
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ let ajv = new Ajv({
4141
addUsedSchema: false,
4242
logger: DEBUG ? console : false
4343
});
44+
let verbose = false;
4445

4546
async function run() {
4647
console.log(`STAC Node Validator v${package.version}\n`);
4748
try {
4849
let args = minimist(process.argv.slice(2));
4950

51+
verbose = (typeof args.verbose !== 'undefined');
52+
5053
let files = args._;
5154
if (files.length === 0) {
5255
throw new Error('No path or URL specified.');
@@ -86,16 +89,16 @@ async function run() {
8689
for(let file of files) {
8790
// Read STAC file
8891
let json;
89-
console.log(`-- ${file}`);
92+
console.log(`- ${file}`);
9093
try {
9194
if (isUrl(file)) {
9295
// For simplicity, we just load the URLs with $RefParser, so we don't need another dependency.
9396
json = await $RefParser.parse(file);
9497
if (doLint) {
95-
console.warn("--- Linting not supported for remote files");
98+
console.warn("-- Linting not supported for remote files");
9699
}
97100
if (doFormat) {
98-
console.warn("--- Formatting not supported for remote files");
101+
console.warn("-- Formatting not supported for remote files");
99102
}
100103
}
101104
else {
@@ -106,24 +109,26 @@ async function run() {
106109
if (!matchFile(fileContent, expectedContent)) {
107110
stats.malformed++;
108111
if (doLint) {
109-
console.warn("--- Lint: File is malformed -> use `--format` to fix the issue");
110-
if (typeof args.verbose !== 'undefined') {
112+
console.warn("-- Lint: File is malformed -> use `--format` to fix the issue");
113+
if (verbose) {
111114
console.log(diffStringsUnified(fileContent, expectedContent));
112115
}
113116
}
114117
if (doFormat) {
115-
console.warn("--- Format: File was malformed -> fixed the issue");
118+
console.warn("-- Format: File was malformed -> fixed the issue");
116119
await fs.writeFile(file, expectedContent);
117120
}
118121
}
119-
else if (doLint) {
120-
console.warn("--- Lint: File is well-formed");
122+
else if (doLint && verbose) {
123+
console.warn("-- Lint: File is well-formed");
121124
}
122125
}
123126
}
124127
}
125128
catch(error) {
126-
console.error("-- " + error.message);
129+
stats.invalid++;
130+
stats.malformed++;
131+
console.error("-- " + error.message + "\n");
127132
continue;
128133
}
129134

@@ -132,12 +137,22 @@ async function run() {
132137
if (Array.isArray(json.collections)) {
133138
entries = json.collections;
134139
isApiList = true;
135-
console.log(`--- The file is a /collections endpoint. Validating all ${entries.length} collections, but ignoring the other parts of the response.\n`);
140+
if (verbose) {
141+
console.log(`-- The file is a /collections endpoint. Validating all ${entries.length} collections, but ignoring the other parts of the response.`);
142+
if (entries.length > 1) {
143+
console.log('');
144+
}
145+
}
136146
}
137147
else if (Array.isArray(json.features)) {
138148
entries = json.features;
139149
isApiList = true;
140-
console.log(`--- The file is a /collections/:id/items endpoint. Validating all ${entries.length} items, but ignoring the other parts of the response.\n`);
150+
if (verbose) {
151+
console.log(`-- The file is a /collections/:id/items endpoint. Validating all ${entries.length} items, but ignoring the other parts of the response.`);
152+
if (entries.length > 1) {
153+
console.log('');
154+
}
155+
}
141156
}
142157
else {
143158
entries = [json];
@@ -151,42 +166,38 @@ async function run() {
151166
id = `${data.id}: `;
152167
}
153168
if (typeof data.stac_version !== 'string') {
154-
console.error(`--- ${id}Skipping; No STAC version found\n`);
169+
console.error(`-- ${id}Skipping; No STAC version found\n`);
155170
fileValid = false;
156171
continue;
157172
}
158173
else if (compareVersions(data.stac_version, '1.0.0-beta.2', '<')) {
159-
console.error(`--- ${id}Skipping; Can only validate STAC version >= 1.0.0-beta.2\n`);
174+
console.error(`-- ${id}Skipping; Can only validate STAC version >= 1.0.0-beta.2\n`);
160175
continue;
161176
}
162-
else {
163-
console.log(`--- ${id}STAC Version: ${data.stac_version}`);
177+
else if (verbose) {
178+
console.log(`-- ${id}STAC Version: ${data.stac_version}`);
164179
}
165180

166181
let type;
167-
if (typeof data.type !== 'undefined') {
168-
if (data.type === 'Feature') {
169-
type = 'item';
170-
}
171-
else if (data.type === 'FeatureCollection') {
172-
// type = 'itemcollection';
173-
console.warn(`--- ${id}Skipping; STAC ItemCollections not supported yet\n`);
174-
continue;
175-
}
176-
else {
177-
console.error(`--- ${id}'type' is invalid; must be 'Feature'\n`);
178-
fileValid = false;
179-
continue;
180-
}
182+
if (data.type === 'Feature') {
183+
type = 'item';
181184
}
182-
else {
183-
if (typeof data.extent !== 'undefined' || typeof data.license !== 'undefined') {
184-
type = 'collection';
185+
else if (data.type === 'FeatureCollection') {
186+
// type = 'itemcollection';
187+
console.warn(`-- ${id}Skipping; STAC ItemCollections not supported yet\n`);
188+
continue;
189+
}
190+
else if (typeof data.extent !== 'undefined' || typeof data.license !== 'undefined') {
191+
type = 'collection';
185192

186-
}
187-
else {
188-
type = 'catalog';
189-
}
193+
}
194+
else if (typeof data.description !== 'undefined') {
195+
type = 'catalog';
196+
}
197+
else {
198+
console.error(`-- ${id}Invalid; Can't detect which schema to use.\n`);
199+
fileValid = false;
200+
continue;
190201
}
191202

192203
// Get all schema to validate against
@@ -201,27 +212,31 @@ async function run() {
201212
let validate = await loadSchema(...loadArgs);
202213
let valid = validate(data);
203214
if (!valid) {
204-
console.log(`---- ${schema}: invalid`);
215+
console.log(`--- ${schema}: invalid`);
205216
console.warn(validate.errors);
206217
console.log("\n");
207218
fileValid = false;
208219
if (schema === 'core' && !DEBUG) {
209-
console.info("--- Validation error in core, skipping extension validation");
220+
if (verbose) {
221+
console.info("-- Validation error in core, skipping extension validation");
222+
}
210223
break;
211224
}
212225
}
213-
else {
214-
console.log(`---- ${schema}: valid`);
226+
else if (verbose) {
227+
console.log(`--- ${schema}: valid`);
215228
}
216229
} catch (error) {
217230
fileValid = false;
218-
console.error(`---- ${schema}: ${error.message}`);
231+
console.error(`--- ${schema}: ${error.message}`);
219232
if (DEBUG) {
220233
console.trace(error);
221234
}
222235
}
223236
}
224-
console.log("\n");
237+
if (!fileValid || verbose) {
238+
console.log('');
239+
}
225240
}
226241
fileValid ? stats.valid++ : stats.invalid++;
227242
}
@@ -312,8 +327,8 @@ async function loadSchema(baseUrl = null, version = null, shortcut = null) {
312327
}
313328
});
314329
COMPILED[url] = ajv.compile(fullSchema);
315-
if (parser.$refs.circular) {
316-
console.log(`Schema ${url} is circular, which is not supported by the library. Some properties may not get validated.`);
330+
if (parser.$refs.circular && verbose) {
331+
console.log(`--- Schema ${url} is circular, which is not supported by the library. Some properties may not get validated.`);
317332
}
318333
return COMPILED[url];
319334
} catch (error) {
@@ -324,7 +339,7 @@ async function loadSchema(baseUrl = null, version = null, shortcut = null) {
324339
if (DEBUG) {
325340
console.trace(error);
326341
}
327-
throw new Error(`Schema at '${url}' not found. Please ensure all entries in 'stac_extensions' are valid.`);
342+
throw new Error(`-- Schema at '${url}' not found. Please ensure all entries in 'stac_extensions' are valid.`);
328343
}
329344
else {
330345
throw error;

0 commit comments

Comments
 (0)