Skip to content

Commit 9f358c0

Browse files
committed
Merge remote-tracking branch 'origin/dev' into master, v0.4.5
2 parents 59fbb4a + eab7ddf commit 9f358c0

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

COMPARISON.md

+2
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ Here I'd like to give an overview of what the validators are capable of and what
4242
| Can follow links | Yes | Yes | No |
4343
| Parallelisation | Yes | No | No |
4444
| Validate against local schemas | Yes | Planned | Yes |
45+
| Lint JSON files | No | No | Yes |
46+
| Format/Pretty-print JSON files | No | Yes | Yes |
4547
| Other comments | Uses pystac validation | General Python library to work with STAC catalogs | - |

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ See the [STAC Validator Comparison](COMPARISON.md) for the features supported by
66

77
## Versions
88

9-
**Current version: 0.4.4**
9+
**Current version: 0.4.5**
1010

1111
| STAC Node Validator Version | Supported STAC Versions |
1212
| --------------------------- | ----------------------- |
@@ -37,6 +37,8 @@ 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+
- To lint local JSON files: `--lint`
41+
- To format / pretty-print local JSON files: `--format` (Attention: this will override the source files without warning!)
4042

4143
**Note on API support:** Validating lists of STAC items/collections (i.e. `GET /collections` and `GET /collections/:id/items`) is partially supported.
4244
It only checks the contained items/collections, but not the other parts of the response (e.g. `links`).

index.js

+43-11
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,51 @@ async function run() {
6565
throw new Error('Schema folder is not a valid directory');
6666
}
6767
}
68+
69+
const doLint = (typeof args.lint !== 'undefined');
70+
const doFormat = (typeof args.format !== 'undefined');
6871

6972
let stats = {
7073
files: files.length,
7174
invalid: 0,
72-
valid: 0
75+
valid: 0,
76+
malformed: 0
7377
}
7478
for(let file of files) {
7579
// Read STAC file
7680
let json;
81+
console.log(`-- ${file}`);
7782
try {
7883
if (isUrl(file)) {
7984
// For simplicity, we just load the URLs with $RefParser, so we don't need another dependency.
8085
json = await $RefParser.parse(file);
86+
if (doLint) {
87+
console.warn("--- Linting not supported for remote files");
88+
}
89+
if (doFormat) {
90+
console.warn("--- Formatting not supported for remote files");
91+
}
8192
}
8293
else {
83-
json = await fs.readJson(file);
94+
let fileContent = await fs.readFile(file);
95+
json = JSON.parse(fileContent);
96+
if (doLint || doFormat) {
97+
// 2 spaces, *nix newlines, newline at end of file
98+
const expectedContent = JSON.stringify(json, null, 2).replace(/(\r\n|\r)/g, "\n") + "\n";
99+
if (fileContent !== expectedContent) {
100+
stats.malformed++;
101+
if (doLint) {
102+
console.warn("--- Lint: File is malformed -> use `--format` to fix the issue");
103+
}
104+
if (doFormat) {
105+
console.warn("--- Format: File was malformed -> fixed the issue");
106+
await fs.writeFile(file, expectedContent);
107+
}
108+
}
109+
else if (doLint) {
110+
console.warn("--- Lint: File is well-formed");
111+
}
112+
}
84113
}
85114
}
86115
catch(error) {
@@ -93,12 +122,12 @@ async function run() {
93122
if (Array.isArray(json.collections)) {
94123
entries = json.collections;
95124
isApiList = true;
96-
console.log(`${file} is a /collections endpoint. Validating all ${entries.length} collections, but ignoring the other parts of the response.\n`);
125+
console.log(`--- The file is a /collections endpoint. Validating all ${entries.length} collections, but ignoring the other parts of the response.\n`);
97126
}
98127
else if (Array.isArray(json.features)) {
99128
entries = json.features;
100129
isApiList = true;
101-
console.log(`${file} is a /collections/:id/items endpoint. Validating all ${entries.length} items, but ignoring the other parts of the response.\n`);
130+
console.log(`--- The file is a /collections/:id/items endpoint. Validating all ${entries.length} items, but ignoring the other parts of the response.\n`);
102131
}
103132
else {
104133
entries = [json];
@@ -107,21 +136,21 @@ async function run() {
107136
let fileValid = true;
108137
for(let data of entries) {
109138

110-
let id = file;
139+
let id = '';
111140
if (isApiList) {
112-
id += " -> " + data.id;
141+
id = `${data.id}: `;
113142
}
114143
if (typeof data.stac_version !== 'string') {
115-
console.error(`-- ${id}: Skipping; No STAC version found\n`);
144+
console.error(`--- ${id}Skipping; No STAC version found\n`);
116145
fileValid = false;
117146
continue;
118147
}
119148
else if (compareVersions(data.stac_version, '1.0.0-beta.2', '<')) {
120-
console.error(`-- ${id}: Skipping; Can only validate STAC version >= 1.0.0-beta.2\n`);
149+
console.error(`--- ${id}Skipping; Can only validate STAC version >= 1.0.0-beta.2\n`);
121150
continue;
122151
}
123152
else {
124-
console.log(`-- ${id} (${data.stac_version})`);
153+
console.log(`--- ${id}STAC Version: ${data.stac_version}`);
125154
}
126155

127156
let type;
@@ -131,11 +160,11 @@ async function run() {
131160
}
132161
else if (data.type === 'FeatureCollection') {
133162
// type = 'itemcollection';
134-
console.warn(`-- ${id}: Skipping; STAC ItemCollections not supported yet\n`);
163+
console.warn(`--- ${id}Skipping; STAC ItemCollections not supported yet\n`);
135164
continue;
136165
}
137166
else {
138-
console.error(`-- ${id}: 'type' is invalid; must be 'Feature'\n`);
167+
console.error(`--- ${id}'type' is invalid; must be 'Feature'\n`);
139168
fileValid = false;
140169
continue;
141170
}
@@ -189,6 +218,9 @@ async function run() {
189218
console.info("Files: " + stats.files);
190219
console.info("Valid: " + stats.valid);
191220
console.info("Invalid: " + stats.invalid);
221+
if (doLint || doFormat) {
222+
console.info("Malformed: " + stats.malformed);
223+
}
192224
process.exit(stats.invalid);
193225
}
194226
catch(error) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stac-node-validator",
3-
"version": "0.4.4",
3+
"version": "0.4.5",
44
"description": "STAC Validator for NodeJS",
55
"author": "Matthias Mohr",
66
"license": "Apache-2.0",

0 commit comments

Comments
 (0)