Skip to content

Commit

Permalink
chore!: improve decoding of required fields (#254)
Browse files Browse the repository at this point in the history
A required field, confusingly, should probably be marked *optional* in
the protobuf. That way, we can throw errors when decoding. See [#252]
for more details.

This change updates how we handle required fields across the schema.

I tested this end-to-end, going "all the way" to comapeo-mobile.

[#252]: https://github.com/digidem/mapeo-schema/issues/252

Co-authored-by: Gregor MacLennan <gmaclennan@digital-democracy.org>
  • Loading branch information
EvanHahn and gmaclennan authored Sep 16, 2024
1 parent 3f44efb commit 1c88ee3
Show file tree
Hide file tree
Showing 19 changed files with 192 additions and 131 deletions.
2 changes: 1 addition & 1 deletion proto/deviceInfo/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ message DeviceInfo_1 {
}

string name = 5;
optional DeviceType deviceType = 6;
DeviceType deviceType = 6;
}
2 changes: 1 addition & 1 deletion proto/field/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ message Field_1 {
selectOne = 3;
selectMultiple = 4;
}
Type type = 6 [(required) = true];
Type type = 6;
string label = 7 [(required) = true];
enum Appearance {
appearance_unspecified = 0;
Expand Down
2 changes: 1 addition & 1 deletion proto/icon/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ message Icon_1 {
option (schemaName) = "icon";

Common_1 common = 1;
string name = 2 [(required) = true];
string name = 2;

message IconVariantSvg {}

Expand Down
4 changes: 2 additions & 2 deletions proto/observation/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ message Observation_1 {
audio = 3;
}
message Attachment {
bytes driveDiscoveryId = 1;
string name = 2;
bytes driveDiscoveryId = 1 [(required) = true];
string name = 2 [(required) = true];
AttachmentType type = 3;
bytes hash = 4;
}
Expand Down
2 changes: 1 addition & 1 deletion proto/preset/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ message Preset_1 {
repeated FieldRef fieldRefs = 10;
optional IconRef iconRef = 11;
repeated string terms = 12;
string color = 13;
optional string color = 13;

enum Geometry {
geometry_unspecified = 0;
Expand Down
4 changes: 2 additions & 2 deletions proto/role/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ message Role_1 {

Common_1 common = 1;

bytes roleId = 5;
uint32 fromIndex = 6;
bytes roleId = 5 [(required) = true];
optional uint32 fromIndex = 6 [(required) = true];
}
6 changes: 3 additions & 3 deletions proto/translation/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ message Translation_1 {
Common_1 common = 1;
DocRef docRef = 2;

string propertyRef = 3;
string languageCode = 4;
string regionCode = 5;
string propertyRef = 3 [(required) = true];
string languageCode = 4 [(required) = true];
optional string regionCode = 5;
string message = 6;

message DocRef {
Expand Down
12 changes: 8 additions & 4 deletions schema/common/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
"properties": {
"docId": {
"description": "Hex-encoded 32-byte buffer",
"type": "string"
"type": "string",
"minLength": 1
},
"versionId": {
"description": "core discovery id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"type": "string"
"type": "string",
"minLength": 1
},
"originalVersionId": {
"description": "Version ID of the original version of this document. For the original version, matches `versionId`.",
"type": "string"
"type": "string",
"minLength": 1
},
"schemaName": {
"description": "Name of Mapeo data type / schema",
"type": "string"
"type": "string",
"minLength": 1
},
"createdAt": {
"description": "RFC3339-formatted datetime of when the first version of the element was created",
Expand Down
2 changes: 1 addition & 1 deletion schema/deviceInfo/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"description": "Type of device"
}
},
"required": ["schemaName", "name"],
"required": ["schemaName", "name", "deviceType"],
"additionalProperties": false
}
9 changes: 6 additions & 3 deletions schema/field/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"tagKey": {
"description": "They key in a tags object that this field applies to",
"type": "string"
"type": "string",
"minLength": 1
},
"type": {
"description": "Type of field - defines how the field is displayed to the user.",
Expand All @@ -35,7 +36,8 @@
},
"label": {
"description": "Default language label for the form field label",
"type": "string"
"type": "string",
"minLength": 1
},
"appearance": {
"description": "For text fields, display as a single-line or multi-line field",
Expand Down Expand Up @@ -66,7 +68,8 @@
"type": "object",
"properties": {
"label": {
"type": "string"
"type": "string",
"minLength": 1
},
"value": {
"anyOf": [
Expand Down
6 changes: 4 additions & 2 deletions schema/icon/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"blobVersionId": {
"description": "Version id of the icon blob. Each id is id (hex-encoded 32 byte buffer) and index number, separated by '/'",
"type": "string"
"type": "string",
"minLength": 1
}
},
"properties": {
Expand All @@ -21,7 +22,8 @@
"const": "icon"
},
"name": {
"type": "string"
"type": "string",
"minLength": 1
},
"variants": {
"type": "array",
Expand Down
15 changes: 10 additions & 5 deletions schema/observation/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@
"properties": {
"driveDiscoveryId": {
"type": "string",
"description": "core discovery id for the drive that the attachment belongs to"
"description": "core discovery id for the drive that the attachment belongs to",
"minLength": 1
},
"name": {
"type": "string",
"description": "name of the attachment"
"description": "name of the attachment",
"minLength": 1
},
"type": {
"type": "string",
Expand All @@ -99,7 +101,8 @@
},
"hash": {
"type": "string",
"description": "SHA256 hash of the attachment"
"description": "SHA256 hash of the attachment",
"minLength": 1
}
},
"required": ["driveDiscoveryId", "name", "type", "hash"]
Expand Down Expand Up @@ -192,11 +195,13 @@
"properties": {
"docId": {
"description": "hex-encoded id of the element that this observation references",
"type": "string"
"type": "string",
"minLength": 1
},
"versionId": {
"description": "core discovery id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"type": "string"
"type": "string",
"minLength": 1
}
},
"required": ["docId", "versionId"]
Expand Down
15 changes: 9 additions & 6 deletions schema/preset/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@
"properties": {
"docId": {
"description": "hex-encoded id of the element that this observation references",
"type": "string"
"type": "string",
"minLength": 1
},
"versionId": {
"description": "core discovery id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"type": "string"
"type": "string",
"minLength": 1
}
},
"required": ["docId", "versionId"]
Expand All @@ -100,11 +102,13 @@
"properties": {
"docId": {
"description": "hex-encoded id of the element that this observation references",
"type": "string"
"type": "string",
"minLength": 1
},
"versionId": {
"description": "core discovery id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"type": "string"
"type": "string",
"minLength": 1
}
},
"required": ["docId", "versionId"]
Expand All @@ -130,8 +134,7 @@
"removeTags",
"fieldRefs",
"schemaName",
"terms",
"color"
"terms"
],
"additionalProperties": false
}
3 changes: 2 additions & 1 deletion schema/role/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"roleId": {
"type": "string",
"description": "Unique identifier for role assigned to device with auth core ID equal to `docId` of this record"
"description": "Unique identifier for role assigned to device with auth core ID equal to `docId` of this record",
"minLength": 1
},
"fromIndex": {
"type": "integer",
Expand Down
6 changes: 4 additions & 2 deletions schema/track/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@
"properties": {
"docId": {
"description": "hex-encoded id of the element that this track references",
"type": "string"
"type": "string",
"minLength": 1
},
"versionId": {
"description": "core discovery id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"type": "string"
"type": "string",
"minLength": 1
}
},
"required": ["docId", "versionId"]
Expand Down
18 changes: 12 additions & 6 deletions schema/translation/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
"properties": {
"docId": {
"description": "hex-encoded id of the element that this observation references",
"type": "string"
"type": "string",
"minLength": 1
},
"versionId": {
"description": "core discovery id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"type": "string"
"type": "string",
"minLength": 1
}
},
"required": ["docId", "versionId"]
Expand All @@ -40,15 +42,20 @@
},
"propertyRef": {
"type": "string",
"description": "identifier for translated field/property in dot-prop notation"
"description": "identifier for translated field/property in dot-prop notation",
"minLength": 1
},
"languageCode": {
"type": "string",
"description": "three-letter ISO 169-3 language code"
"description": "three-letter ISO 169-3 language code",
"minLength": 1,
"maxLength": 3
},
"regionCode": {
"type": "string",
"description": "two-letter country code from ISO 3166-1 alpha-2 or a three-digit code from UN M.49 for geographical regions"
"description": "two-letter country code from ISO 3166-1 alpha-2 or a three-digit code from UN M.49 for geographical regions",
"minLength": 2,
"maxLength": 3
},
"message": {
"type": "string",
Expand All @@ -61,7 +68,6 @@
"docRefType",
"propertyRef",
"languageCode",
"regionCode",
"message"
],
"additionalProperties": false
Expand Down
Loading

0 comments on commit 1c88ee3

Please sign in to comment.