Apply filtering to object content (value options) nodejs

  filter, javascript, lodash, node.js, object

I’m trying to look through an object, and some attributes, I have limited number of values that they can be assigned to. Here’s how I check for their value at the moment:

    for (const [attribute, value] of Object.entries(params.object)) {
      if(attribute == "accountType")
        if(value != "external" && value != "internal") 
          return { status: `Failed! '${attribute}' must only equal to 'internal' or 'external'` };
      else if(attribute == "dnsName")
        if(value.match('^[a-z]*-[0-9]*$') == null) 
          return { status: `Failed! '${attribute}' must follow the pattern [a-z]*-[0-9]*` };
      else if(attribute == "edition")
        if(value != "starter" && value != "basic" && value != "classic") 
          return { status: `Failed! '${attribute}' must only equal to 'starter', 'basic' or 'classic'` };
      else if(attribute == "forceProvision" || attribute == "installSoltions")
        if (typeof value !== "boolean")
            return { status: `Failed! '${attribute}' must be of type boolean true/false` };
    }

Is there a way to optimize this?

Source: Ask Javascript Questions

LEAVE A COMMENT