diff --git a/eq-author-api/src/validation/customKeywords/validateSurveyId.js b/eq-author-api/src/validation/customKeywords/validateSurveyId.js index c3853d86ad..5d97110391 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) => @@ -19,8 +21,37 @@ 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 ( + typeof questionnaire.surveyId === "string" && + questionnaire.surveyId.length === 0 + ) { + isValid.errors = [ + createValidationError( + instancePath, + fieldName, + ERR_VALID_REQUIRED, + questionnaire, + ERR_VALID_REQUIRED + ), + ]; + 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 @@ -34,9 +65,10 @@ module.exports = (ajv) => ERR_SURVEY_ID_MISMATCH ), ]; + return false; + } else { + return true; } - - return true; }, }); diff --git a/eq-author-api/src/validation/index.test.js b/eq-author-api/src/validation/index.test.js index 914fb448b9..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", @@ -198,7 +203,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,11 +215,16 @@ 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); }); + 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", () => { 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": {