From af7910dca31992f2617c169812675b09acf29cd0 Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Tue, 5 Nov 2024 14:35:56 +0000 Subject: [PATCH 1/5] fixed the bug for returning two errors, updated the validateSurveyId --- .../customKeywords/validateSurveyId.js | 38 ++++++++++++++++++- .../src/validation/schemas/questionnaire.json | 20 +--------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/eq-author-api/src/validation/customKeywords/validateSurveyId.js b/eq-author-api/src/validation/customKeywords/validateSurveyId.js index c3853d86ad..090000320e 100644 --- a/eq-author-api/src/validation/customKeywords/validateSurveyId.js +++ b/eq-author-api/src/validation/customKeywords/validateSurveyId.js @@ -1,6 +1,8 @@ const createValidationError = require("../createValidationError"); const { ERR_SURVEY_ID_MISMATCH, + ERR_INVALID, + ERR_VALID_REQUIRED, } = require("../../../constants/validationErrorCodes"); module.exports = (ajv) => @@ -35,8 +37,40 @@ module.exports = (ajv) => ), ]; return false; - } + } else { + if ( + typeof questionnaire.surveyId === "string" && + questionnaire.surveyId.length > 0 && + !questionnaire.surveyId.match(/^\d{3}$/) + ) { + isValid.errors = [ + createValidationError( + instancePath, + fieldName, + ERR_INVALID, + questionnaire, + ERR_INVALID + ), + ]; + return false; + } else if ( + typeof questionnaire.surveyId === "string" && + questionnaire.surveyId.length === 0 + ) { + isValid.errors = [ + createValidationError( + instancePath, + fieldName, + ERR_VALID_REQUIRED, + questionnaire, + ERR_VALID_REQUIRED + ), + ]; + + return false; + } - return true; + return true; + } }, }); diff --git a/eq-author-api/src/validation/schemas/questionnaire.json b/eq-author-api/src/validation/schemas/questionnaire.json index 85b46ca871..acbbad80b2 100644 --- a/eq-author-api/src/validation/schemas/questionnaire.json +++ b/eq-author-api/src/validation/schemas/questionnaire.json @@ -26,25 +26,7 @@ "$ref": "submission.json" }, "surveyId": { - "allOf": [ - { - "if": { - "type": "string", - "minLength": 1 - }, - "then": { - "type": "string", - "pattern": "^\\d{3}$", - "errorMessage": "ERR_INVALID" - }, - "else": { - "$ref": "definitions.json#/definitions/populatedString" - } - }, - { - "validateSurveyId": true - } - ] + "validateSurveyId": true }, "formType": { "if": { From 46e0ee2dc0c8c6c675535fa25fd2c79be66ae172 Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Tue, 5 Nov 2024 14:53:10 +0000 Subject: [PATCH 2/5] updated the test to change from null to empty string. --- eq-author-api/src/validation/index.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eq-author-api/src/validation/index.test.js b/eq-author-api/src/validation/index.test.js index 914fb448b9..1814b4a6e5 100644 --- a/eq-author-api/src/validation/index.test.js +++ b/eq-author-api/src/validation/index.test.js @@ -198,7 +198,7 @@ describe("schema validation", () => { describe("Themes validation", () => { it("should return an error if survey ID missing", () => { - questionnaire.surveyId = null; + questionnaire.surveyId = ""; const errors = validation(questionnaire); expect(errors[0].errorCode).toBe(ERR_VALID_REQUIRED); }); @@ -210,7 +210,7 @@ describe("schema validation", () => { }); it("should return an error if survey ID is missing on a social survey", () => { - questionnaire.surveyId = null; + questionnaire.surveyId = ""; questionnaire.type = "Social"; const errors = validation(questionnaire); expect(errors[0].errorCode).toBe(ERR_VALID_REQUIRED); From ae96d3492aa6228c2ec5152ea54b4a52e2358527 Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Tue, 12 Nov 2024 08:00:01 +0000 Subject: [PATCH 3/5] update the code to show the error messages in the correct order --- .../customKeywords/validateSurveyId.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/eq-author-api/src/validation/customKeywords/validateSurveyId.js b/eq-author-api/src/validation/customKeywords/validateSurveyId.js index 090000320e..dda88455c8 100644 --- a/eq-author-api/src/validation/customKeywords/validateSurveyId.js +++ b/eq-author-api/src/validation/customKeywords/validateSurveyId.js @@ -21,19 +21,17 @@ module.exports = (ajv) => // Get the supplementary data from the questionnaire object const supplementaryData = questionnaire.supplementaryData; - // If supplementaryData exists and contains a surveyId, and supplementaryData's surveyId doesn't match the questionnaire's surveyId, throw a validation error if ( - supplementaryData && - supplementaryData.surveyId && - questionnaireSurveyId !== supplementaryData.surveyId + typeof questionnaire.surveyId === "string" && + questionnaire.surveyId.length === 0 ) { isValid.errors = [ createValidationError( instancePath, fieldName, - ERR_SURVEY_ID_MISMATCH, + ERR_VALID_REQUIRED, questionnaire, - ERR_SURVEY_ID_MISMATCH + ERR_VALID_REQUIRED ), ]; return false; @@ -53,17 +51,19 @@ module.exports = (ajv) => ), ]; return false; + // If supplementaryData exists and contains a surveyId, and supplementaryData's surveyId doesn't match the questionnaire's surveyId, throw a validation error } else if ( - typeof questionnaire.surveyId === "string" && - questionnaire.surveyId.length === 0 + supplementaryData && + supplementaryData.surveyId && + questionnaireSurveyId !== supplementaryData.surveyId ) { isValid.errors = [ createValidationError( instancePath, fieldName, - ERR_VALID_REQUIRED, + ERR_SURVEY_ID_MISMATCH, questionnaire, - ERR_VALID_REQUIRED + ERR_SURVEY_ID_MISMATCH ), ]; From 7b006d83d6ebb81ccc4771342fa6377e20b88f2b Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Fri, 15 Nov 2024 13:39:28 +0000 Subject: [PATCH 4/5] added test to test the survey id mismatch when supplementary data is linked --- eq-author-api/src/validation/index.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/eq-author-api/src/validation/index.test.js b/eq-author-api/src/validation/index.test.js index 1814b4a6e5..7038dbf4fc 100644 --- a/eq-author-api/src/validation/index.test.js +++ b/eq-author-api/src/validation/index.test.js @@ -38,6 +38,7 @@ const { CALCSUM_MOVED, ERR_SEC_CONDITION_NOT_SELECTED, ERR_COUNT_OF_GREATER_THAN_AVAILABLE_OPTIONS, + ERR_SURVEY_ID_MISMATCH, } = require("../../constants/validationErrorCodes"); const validation = require("."); @@ -151,6 +152,10 @@ describe("schema validation", () => { ], }, ], + supplementaryData: { + id: "supplementary_dataset_schema", + surveyId: "123", + }, metadata: [ { id: "87c64b20-9662-408b-b674-e2403e90dad3", @@ -215,6 +220,11 @@ describe("schema validation", () => { const errors = validation(questionnaire); expect(errors[0].errorCode).toBe(ERR_VALID_REQUIRED); }); + it("should return an error if survey ID does not match with supplementary dataset schema’s survey ID", () => { + questionnaire.surveyId = "111"; + const errors = validation(questionnaire); + expect(errors[0].errorCode).toBe(ERR_SURVEY_ID_MISMATCH); + }); }); describe("Question page validation", () => { From 9776c56fdc725fe35875ed437f66939a6694c2c6 Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Thu, 21 Nov 2024 14:05:13 +0000 Subject: [PATCH 5/5] fixed the conditional statement arrangements --- .../customKeywords/validateSurveyId.js | 66 +++++++++---------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/eq-author-api/src/validation/customKeywords/validateSurveyId.js b/eq-author-api/src/validation/customKeywords/validateSurveyId.js index dda88455c8..5d97110391 100644 --- a/eq-author-api/src/validation/customKeywords/validateSurveyId.js +++ b/eq-author-api/src/validation/customKeywords/validateSurveyId.js @@ -35,41 +35,39 @@ module.exports = (ajv) => ), ]; return false; - } else { - if ( - typeof questionnaire.surveyId === "string" && - questionnaire.surveyId.length > 0 && - !questionnaire.surveyId.match(/^\d{3}$/) - ) { - isValid.errors = [ - createValidationError( - instancePath, - fieldName, - ERR_INVALID, - questionnaire, - ERR_INVALID - ), - ]; - return false; - // If supplementaryData exists and contains a surveyId, and supplementaryData's surveyId doesn't match the questionnaire's surveyId, throw a validation error - } else if ( - supplementaryData && - supplementaryData.surveyId && - questionnaireSurveyId !== supplementaryData.surveyId - ) { - isValid.errors = [ - createValidationError( - instancePath, - fieldName, - ERR_SURVEY_ID_MISMATCH, - questionnaire, - ERR_SURVEY_ID_MISMATCH - ), - ]; - - return false; - } + } else if ( + typeof questionnaire.surveyId === "string" && + questionnaire.surveyId.length > 0 && + !questionnaire.surveyId.match(/^\d{3}$/) + ) { + isValid.errors = [ + createValidationError( + instancePath, + fieldName, + ERR_INVALID, + questionnaire, + ERR_INVALID + ), + ]; + return false; + // If supplementaryData exists and contains a surveyId, and supplementaryData's surveyId doesn't match the questionnaire's surveyId, throw a validation error + } else if ( + supplementaryData && + supplementaryData.surveyId && + questionnaireSurveyId !== supplementaryData.surveyId + ) { + isValid.errors = [ + createValidationError( + instancePath, + fieldName, + ERR_SURVEY_ID_MISMATCH, + questionnaire, + ERR_SURVEY_ID_MISMATCH + ), + ]; + return false; + } else { return true; } },