title: Validating query parameters for REST api requests in node.js
body: <p>I have an object of valid query parameters for each object type for a GET request to the API.</p>
  
  <pre><code>var queryFields = {
    'organisation': ['limit', 'page', 'id', 'search'],
    'actor': ['limit', 'page', 'id', 'search'],
    'version': ['limit', 'page', 'search'],
    'product': ['limit', 'page', 'search', 'id', 'type', 'brand', 'model', 'manufacturerSpid'],
    'asset': ['limit', 'page', 'search', 'order', 'sort', 'id', 'name', 'currentCustodianSpid', 'currentLocationSpid', 'productSpid', 'search'],
    'location': ['limit', 'page', 'search', 'id'],
    'workorder': ['limit', 'page', 'search', 'id', 'type', 'status', 'requirementSpid', ],
    'move': ['limit', 'page', 'search'],
    'transfer': ['limit', 'page', 'search'],
    'requirement': ['limit', 'page', 'search', 'id', 'type', 'source', 'productSpid', 'status', ],
    'artefact': ['limit', 'page', 'search'],
    'attestation': ['limit', 'page', 'search'],
  };
  </code></pre>
  
  <p>I want to use this function to make sure that only these valid parameters are accepted for a request. Right now the promise resolves <code>false</code> with valid, invalid, or 0 parameters. It seems to be an issue with the way I am filtering. I pass in the object type and the request. If the request has query parameters, I want to grab the valid parameters from the object, and check that the parameters in the <code>req</code> are all valid matches to ones in the object. If there are any that are invalid, I want to resolve <code>false</code>. If there are no parameters, I want to resolve <code>true</code>. If there are only valid parameters, I want to resolve <code>true</code>. Is there some tweaking I can do to this function to get that outcome?</p>
  
  <pre><code>function getQueryFields(object) {
    if (utils.isDefined(queryFields[object])) return queryFields[object];
    return [];
  }
  
  function fieldValidator (objType, req) {
    return new Promise(function(resolve) {
      if (utils.isDefined(req.query)) {
        var fields = getQueryFields(objType);
        //Only resolve true with valid fields
        fields = fields.filter(function(field) { return Object.keys(req.query).indexOf(field) &gt; -1;});
        if (Object.keys(req.query) !== Object.keys(fields)) {
          resolve(false);
        } else {
          resolve (true);
        }
      } else {
        resolve(true);
      }
    });
  }
  </code></pre>