Skip to content

Commit

Permalink
Merge pull request #3132 from ONSdigital/EAR-2494-suveryID-returning-…
Browse files Browse the repository at this point in the history
…2-errors

EAR-2494 Survey Id returning two errors when supplementary data schema is linked
  • Loading branch information
Farhanam76 authored Nov 21, 2024
2 parents 220ad87 + 9776c56 commit ec6ffa9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
38 changes: 35 additions & 3 deletions eq-author-api/src/validation/customKeywords/validateSurveyId.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const createValidationError = require("../createValidationError");
const {
ERR_SURVEY_ID_MISMATCH,
ERR_INVALID,
ERR_VALID_REQUIRED,
} = require("../../../constants/validationErrorCodes");

module.exports = (ajv) =>
Expand All @@ -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
Expand All @@ -34,9 +65,10 @@ module.exports = (ajv) =>
ERR_SURVEY_ID_MISMATCH
),
];

return false;
} else {
return true;
}

return true;
},
});
14 changes: 12 additions & 2 deletions eq-author-api/src/validation/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(".");
Expand Down Expand Up @@ -151,6 +152,10 @@ describe("schema validation", () => {
],
},
],
supplementaryData: {
id: "supplementary_dataset_schema",
surveyId: "123",
},
metadata: [
{
id: "87c64b20-9662-408b-b674-e2403e90dad3",
Expand Down Expand Up @@ -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);
});
Expand All @@ -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", () => {
Expand Down
20 changes: 1 addition & 19 deletions eq-author-api/src/validation/schemas/questionnaire.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit ec6ffa9

Please sign in to comment.