vasansr / pro-mern-stack

Code Listing for the book Pro MERN Stack
http://www.apress.com/in/book/9781484226520
343 stars 159 forks source link

chapter 5 - error handling - validateIssue function #53

Open MSKhodadady opened 6 years ago

MSKhodadady commented 6 years ago

If we see this part of validateIssue function:

for (const field in issueFieldType) {
    const type = issueFieldType[field];
    if (!type) {
      delete issue[field];
    } else if (type === 'required' && !issue[field]) {
      return `${field} is required.`;
    }
  }

we see that the if (!type) is redundant. because every type in issueFieldType is not undefind . I think we should write issue instead of issueFieldType in the for statement:

for (const field in issue) {
    const type = issueFieldType[field];
    if (!type) {
      delete issue[field];
    } else if (type === 'required' && !issue[field]) {
      return `${field} is required.`;
    }
  }

The resulted function is:

function validateIssue(issue) {
  for (const field in issue) {
    const type = issueFieldType[field];
    if (!type) {
      delete issue[field];
    } else if (type === 'required' && !issue[field]) {
      return `${field} is required.`;
    }
  }
  if (!validIssueStatus[issue.status])
    return `${issue.status} is not a valid status.`;
  return null;
}
edusantana commented 5 years ago

I think it was an error, instead of:

for (const field in issueFieldType) {

it should be:

for(const field in issue){