Refactor contribution serializer and validation
This commit is contained in:
parent
aa57d7c70b
commit
d953141f52
@ -1,20 +1,5 @@
|
|||||||
const ethers = require('ethers');
|
const ethers = require('ethers');
|
||||||
|
|
||||||
const schemas = require('kosmos-schemas');
|
|
||||||
const tv4 = require('tv4');
|
|
||||||
const validator = tv4.freshApi();
|
|
||||||
|
|
||||||
validator.addFormat({
|
|
||||||
'date': function(value) {
|
|
||||||
const dateRegexp = /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/;
|
|
||||||
return dateRegexp.test(value) ? null : "A valid ISO 8601 full-date string is expected";
|
|
||||||
},
|
|
||||||
'time': function(value) {
|
|
||||||
const timeRegexp = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
|
|
||||||
return timeRegexp.test(value) ? null : "A valid ISO 8601 full-time string is expected";
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const ContributionSerializer = require('../serializers/contribution');
|
const ContributionSerializer = require('../serializers/contribution');
|
||||||
const Base = require('./base');
|
const Base = require('./base');
|
||||||
|
|
||||||
@ -62,12 +47,13 @@ class Contribution extends Base {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addContribution(contributionAttr, callOptions = {}) {
|
async addContribution(contributionAttr, callOptions = {}) {
|
||||||
let jsonStr = ContributionSerializer.serialize(contributionAttr);
|
const contribution = new ContributionSerializer(contributionAttr);
|
||||||
|
|
||||||
// Validate JSON document against schema
|
try { await contribution.validate(); }
|
||||||
let result = validator.validate(JSON.parse(jsonStr), schemas['contribution']);
|
catch (error) { return Promise.reject(error); }
|
||||||
if (!result) { return Promise.reject(validator.error); }
|
|
||||||
|
const jsonStr = contribution.serialize();
|
||||||
|
|
||||||
return this.ipfs
|
return this.ipfs
|
||||||
.add(jsonStr)
|
.add(jsonStr)
|
||||||
|
@ -1,45 +1,24 @@
|
|||||||
|
const schemas = require('kosmos-schemas');
|
||||||
|
const validator = require('../utils/validator');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle serialization for JSON-LD object of the contribution, according to
|
* Serialization and validation for JSON-LD document of the contribution.
|
||||||
* https://github.com/67P/kosmos-schemas/blob/master/schemas/contribution.json
|
|
||||||
*
|
*
|
||||||
* @class
|
* @class
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
class Contribution {
|
class Contribution {
|
||||||
/**
|
|
||||||
* Deserialize JSON to object
|
|
||||||
*
|
|
||||||
* @method
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
static deserialize(serialized) {
|
|
||||||
let {
|
|
||||||
date,
|
|
||||||
time,
|
|
||||||
kind,
|
|
||||||
description,
|
|
||||||
details,
|
|
||||||
url,
|
|
||||||
} = JSON.parse(serialized.toString('utf8'));
|
|
||||||
|
|
||||||
return {
|
constructor(attrs) {
|
||||||
date,
|
Object.keys(attrs).forEach(a => this[a] = attrs[a]);
|
||||||
time,
|
|
||||||
kind,
|
|
||||||
description,
|
|
||||||
details,
|
|
||||||
url,
|
|
||||||
ipfsData: serialized,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize object to JSON
|
* Serialize object to JSON
|
||||||
*
|
*
|
||||||
* @method
|
* @public
|
||||||
* @public
|
*/
|
||||||
*/
|
serialize () {
|
||||||
static serialize(deserialized) {
|
|
||||||
let {
|
let {
|
||||||
contributorIpfsHash,
|
contributorIpfsHash,
|
||||||
date,
|
date,
|
||||||
@ -48,7 +27,7 @@ class Contribution {
|
|||||||
description,
|
description,
|
||||||
url,
|
url,
|
||||||
details
|
details
|
||||||
} = deserialized;
|
} = this;
|
||||||
|
|
||||||
let data = {
|
let data = {
|
||||||
"@context": "https://schema.kosmos.org",
|
"@context": "https://schema.kosmos.org",
|
||||||
@ -70,6 +49,44 @@ class Contribution {
|
|||||||
// 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());
|
||||||
|
const valid = validator.validate(serialized, schemas['contribution']);
|
||||||
|
return valid ? Promise.resolve() : Promise.reject(validator.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize JSON to object
|
||||||
|
*
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
static deserialize (serialized) {
|
||||||
|
let {
|
||||||
|
date,
|
||||||
|
time,
|
||||||
|
kind,
|
||||||
|
description,
|
||||||
|
details,
|
||||||
|
url,
|
||||||
|
} = JSON.parse(serialized.toString('utf8'));
|
||||||
|
|
||||||
|
return {
|
||||||
|
date,
|
||||||
|
time,
|
||||||
|
kind,
|
||||||
|
description,
|
||||||
|
details,
|
||||||
|
url,
|
||||||
|
ipfsData: serialized,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Contribution;
|
module.exports = Contribution;
|
||||||
|
15
lib/utils/validator.js
Normal file
15
lib/utils/validator.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
const tv4 = require('tv4');
|
||||||
|
const validator = tv4.freshApi();
|
||||||
|
|
||||||
|
validator.addFormat({
|
||||||
|
'date': function(value) {
|
||||||
|
const dateRegexp = /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/;
|
||||||
|
return dateRegexp.test(value) ? null : "A valid ISO 8601 full-date string is expected";
|
||||||
|
},
|
||||||
|
'time': function(value) {
|
||||||
|
const timeRegexp = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
|
||||||
|
return timeRegexp.test(value) ? null : "A valid ISO 8601 full-time string is expected";
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = validator;
|
Loading…
x
Reference in New Issue
Block a user