Add identityKey model validation

This will enforce that all identity record attributes are valid and present
before allowing the record to be saved. This is necessary since we will be
exposing a lower-level method to save an identity with explicit values for
firstUse, nonblockingApproval, and verified status.
This commit is contained in:
lilia 2017-06-13 12:38:44 -07:00 committed by Scott Nonnenberg
parent b93042f12f
commit 50dd9c6772

View file

@ -15,6 +15,15 @@
UNVERIFIED: 2,
};
function validateVerifiedStatus(status) {
if ( status === VerifiedStatus.DEFAULT
|| status === VerifiedStatus.VERIFIED
|| status === VerifiedStatus.UNVERIFIED) {
return true;
}
return false;
}
var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
var StaticUint8ArrayProto = new Uint8Array().__proto__;
@ -95,7 +104,52 @@
return this.fetch({range: [number + '.1', number + '.' + ':']});
}
});
var IdentityKey = Model.extend({ storeName: 'identityKeys' });
var IdentityKey = Model.extend({
storeName: 'identityKeys',
validAttributes: [
'id',
'publicKey',
'firstUse',
'timestamp',
'verified',
'nonblockingApproval'
],
validate: function(attrs, options) {
var attributeNames = _.keys(attrs);
var validAttributes = this.validAttributes;
var allValid = _.all(attributeNames, function(attributeName) {
return _.contains(validAttributes, attributeName);
});
if (!allValid) {
return new Error("Invalid identity key attribute names");
}
var allPresent = _.all(validAttributes, function(attributeName) {
return _.contains(attributeNames, attributeName);
});
if (!allPresent) {
return new Error("Missing identity key attributes");
}
if (typeof attrs.id !== 'string') {
return new Error("Invalid identity key id");
}
if (!(attrs.publicKey instanceof ArrayBuffer)) {
return new Error("Invalid identity key publicKey");
}
if (typeof attrs.firstUse !== 'boolean') {
return new Error("Invalid identity key firstUse");
}
if (typeof attrs.timestamp !== 'number' || !(attrs.timestamp >= 0)) {
return new Error("Invalid identity key timestamp");
}
if (!validateVerifiedStatus(attrs.verified)) {
return new Error("Invalid identity key verified");
}
if (typeof attrs.nonblockingApproval !== 'boolean') {
return new Error("Invalid identity key nonblockingApproval");
}
}
});
var Group = Model.extend({ storeName: 'groups' });
var Item = Model.extend({ storeName: 'items' });