-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
conformance-profiles: Add Submodule and util function
- Loading branch information
1 parent
c055722
commit a5ec845
Showing
7 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Submodule conformance-profiles
added at
a966d0
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |