Skip to content

Commit

Permalink
conformance-profiles: Add Submodule and util function
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Jul 16, 2020
1 parent c055722 commit a5ec845
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:

steps:
- uses: actions/checkout@v2
with:
submodules: 'true'
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "conformance-profiles"]
path = conformance-profiles
url = https://github.com/openactive/conformance-profiles.git
1 change: 1 addition & 0 deletions conformance-profiles
Submodule conformance-profiles added at a966d0
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"homepage": "https://github.com/openactive/feed-normaliser#readme",
"dependencies": {
"@openactive/data-model-validator": "^2.0.25",
"ajv": "^6.12.3",
"cheerio": "^1.0.0-rc.3",
"express": "^4.17.1",
"node-fetch": "^2.6.0",
Expand Down
63 changes: 63 additions & 0 deletions src/lib/util-data-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Ajv from 'ajv';
import fs from 'fs';
import Utils from "./utils.js";

async function apply_data_profile(data, data_profile_name) {

// ---------------------- Load Data Profile Info
if (!(await does_data_profile_exist(data_profile_name))) {
return {
"done": false,
"error": "Data Profile Does Not Exist"
};
}

// ---------------------- Load Type Info
var data_type = data['@type'];
if (!(await does_data_profile_schema_exist(data_profile_name, data_type))) {
return {
"done": false,
"error": "Type Does Not Exist " + data_type
};
}

// ---------------------- Process
var validate = await getAjvValidate(data_profile_name, data_type);
var valid = await validate(data);

return {
"done": true,
"results": validate.errors,
};

};

async function getAjvValidate(data_profile_name, type) {
// we will assume data_profile_name and type exists and you've already checked that with other functions
// TODO every time it's called, we load a lot from disk. Can we cache result anywhere in a "Thread"-safe manner?
const ajv = new Ajv({allErrors: true});
// TODO load from an absolute path, not a relative one
const file_names = fs.readdirSync('conformance-profiles/'+data_profile_name);
for(var file_name of file_names) {
if (file_name != 'data-profile.json' && file_name.substring(file_name.length - 5) == '.json' && file_name != type+'.json') {
const json_schema_string = await fs.promises.readFile('conformance-profiles/'+data_profile_name+'/'+file_name, "utf8");
const json_schema = await JSON.parse(json_schema_string);
await ajv.addSchema(json_schema);
}
};
const type_json_schema_string = await fs.promises.readFile('conformance-profiles/'+data_profile_name+'/'+type+'.json', "utf8");
const type_json_schema = await JSON.parse(type_json_schema_string);
return await ajv.compile(type_json_schema);
}

async function does_data_profile_exist(data_profile_name) {
// TODO load from an absolute path, not a relative one
return await fs.existsSync('conformance-profiles/'+data_profile_name+'/data-profile.json');
}

async function does_data_profile_schema_exist(data_profile_name, schema_name) {
// TODO load from an absolute path, not a relative one
return await fs.existsSync('conformance-profiles/'+data_profile_name+'/'+schema_name+'.json');
}

export default apply_data_profile;
43 changes: 43 additions & 0 deletions test/test-util-data-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import assert from 'assert';
import apply_data_profile from '../src/lib/util-data-profile.js';


describe('util-data-profile', function() {

it('Data Profile Name Does Not Exist', async function() {

let results = await apply_data_profile({'@type': 'Event'}, 'kangaroos');

assert.equal(results.done,false);
assert.equal(results.error,"Data Profile Does Not Exist");

});

it('Test', function(done) {

let results_promise = apply_data_profile(
{
'@type': 'Event',
'location': {
'@type': 'Place'
}
},
'core'
);
results_promise.then((results)=> {

assert.equal(results.done, true);
assert.equal(results.results.length,22);
// Just test a couple
assert.deepEqual(results.results[0].message,"should have required property 'activity'");
assert.deepEqual(results.results[0].dataPath,"");
assert.deepEqual(results.results[6].message,"should have required property 'address'");
assert.deepEqual(results.results[6].dataPath,".location");

})
.then(() => done(), done)
.catch((error) => {
done(error);
});
});
});

0 comments on commit a5ec845

Please sign in to comment.