Skip to content

Commit

Permalink
make presetRef optional, add test with minimal observation without …
Browse files Browse the repository at this point in the history
…`presetRef`
  • Loading branch information
Tomás Ciccola committed Aug 5, 2024
1 parent f81da9a commit dd3122c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion proto/observation/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ message Observation_1 {
}
optional Metadata metadata = 9;

PresetRef presetRef = 10;
optional PresetRef presetRef = 10;

message PresetRef {
bytes docId = 1;
Expand Down
2 changes: 1 addition & 1 deletion schema/observation/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,6 @@
"required": ["docId", "versionId"]
}
},
"required": ["schemaName", "tags", "attachments", "metadata", "presetRef"],
"required": ["schemaName", "tags", "attachments", "metadata"],
"additionalProperties": false
}
17 changes: 8 additions & 9 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ export const convertObservation: ConvertFunction<'observation'> = (
const jsonSchemaCommon = convertCommon(common, versionObj)
let presetRef

if (!rest.presetRef) {
throw new Error('missing presetRef on observation')
}
if (!rest.presetRef.versionId) {
throw new Error('found presetRef on observation but is missing versionId')
}
presetRef = {
docId: rest.presetRef.docId.toString('hex'),
versionId: getVersionId(rest.presetRef.versionId),
if (rest.presetRef) {
if (!rest.presetRef.versionId)
throw new Error('found presetRef on observation but is missing versionId')

presetRef = {
docId: rest.presetRef.docId.toString('hex'),
versionId: getVersionId(rest.presetRef.versionId),
}
}

const obs: Observation = {
Expand Down
12 changes: 8 additions & 4 deletions src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,21 @@ export const convertObservation: ConvertFunction<'observation'> = (
const metadata: Observation_1_Metadata = mapeoDoc.metadata && {
...Observation_1_Metadata.fromPartial(mapeoDoc.metadata),
}
let presetRef
if (mapeoDoc.presetRef) {
presetRef = {
docId: Buffer.from(mapeoDoc.presetRef?.docId, 'hex'),
versionId: parseVersionId(mapeoDoc.presetRef?.versionId),
}
}

return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
attachments,
tags: convertTags(mapeoDoc.tags),
metadata,
presetRef: {
docId: Buffer.from(mapeoDoc.presetRef?.docId, 'hex'),
versionId: parseVersionId(mapeoDoc.presetRef?.versionId),
},
presetRef,
}
}

Expand Down
4 changes: 0 additions & 4 deletions test/fixtures/good-docs-minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ export const goodDocsMinimal = [
attachments: [],
tags: {},
metadata: {},
presetRef: {
docId: cachedValues.refs.docId,
versionId: cachedValues.refs.versionId,
},
deleted: false,
},
expected: {},
Expand Down

0 comments on commit dd3122c

Please sign in to comment.