From 1986291632884afd81aea68a5b633c916b2729d4 Mon Sep 17 00:00:00 2001 From: fpotier Date: Mon, 15 Jun 2026 12:01:07 +0000 Subject: [PATCH 1/2] feat(publish): run validation on create/update --- .../dto/update-published-data.v4.dto.ts | 2 +- .../published-data.v4.controller.ts | 3 + test/PublishedDataV4.js | 61 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/published-data/dto/update-published-data.v4.dto.ts b/src/published-data/dto/update-published-data.v4.dto.ts index ad15dcee9..117c5e727 100644 --- a/src/published-data/dto/update-published-data.v4.dto.ts +++ b/src/published-data/dto/update-published-data.v4.dto.ts @@ -52,7 +52,7 @@ export class UpdatePublishedDataV4Dto { */ @IsObject() @IsOptional() - readonly metadata?: Record; + readonly metadata?: Record = {}; } export class PartialUpdatePublishedDataV4Dto extends PartialType( diff --git a/src/published-data/published-data.v4.controller.ts b/src/published-data/published-data.v4.controller.ts index a8ea40d9d..b5df4f5e3 100644 --- a/src/published-data/published-data.v4.controller.ts +++ b/src/published-data/published-data.v4.controller.ts @@ -101,6 +101,7 @@ export class PublishedDataV4Controller { async create( @Body() createPublishedDataDto: CreatePublishedDataV4Dto, ): Promise { + await this.validatorService.validate(createPublishedDataDto); return this.publishedDataService.create(createPublishedDataDto); } @@ -390,6 +391,7 @@ export class PublishedDataV4Controller { } } + await this.validatorService.validate(updatePublishedDataDto); return this.publishedDataService.update( { doi: id }, updatePublishedDataDto, @@ -715,6 +717,7 @@ export class PublishedDataV4Controller { ); } + await this.validatorService.validate(data); await this.publishedDataService.update({ doi: id }, data); return returnValue; diff --git a/test/PublishedDataV4.js b/test/PublishedDataV4.js index 12c27d3f6..e85ae1c45 100644 --- a/test/PublishedDataV4.js +++ b/test/PublishedDataV4.js @@ -467,4 +467,65 @@ describe("1600: PublishedDataV4: Test of access to published data v4 endpoints", .expect(TestData.NotFoundStatusCode) .expect("Content-Type", /json/); }); + + describe("Ajv extensions are executed on create/save", () => { + const { metadata, ...strippedPublishedData } = publishedData; + const expectedPublicationYear = new Date().getFullYear(); + let id; + + it("should set 'metadata.publicationYear' on create", async () => { + return request(appUrl) + .post("/api/v4/PublishedData") + .send(strippedPublishedData) + .set("Accept", "application/json") + .set({ Authorization: `Bearer ${accessTokenAdminIngestor}` }) + .expect(TestData.EntryCreatedStatusCode) + .expect("Content-Type", /json/) + .then((res) => { + res.body.should.have.property("metadata"); + res.body.metadata.should.have + .property("publicationYear") + .and.equal(expectedPublicationYear); + id = encodeURIComponent(res.body.doi); + }); + }); + + it("should set 'metadata.publicationYear' on patch", async () => { + return request(appUrl) + .patch(`/api/v4/PublishedData/${id}`) + .send(strippedPublishedData) + .set("Accept", "application/json") + .set({ Authorization: `Bearer ${accessTokenAdminIngestor}` }) + .expect(TestData.EntryValidStatusCode) + .expect("Content-Type", /json/) + .then((res) => { + res.body.should.have.property("metadata"); + res.body.metadata.should.have + .property("publicationYear") + .and.equal(expectedPublicationYear); + }); + }); + + it("should set 'metadata.publicationYear' on resync", async () => { + request(appUrl) + .post(`/api/v4/PublishedData/${id}/resync`) + .send(strippedPublishedData) + .set("Accept", "application/json") + .set({ Authorization: `Bearer ${accessTokenAdminIngestor}` }) + .expect(TestData.EntryCreatedStatusCode); + + return request(appUrl) + .get(`/api/v4/PublishedData/${id}`) + .set("Accept", "application/json") + .set({ Authorization: `Bearer ${accessTokenAdminIngestor}` }) + .expect(TestData.EntryValidStatusCode) + .expect("Content-Type", /json/) + .then((res) => { + res.body.should.have.property("metadata"); + res.body.metadata.should.have + .property("publicationYear") + .and.equal(expectedPublicationYear); + }); + }); + }); }); From 860efdad4a3cba59f455db0af735652bb8404457 Mon Sep 17 00:00:00 2001 From: fpotier Date: Tue, 16 Jun 2026 13:00:06 +0000 Subject: [PATCH 2/2] address sourcery comments --- src/published-data/dto/update-published-data.v4.dto.ts | 2 +- test/PublishedDataV4.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/published-data/dto/update-published-data.v4.dto.ts b/src/published-data/dto/update-published-data.v4.dto.ts index 117c5e727..ad15dcee9 100644 --- a/src/published-data/dto/update-published-data.v4.dto.ts +++ b/src/published-data/dto/update-published-data.v4.dto.ts @@ -52,7 +52,7 @@ export class UpdatePublishedDataV4Dto { */ @IsObject() @IsOptional() - readonly metadata?: Record = {}; + readonly metadata?: Record; } export class PartialUpdatePublishedDataV4Dto extends PartialType( diff --git a/test/PublishedDataV4.js b/test/PublishedDataV4.js index e85ae1c45..dbec73f4f 100644 --- a/test/PublishedDataV4.js +++ b/test/PublishedDataV4.js @@ -469,11 +469,11 @@ describe("1600: PublishedDataV4: Test of access to published data v4 endpoints", }); describe("Ajv extensions are executed on create/save", () => { - const { metadata, ...strippedPublishedData } = publishedData; + const strippedPublishedData = { ...publishedData, metadata: {} }; const expectedPublicationYear = new Date().getFullYear(); let id; - it("should set 'metadata.publicationYear' on create", async () => { + it("should set 'metadata.publicationYear' on create", () => { return request(appUrl) .post("/api/v4/PublishedData") .send(strippedPublishedData) @@ -490,7 +490,7 @@ describe("1600: PublishedDataV4: Test of access to published data v4 endpoints", }); }); - it("should set 'metadata.publicationYear' on patch", async () => { + it("should set 'metadata.publicationYear' on patch", () => { return request(appUrl) .patch(`/api/v4/PublishedData/${id}`) .send(strippedPublishedData) @@ -507,7 +507,7 @@ describe("1600: PublishedDataV4: Test of access to published data v4 endpoints", }); it("should set 'metadata.publicationYear' on resync", async () => { - request(appUrl) + await request(appUrl) .post(`/api/v4/PublishedData/${id}/resync`) .send(strippedPublishedData) .set("Accept", "application/json")