The first line of the validateIssue function for (const field in issueFieldType) { should surely be for (const field in issue) { since otherwise you are just iterating over the fields of the global 'schema' object rather than the issue object. Thus, as it stands, no invalid fields in the issue object will ever be removed.
However, by making this change, the function will now fail to ensure that all required fields are included in the received issue object if a required field is entirely absent from the posted issue object. Thus you actually need two loops, one that iterates over the issue object to check for and prune any unwanted fields and another to iterate over the 'schema' object to check that the issue object contains all the required fields. Something along the lines of:-
function validateIssue(issue) {
// Check for and prune any non-schema fields
// from the issue object.
for (const field in issue) {
if (!issueFieldType[field]) {
delete issue[field];
}
}
// Check that all required fields are present
// in the issue object.
for (const field in issueFieldType) {
const type = issueFieldType[field];
if (type === 'required' && !issue[field]) {
return `${field} is required`;
}
}
if (!validIssueStatus[issue.status]) {
return `${issue.status} is not a valid status`;
}
return null;
}
(My inclination now would be to break this out into three functions)
The first line of the validateIssue function
for (const field in issueFieldType) {
should surely befor (const field in issue) {
since otherwise you are just iterating over the fields of the global 'schema' object rather than the issue object. Thus, as it stands, no invalid fields in the issue object will ever be removed.However, by making this change, the function will now fail to ensure that all required fields are included in the received issue object if a required field is entirely absent from the posted issue object. Thus you actually need two loops, one that iterates over the issue object to check for and prune any unwanted fields and another to iterate over the 'schema' object to check that the issue object contains all the required fields. Something along the lines of:-
(My inclination now would be to break this out into three functions)