Skip to content

Commit a68409a

Browse files
committed
fix: Require processing:* properties on providers
1 parent b47551e commit a68409a

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
"name": "stac-extensions",
33
"version": "1.0.0",
44
"scripts": {
5-
"test": "npm run check-markdown && npm run check-examples",
5+
"test": "npx ospec && npm run check-markdown && npm run check-examples",
66
"check-markdown": "remark . -f -r .github/remark.yaml",
77
"check-examples": "stac-node-validator . --lint --verbose --schemaMap https://stac-extensions.github.io/processing/v1.0.0/schema.json=./json-schema/schema.json",
88
"format-examples": "stac-node-validator . --format --schemaMap https://stac-extensions.github.io/processing/v1.0.0/schema.json=./json-schema/schema.json"
99
},
10+
"type": "module",
1011
"dependencies": {
12+
"@types/ospec": "^4.0.2",
13+
"ospec": "^4.1.1",
1114
"remark-cli": "^8.0.0",
1215
"remark-lint": "^7.0.0",
1316
"remark-lint-no-html": "^2.0.0",

tests/template_collection.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import o from 'ospec';
2+
import Ajv from 'ajv';
3+
import { join } from 'path';
4+
import { promises as fs } from 'fs';
5+
import { AjvOptions, rootDirectory, schemaPath } from './validation.js';
6+
7+
const examplePath = join(rootDirectory, 'examples/collection.json');
8+
9+
const propertyNames = [
10+
'processing:expression',
11+
'processing:facility',
12+
'processing:level',
13+
'processing:lineage',
14+
'processing:software',
15+
];
16+
17+
o.spec('Collection', () => {
18+
let validate;
19+
const ajv = new Ajv(AjvOptions);
20+
21+
o.before(async () => {
22+
const data = JSON.parse(await fs.readFile(schemaPath));
23+
validate = await ajv.compileAsync(data);
24+
});
25+
26+
o('Example should pass validation', async () => {
27+
// given
28+
const example = JSON.parse(await fs.readFile(examplePath));
29+
30+
// when
31+
let valid = validate(example);
32+
33+
// then
34+
o(valid).equals(true)(JSON.stringify(validate.errors, null, 2));
35+
});
36+
37+
o("Example without any provider processing: values should fail validation", async () => {
38+
// given
39+
const example = JSON.parse(await fs.readFile(examplePath));
40+
for (const propertyName of propertyNames) {
41+
delete example['providers'][0][propertyName];
42+
}
43+
44+
// when
45+
let valid = validate(example);
46+
47+
// then
48+
o(valid).equals(false);
49+
for (const propertyName of propertyNames) {
50+
o(
51+
validate.errors.some(
52+
(error) =>
53+
error.dataPath === ".providers[0]" &&
54+
error.message === `should have required property '['${propertyName}']'`,
55+
),
56+
).equals(true)(JSON.stringify(validate.errors));
57+
}
58+
});
59+
});

tests/validation.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import axios from 'axios';
2+
import { dirname, join } from 'path';
3+
import { fileURLToPath } from 'url';
4+
import iriFormats from 'stac-node-validator/iri.js';
5+
6+
const directory = dirname(fileURLToPath(import.meta.url));
7+
8+
const Schemas = new Map();
9+
export function loadSchema(uri) {
10+
let existing = Schemas.get(uri);
11+
if (existing == null) {
12+
existing = loadSchemaFromUri(uri);
13+
Schemas.set(uri, existing);
14+
}
15+
return existing;
16+
}
17+
18+
/**
19+
* function passed in to Ajv instance which allows us to load schemas from a url at run time.
20+
*/
21+
export async function loadSchemaFromUri(uri) {
22+
try {
23+
let response = await axios.get(uri);
24+
return response.data;
25+
} catch (error) {
26+
throw new Error(`-- Schema at '${uri}' not found. Please ensure all entries in 'stac_extensions' are valid.`);
27+
}
28+
}
29+
30+
export const AjvOptions = { loadSchema, formats: Object.assign(iriFormats) };
31+
export const rootDirectory = dirname(directory);
32+
export const schemaPath = join(rootDirectory, 'json-schema/schema.json');

0 commit comments

Comments
 (0)