Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validate processing fields everywhere #6

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"name": "stac-extensions",
"version": "1.1.0",
"scripts": {
"test": "npm run check-markdown && npm run check-examples",
"test": "jest && npm run check-markdown && npm run check-examples",
"check-markdown": "remark . -f -r .github/remark.yaml",
"check-examples": "stac-node-validator . --lint --verbose --schemaMap https://stac-extensions.github.io/processing/v1.1.0/schema.json=./json-schema/schema.json",
"format-examples": "stac-node-validator . --format --schemaMap https://stac-extensions.github.io/processing/v1.1.0/schema.json=./json-schema/schema.json"
},
"dependencies": {
"jest": "^27.4.4",
"remark-cli": "^8.0.0",
"remark-lint": "^7.0.0",
"remark-lint-no-html": "^2.0.0",
Expand Down
25 changes: 25 additions & 0 deletions tests/collection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { join } = require('path');
const { promises } = require('fs');
const { AjvOptions, rootDirectory, schemaPath } = require('./validation.js');
const ajv = new (require('ajv'))(AjvOptions);

const examplePath = join(rootDirectory, 'examples/collection.json');

let validate;
beforeAll(async () => {
const data = JSON.parse(await promises.readFile(schemaPath));
validate = await ajv.compileAsync(data);
});

describe('Collection example', () => {
it('should pass validation', async () => {
// given
const example = JSON.parse(await promises.readFile(examplePath));

// when
let valid = validate(example);

// then
expect(valid).toBeTruthy()
});
});
63 changes: 63 additions & 0 deletions tests/item.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { join } = require('path');
const { promises } = require('fs');
const { AjvOptions, rootDirectory, schemaPath } = require('./validation.js');
const ajv = new (require('ajv'))(AjvOptions);

const examplePath = join(rootDirectory, 'examples/item.json');

let validate;
beforeAll(async () => {
const data = JSON.parse(await promises.readFile(schemaPath));
validate = await ajv.compileAsync(data);
});

describe('Item example', () => {
it('should pass validation', async () => {
// given
const example = JSON.parse(await promises.readFile(examplePath));

// when
let valid = validate(example);

// then
expect(valid).toBeTruthy();
});

it('should fail validation with invalid properties processing:software value', async () => {
// given
const example = JSON.parse(await promises.readFile(examplePath));
example['properties']['processing:software'] = null;

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/properties/processing:software' &&
error.message === 'must be object',
),
).toBeTruthy();
});

it('should fail validation with invalid asset processing:software value ', async () => {
// given
const example = JSON.parse(await promises.readFile(examplePath));
example['assets']['manifest']['processing:software'] = null;

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/assets/manifest/processing:software' &&
error.message === 'must be object',
),
).toBeTruthy();
});
});
31 changes: 31 additions & 0 deletions tests/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const axios = require('axios');
const { dirname, join } = require('path');
const iriFormats = require('stac-node-validator/iri.js');

// const directory = dirname(fileURLToPath(import.meta.url));

const Schemas = new Map();
const loadSchema = function (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.
*/
module.exports.loadSchemaFromUri = async function (uri) {
try {
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.`);
}
}

module.exports.AjvOptions = {loadSchema, formats: Object.assign(iriFormats)};
module.exports.rootDirectory = dirname(__dirname);
module.exports.schemaPath = join(module.exports.rootDirectory, 'json-schema/schema.json');