diff --git a/eq-author-api/constants/validationErrorCodes.js b/eq-author-api/constants/validationErrorCodes.js index 1b549dd0a4..880f769b32 100644 --- a/eq-author-api/constants/validationErrorCodes.js +++ b/eq-author-api/constants/validationErrorCodes.js @@ -38,6 +38,7 @@ const ERR_COUNT_OF_GREATER_THAN_AVAILABLE_OPTIONS = const ERR_VALID_PIPED_ANSWER_REQUIRED = "ERR_VALID_PIPED_ANSWER_REQUIRED"; const ERR_UNIQUE_PAGE_DESCRIPTION = "ERR_UNIQUE_PAGE_DESCRIPTION"; const ERR_NO_ANSWERS = "ERR_NO_ANSWERS"; +const ERR_SURVEY_ID_MISMATCH = "ERR_SURVEY_ID_MISMATCH"; module.exports = { ERR_INVALID, @@ -76,4 +77,5 @@ module.exports = { ERR_VALID_PIPED_ANSWER_REQUIRED, ERR_UNIQUE_PAGE_DESCRIPTION, ERR_NO_ANSWERS, + ERR_SURVEY_ID_MISMATCH, }; diff --git a/eq-author-api/src/validation/customKeywords/index.js b/eq-author-api/src/validation/customKeywords/index.js index 4c325b9007..ec77ba8e42 100644 --- a/eq-author-api/src/validation/customKeywords/index.js +++ b/eq-author-api/src/validation/customKeywords/index.js @@ -5,6 +5,7 @@ module.exports = (ajv) => { require("./calculatedSummaryPosition")(ajv); require("./validateLatestAfterEarliest")(ajv); require("./validateDuration")(ajv); + require("./validateSurveyId")(ajv); require("./validateMultipleChoiceCondition")(ajv); require("./validateExpression")(ajv); require("./validateRoutingDestination")(ajv); diff --git a/eq-author-api/src/validation/customKeywords/validateSurveyId.js b/eq-author-api/src/validation/customKeywords/validateSurveyId.js new file mode 100644 index 0000000000..c3853d86ad --- /dev/null +++ b/eq-author-api/src/validation/customKeywords/validateSurveyId.js @@ -0,0 +1,42 @@ +const createValidationError = require("../createValidationError"); +const { + ERR_SURVEY_ID_MISMATCH, +} = require("../../../constants/validationErrorCodes"); + +module.exports = (ajv) => + ajv.addKeyword({ + keyword: "validateSurveyId", + validate: function isValid( + _schema, + questionnaireSurveyId, // gives the data entered into the survey ID field + _parentSchema, + { + instancePath, // gives the path /surveyId + rootData: questionnaire, // gives the whole questionnaire object + parentDataProperty: fieldName, // gives the field name surveyId + } + ) { + // 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 + ) { + isValid.errors = [ + createValidationError( + instancePath, + fieldName, + ERR_SURVEY_ID_MISMATCH, + questionnaire, + ERR_SURVEY_ID_MISMATCH + ), + ]; + return false; + } + + return true; + }, + }); diff --git a/eq-author-api/src/validation/schemas/questionnaire.json b/eq-author-api/src/validation/schemas/questionnaire.json index 77a6bde3d1..85b46ca871 100644 --- a/eq-author-api/src/validation/schemas/questionnaire.json +++ b/eq-author-api/src/validation/schemas/questionnaire.json @@ -26,18 +26,25 @@ "$ref": "submission.json" }, "surveyId": { - "if": { - "type": "string", - "minLength": 1 - }, - "then": { - "type": "string", - "pattern": "^\\d{3}$", - "errorMessage": "ERR_INVALID" - }, - "else": { - "$ref": "definitions.json#/definitions/populatedString" - } + "allOf": [ + { + "if": { + "type": "string", + "minLength": 1 + }, + "then": { + "type": "string", + "pattern": "^\\d{3}$", + "errorMessage": "ERR_INVALID" + }, + "else": { + "$ref": "definitions.json#/definitions/populatedString" + } + }, + { + "validateSurveyId": true + } + ] }, "formType": { "if": { diff --git a/eq-author/src/constants/validationMessages.js b/eq-author/src/constants/validationMessages.js index 5c8013063a..c305e700b6 100644 --- a/eq-author/src/constants/validationMessages.js +++ b/eq-author/src/constants/validationMessages.js @@ -346,6 +346,8 @@ export const destinationErrors = { export const SURVEY_ID_ERRORS = { ERR_VALID_REQUIRED: "Enter a survey ID", ERR_INVALID: "Enter a survey ID in the correct format", + ERR_SURVEY_ID_MISMATCH: + "The survey ID does not match the linked supplementary data schema", }; export const FORM_TYPE_ERRORS = {