Add Contributor validation against JSON schema
based on the Contribution validation.
This commit is contained in:
parent
80dc787971
commit
e591742e40
@ -37,12 +37,16 @@ class Contributor extends Record {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
add(contributorAttr, callOptions = {}) {
|
async add(contributorAttr, callOptions = {}) {
|
||||||
let json = ContributorSerializer.serialize(contributorAttr);
|
let contributor = new ContributorSerializer(contributorAttr);
|
||||||
// TODO: validate against schema
|
|
||||||
|
try { await contributor.validate(); }
|
||||||
|
catch (error) { return Promise.reject(error); }
|
||||||
|
|
||||||
|
const jsonStr = contributor.serialize();
|
||||||
|
|
||||||
return this.ipfs
|
return this.ipfs
|
||||||
.add(json)
|
.add(jsonStr)
|
||||||
.then((ipfsHashAttr) => {
|
.then((ipfsHashAttr) => {
|
||||||
let contributor = [
|
let contributor = [
|
||||||
contributorAttr.account,
|
contributorAttr.account,
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
const schemas = require('kosmos-schemas');
|
||||||
|
const validator = require('../utils/validator');
|
||||||
/**
|
/**
|
||||||
* Handle serialization for JSON-LD object of the contributor, according to
|
* Handle serialization for JSON-LD object of the contributor, according to
|
||||||
* https://github.com/67P/kosmos-schemas/blob/master/schemas/contributor.json
|
* https://github.com/67P/kosmos-schemas/blob/master/schemas/contributor.json
|
||||||
@ -6,41 +8,9 @@
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
class Contributor {
|
class Contributor {
|
||||||
/**
|
|
||||||
* Deserialize JSON to object
|
|
||||||
*
|
|
||||||
* @method
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
static deserialize(serialized) {
|
|
||||||
let {
|
|
||||||
name,
|
|
||||||
kind,
|
|
||||||
url,
|
|
||||||
accounts,
|
|
||||||
} = JSON.parse(serialized.toString('utf8'));
|
|
||||||
|
|
||||||
let github_username, github_uid, wiki_username;
|
constructor(attrs) {
|
||||||
let github = accounts.find((a) => a.site === 'github.com');
|
Object.keys(attrs).forEach(a => this[a] = attrs[a]);
|
||||||
let wiki = accounts.find((a) => a.site === 'wiki.kosmos.org');
|
|
||||||
|
|
||||||
if (github) {
|
|
||||||
(({ username: github_username, uid: github_uid} = github));
|
|
||||||
}
|
|
||||||
if (wiki) {
|
|
||||||
(({ username: wiki_username } = wiki));
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
name,
|
|
||||||
kind,
|
|
||||||
url,
|
|
||||||
accounts,
|
|
||||||
github_uid,
|
|
||||||
github_username,
|
|
||||||
wiki_username,
|
|
||||||
ipfsData: serialized,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,7 +19,7 @@ class Contributor {
|
|||||||
* @method
|
* @method
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
static serialize(deserialized) {
|
serialize () {
|
||||||
let {
|
let {
|
||||||
name,
|
name,
|
||||||
kind,
|
kind,
|
||||||
@ -57,7 +27,7 @@ class Contributor {
|
|||||||
github_uid,
|
github_uid,
|
||||||
github_username,
|
github_username,
|
||||||
wiki_username,
|
wiki_username,
|
||||||
} = deserialized;
|
} = this;
|
||||||
|
|
||||||
let data = {
|
let data = {
|
||||||
"@context": "https://schema.kosmos.org",
|
"@context": "https://schema.kosmos.org",
|
||||||
@ -91,6 +61,56 @@ class Contributor {
|
|||||||
// Write it pretty to ipfs
|
// Write it pretty to ipfs
|
||||||
return JSON.stringify(data, null, 2);
|
return JSON.stringify(data, null, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate serialized data against schema
|
||||||
|
*
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
validate () {
|
||||||
|
const serialized = JSON.parse(this.serialize());
|
||||||
|
console.log(serialized);
|
||||||
|
const valid = validator.validate(serialized, schemas['contributor']);
|
||||||
|
return valid ? Promise.resolve() : Promise.reject(validator.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize JSON to object
|
||||||
|
*
|
||||||
|
* @method
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
deserialize (serialized) {
|
||||||
|
let {
|
||||||
|
name,
|
||||||
|
kind,
|
||||||
|
url,
|
||||||
|
accounts,
|
||||||
|
} = JSON.parse(serialized.toString('utf8'));
|
||||||
|
|
||||||
|
let github_username, github_uid, wiki_username;
|
||||||
|
let github = accounts.find((a) => a.site === 'github.com');
|
||||||
|
let wiki = accounts.find((a) => a.site === 'wiki.kosmos.org');
|
||||||
|
|
||||||
|
if (github) {
|
||||||
|
(({ username: github_username, uid: github_uid} = github));
|
||||||
|
}
|
||||||
|
if (wiki) {
|
||||||
|
(({ username: wiki_username } = wiki));
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
kind,
|
||||||
|
url,
|
||||||
|
accounts,
|
||||||
|
github_uid,
|
||||||
|
github_username,
|
||||||
|
wiki_username,
|
||||||
|
ipfsData: serialized,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Contributor;
|
module.exports = Contributor;
|
||||||
|
@ -26,7 +26,7 @@ module.exports = async function(callback) {
|
|||||||
kind: await prompt('Kind (default person): ', {default: 'person'}),
|
kind: await prompt('Kind (default person): ', {default: 'person'}),
|
||||||
url: await prompt('URL: '),
|
url: await prompt('URL: '),
|
||||||
github_username: await prompt('GitHub username: '),
|
github_username: await prompt('GitHub username: '),
|
||||||
github_uid: await prompt('GitHub UID: '),
|
github_uid: parseInt(await prompt('GitHub UID: ')),
|
||||||
wiki_username: await prompt('Wiki username: '),
|
wiki_username: await prompt('Wiki username: '),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user