-
Notifications
You must be signed in to change notification settings - Fork 2
/
validation.mjs
37 lines (33 loc) · 1.25 KB
/
validation.mjs
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
import axios from 'axios';
import { promises as fs } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
import { fastFormats } from 'ajv-formats/dist/formats.js';
import iriFormats from 'stac-node-validator/iri.js';
const Schemas = new Map();
export function loadSchema(uri) {
let existing = Schemas.get(uri);
if (existing == null) {
existing = loadSchemaFromUri(uri);
Schemas.set(uri, existing);
}
return existing;
}
/**
* function passed in to Ajv instance which allows us to load schemas from a url at run time.
*/
export async function loadSchemaFromUri(uri) {
try {
if (uri.startsWith('https://stac.linz.govt.nz/_STAC_VERSION_/')) {
const schemaPath = uri.slice('https://stac.linz.govt.nz/_STAC_VERSION_/'.length);
return JSON.parse(await fs.readFile(join(__dirname, 'extensions', schemaPath)));
}
let response = await axios.get(uri);
return response.data;
} catch (error) {
throw new Error(`-- Schema at '${uri}' not found. Please ensure all entries in 'stac_extensions' are valid.`);
}
}
export const AjvOptions = { loadSchema, formats: Object.assign(fastFormats, iriFormats) };
export const DefaultTimeoutMillis = 60_000;