diff --git a/proto/observation/v1.proto b/proto/observation/v1.proto index 02e45ca..d013fe0 100644 --- a/proto/observation/v1.proto +++ b/proto/observation/v1.proto @@ -68,7 +68,7 @@ message Observation_1 { } optional Metadata metadata = 9; - PresetRef presetRef = 10; + optional PresetRef presetRef = 10; message PresetRef { bytes docId = 1; diff --git a/schema/observation/v1.json b/schema/observation/v1.json index 044d168..04d3d6e 100644 --- a/schema/observation/v1.json +++ b/schema/observation/v1.json @@ -197,6 +197,6 @@ "required": ["docId", "versionId"] } }, - "required": ["schemaName", "tags", "attachments", "metadata", "presetRef"], + "required": ["schemaName", "tags", "attachments", "metadata"], "additionalProperties": false } diff --git a/src/lib/decode-conversions.ts b/src/lib/decode-conversions.ts index 02be213..1b9aed1 100644 --- a/src/lib/decode-conversions.ts +++ b/src/lib/decode-conversions.ts @@ -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 = { diff --git a/src/lib/encode-conversions.ts b/src/lib/encode-conversions.ts index 4b270f9..df54f09 100644 --- a/src/lib/encode-conversions.ts +++ b/src/lib/encode-conversions.ts @@ -105,6 +105,13 @@ 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), @@ -112,10 +119,7 @@ export const convertObservation: ConvertFunction<'observation'> = ( attachments, tags: convertTags(mapeoDoc.tags), metadata, - presetRef: { - docId: Buffer.from(mapeoDoc.presetRef?.docId, 'hex'), - versionId: parseVersionId(mapeoDoc.presetRef?.versionId), - }, + presetRef, } } diff --git a/test/fixtures/good-docs-minimal.js b/test/fixtures/good-docs-minimal.js index 6d72a84..c5708bf 100644 --- a/test/fixtures/good-docs-minimal.js +++ b/test/fixtures/good-docs-minimal.js @@ -24,10 +24,6 @@ export const goodDocsMinimal = [ attachments: [], tags: {}, metadata: {}, - presetRef: { - docId: cachedValues.refs.docId, - versionId: cachedValues.refs.versionId, - }, deleted: false, }, expected: {},