From 4d4a0963fe31c463ce1b5cb45a45abe0b229b115 Mon Sep 17 00:00:00 2001 From: Vera Clemens Date: Fri, 20 Nov 2026 11:42:32 +0100 Subject: [PATCH] feat: allow partial updates via updateDataset --- CHANGELOG.md | 2 + docs/useCases.md | 14 +---- .../validators/DatasetResourceValidator.ts | 11 +++- src/datasets/index.ts | 5 +- .../functional/datasets/UpdateDataset.test.ts | 36 ++++++++++++ .../datasets/DatasetResourceValidator.test.ts | 55 +++++++++++++++++++ 6 files changed, 108 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e59cfd4..157e6b3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ This changelog follows the principles of [Keep a Changelog](https://keepachangel ### Fixed +- Datasets: Allow partial metadata updates with `updateDataset` without requiring omitted required fields to be resubmitted. + ### Removed ## [v2.3.0] -- 2026-10-08 diff --git a/docs/useCases.md b/docs/useCases.md index b20f3d1e..f51c5a60 100644 --- a/docs/useCases.md +++ b/docs/useCases.md @@ -1619,9 +1619,9 @@ The use case returns a [CreatedDatasetIdentifiers](../src/datasets/domain/models Updates an existing Dataset, given a [DatasetDTO](../src/datasets/domain/dtos/DatasetDTO.ts) with the updated information. -If a draft of the dataset already exists, the metadata of that draft is overwritten; otherwise, a new draft is created with the updated metadata. +If a draft of the dataset already exists, only the supplied metadata fields are replaced; otherwise, a new draft is created with the updated metadata. Omitted fields retain their existing values, including required fields. Supplied compound fields and arrays replace the entire field value, so include any existing entries you want to keep. -This use case validates the submitted fields of each metadata block and can return errors of type [ResourceValidationError](../src/core/domain/useCases/validators/errors/ResourceValidationError.ts), which include sufficient information to determine which field value is invalid and why. +This use case validates only the submitted fields of each metadata block and can return errors of type [ResourceValidationError](../src/core/domain/useCases/validators/errors/ResourceValidationError.ts), which include sufficient information to determine which field value is invalid and why. ##### Example call: @@ -1652,15 +1652,7 @@ const datasetDTO: DatasetDTO = { datasetContactEmail: 'johndoe@dataverse.com', datasetContactName: 'John' } - ], - dsDescription: [ - { - dsDescriptionValue: 'This is the description of our new dataset' - } - ], - subject: 'Earth and Environmental Sciences' - - /* Rest of field values... */ + ] } } ] diff --git a/src/datasets/domain/useCases/validators/DatasetResourceValidator.ts b/src/datasets/domain/useCases/validators/DatasetResourceValidator.ts index 77282da7..5686a4a9 100644 --- a/src/datasets/domain/useCases/validators/DatasetResourceValidator.ts +++ b/src/datasets/domain/useCases/validators/DatasetResourceValidator.ts @@ -4,7 +4,10 @@ import { MetadataBlock } from '../../../../metadataBlocks' import { BaseMetadataFieldValidator } from './BaseMetadataFieldValidator' export class DatasetResourceValidator implements ResourceValidator { - constructor(private metadataFieldValidator: BaseMetadataFieldValidator) {} + constructor( + private metadataFieldValidator: BaseMetadataFieldValidator, + private onlyValidateProvidedFields = false + ) {} public validate(resource: DatasetDTO, metadataBlocks: MetadataBlock[]) { for (const metadataBlockValues of resource.metadataBlockValues) { @@ -22,6 +25,12 @@ export class DatasetResourceValidator implements ResourceValidator { ) as MetadataBlock for (const metadataFieldKey of Object.keys(metadataBlock.metadataFields)) { + if ( + this.onlyValidateProvidedFields && + !Object.prototype.hasOwnProperty.call(metadataBlockValues.fields, metadataFieldKey) + ) { + continue + } this.metadataFieldValidator.validate({ metadataFieldInfo: metadataBlock.metadataFields[metadataFieldKey], metadataFieldKey: metadataFieldKey, diff --git a/src/datasets/index.ts b/src/datasets/index.ts index faca94e1..01694fa6 100644 --- a/src/datasets/index.ts +++ b/src/datasets/index.ts @@ -71,16 +71,15 @@ const metadataFieldValidator = new MetadataFieldValidator( ) const publishDataset = new PublishDataset(datasetsRepository) const metadataBlocksRepository = new MetadataBlocksRepository() -const datasetResourceValidator = new DatasetResourceValidator(metadataFieldValidator) const createDataset = new CreateDataset( datasetsRepository, metadataBlocksRepository, - datasetResourceValidator + new DatasetResourceValidator(metadataFieldValidator) ) const updateDataset = new UpdateDataset( datasetsRepository, metadataBlocksRepository, - datasetResourceValidator + new DatasetResourceValidator(metadataFieldValidator, true) ) const deaccessionDataset = new DeaccessionDataset(datasetsRepository) const getDatasetDownloadCount = new GetDatasetDownloadCount(datasetsRepository) diff --git a/test/functional/datasets/UpdateDataset.test.ts b/test/functional/datasets/UpdateDataset.test.ts index a13cdad6..eaed73a3 100644 --- a/test/functional/datasets/UpdateDataset.test.ts +++ b/test/functional/datasets/UpdateDataset.test.ts @@ -7,6 +7,7 @@ import { import { ApiConfig } from '../../../src' import { TestConstants } from '../../testHelpers/TestConstants' import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' +import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper' import { DatasetDescription } from '../../../src/datasets/domain/models/Dataset' describe('execute', () => { @@ -71,4 +72,39 @@ describe('execute', () => { .dsDescriptionValue ).toEqual(updatedDsDescription) }) + + test('should update only the supplied fields, and preserve omitted fields', async () => { + const createdDataset = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO) + + try { + const originalDataset = await getDataset.execute(createdDataset.numericId) + const originalCitation = originalDataset.metadataBlocks.find( + (block) => block.name === 'citation' + ) + const updatedTitle = 'Title updated without resending other required fields' + + await expect( + updateDataset.execute(createdDataset.numericId, { + metadataBlockValues: [ + { + name: 'citation', + fields: { title: updatedTitle } + } + ] + }) + ).resolves.toBeUndefined() + + const updatedDataset = await getDataset.execute(createdDataset.numericId) + const updatedCitation = updatedDataset.metadataBlocks.find( + (block) => block.name === 'citation' + ) + expect(originalCitation).toBeDefined() + expect(updatedCitation?.fields).toEqual({ + ...originalCitation?.fields, + title: updatedTitle + }) + } finally { + await deleteUnpublishedDatasetViaApi(createdDataset.numericId) + } + }) }) diff --git a/test/unit/datasets/DatasetResourceValidator.test.ts b/test/unit/datasets/DatasetResourceValidator.test.ts index c83bb241..9dd6500b 100644 --- a/test/unit/datasets/DatasetResourceValidator.test.ts +++ b/test/unit/datasets/DatasetResourceValidator.test.ts @@ -235,4 +235,59 @@ describe('validate', () => { 0 ) }) + + describe('when validating a partial update', () => { + const partialUpdateValidator = new DatasetResourceValidator(metadataFieldValidator, true) + const partialUpdate = ( + fields: DatasetDTO['metadataBlockValues'][number]['fields'] + ): DatasetDTO => ({ + metadataBlockValues: [{ name: 'citation', fields }] + }) + + test('should allow omitted required fields', () => { + expect(() => + partialUpdateValidator.validate( + partialUpdate({ title: 'Updated title' }), + testMetadataBlocks + ) + ).not.toThrow() + }) + + test.each(['', ' '])('should reject a supplied empty required title: %p', (title) => { + expect(() => + partialUpdateValidator.validate(partialUpdate({ title: title }), testMetadataBlocks) + ).toThrow( + 'There was an error when validating the field title from metadata block citation. Reason was: The field should not be empty.' + ) + }) + + test('should reject an invalid supplied field type', () => { + expect(() => + partialUpdateValidator.validate( + partialUpdate({ title: ['Invalid title'] }), + testMetadataBlocks + ) + ).toThrow('Expecting a single field, not an array.') + }) + + test('should require mandatory children of supplied compound fields', () => { + expect(() => + partialUpdateValidator.validate( + partialUpdate({ author: [{ authorAffiliation: 'Dataverse.org' }] }), + testMetadataBlocks + ) + ).toThrow( + 'There was an error when validating the field authorName from metadata block citation with parent field author in position 0. Reason was: The field should not be empty.' + ) + }) + + test('should allow a valid supplied compound field without other required fields', () => { + expect(() => + partialUpdateValidator.validate( + partialUpdate({ author: [{ authorName: 'Updated author' }] }), + testMetadataBlocks + ) + ).not.toThrow() + }) + }) })