Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
release:
types: [published]
jobs:
deploy:
publish:
runs-on: ubuntu-latest
steps:
- name: Inject env variables
Expand Down
17 changes: 12 additions & 5 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
name: Check Markdown and Examples
on: [push, pull_request]
jobs:
deploy:
runs-on: ubuntu-latest
check:
strategy:
fail-fast: false
matrix:
runner: [
'macos-latest',
'ubuntu-latest',
'windows-latest',
]
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/setup-node@v2
with:
node-version: 'lts/*'
- uses: actions/checkout@v2
- run: |
npm install
npm test
- run: npm install
- run: npm test
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"name": "stac-extensions",
"version": "1.0.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/template/v1.0.0/schema.json=./json-schema/schema.json",
"format-examples": "stac-node-validator . --format --schemaMap https://stac-extensions.github.io/template/v1.0.0/schema.json=./json-schema/schema.json"
},
"type": "module",
"dependencies": {
"jest": "^27.4.4",
"remark-cli": "^8.0.0",
"remark-lint": "^7.0.0",
"remark-lint-no-html": "^2.0.0",
Expand Down
42 changes: 42 additions & 0 deletions tests/collection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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();
});

it('should fail validation without mandatory template:new_field field', async () => {
// given
const example = JSON.parse(await promises.readFile(examplePath));
delete example['assets'];
delete example['item_assets'];
delete example['template:new_field'];

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some((error) => error.message === "must have required property 'template:new_field'"),
).toBeTruthy();
});
});
39 changes: 39 additions & 0 deletions tests/item.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 without mandatory template:new_field property ', async () => {
// given
const example = JSON.parse(await promises.readFile(examplePath));
delete example.properties['template:new_field'];
// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some((error) => error.message === "must have required property 'template:new_field'"),
).toBeTruthy();
});
});
29 changes: 29 additions & 0 deletions tests/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const axios = require('axios');
const { dirname, join } = require('path');
const iriFormats = require('stac-node-validator/iri.js');

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');