Skip to content

Commit

Permalink
Merge pull request #3127 from ONSdigital/EAR-2394-Validation-for-surv…
Browse files Browse the repository at this point in the history
…eyID-when-SDS-is-linked

Created the validation message and imported to setting page
  • Loading branch information
Farhanam76 authored Sep 19, 2024
2 parents e67185d + bb231f1 commit 086f70d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
2 changes: 2 additions & 0 deletions eq-author-api/constants/validationErrorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -76,4 +77,5 @@ module.exports = {
ERR_VALID_PIPED_ANSWER_REQUIRED,
ERR_UNIQUE_PAGE_DESCRIPTION,
ERR_NO_ANSWERS,
ERR_SURVEY_ID_MISMATCH,
};
1 change: 1 addition & 0 deletions eq-author-api/src/validation/customKeywords/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions eq-author-api/src/validation/customKeywords/validateSurveyId.js
Original file line number Diff line number Diff line change
@@ -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;
},
});
31 changes: 19 additions & 12 deletions eq-author-api/src/validation/schemas/questionnaire.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions eq-author/src/constants/validationMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 086f70d

Please sign in to comment.