Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 3 additions & 11 deletions docs/useCases.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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... */
]
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/datasets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions test/functional/datasets/UpdateDataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
}
})
})
55 changes: 55 additions & 0 deletions test/unit/datasets/DatasetResourceValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
Loading