2

I am writing a validation method to make sure the required nested keys are present in data before calling another method that performs calculations using the data object. Here is the code thus far for the validation method -

function checkInputDataForMissingNestedKeys(data) {
  const parentKeys = [
    'initialDistance',
    'finalDistance'
  ];

  const nestedKeys = [
    'value',
    'uom'
  ];

  let result = {};

  // if parentKey exists in data, then check for nestedKeys
  parentKeys.forEach(function(entry) {
    if(data[entry] !== undefined) {
      let i = 0;
      nestedKeys.forEach(function(element) {
        if(data[entry][element] === undefined) result[entry][i++] = element;
      })
    }
  });

  return (result.constructor === Object && Object.entries(result).length === 0) ? result : { "missing nested keys": result };
}

As written, an error is being encountered: "TypeError: undefined is not an object (evaluating 'result[entry][i++] = element')". I think it's broken due to result[entry][i++] = element; because when that part is changed to result[i++] = element; then the error goes away BUT overwriting of results occurs.

For testing purposes data['initialDistance'] and data['finalDistance] are both missing the nested key uom so the desired result of this, if outputted to the console (the formatting may not be flawless because it was written manually), is -

Object {
  "missing nested keys": Object {
     "initialDistance": Object {
        "0": "uom"
      },
     "finalDistance": Object {
        "0": "uom"
      },
   }
}

When result[i++] = element; is plugged in for experimentation then the console output looks like this (here again, typed out manually) -

Object {
  "missing nested keys": Object {
    "0": "uom"
  }    
}

What needs to be changed so that "missing nested keys" includes the values from entry?

2 Answers 2

1

Forget your i variable. This is tripping you up

parentKeys.forEach(function(entry) { if(data[entry] !== undefined) {

  nestedKeys.forEach(function(element) {
    if(data[entry][element] === undefined) data[entry][element] = "";
  })
}

});

Sign up to request clarification or add additional context in comments.

1 Comment

I copied/pasted the code provided but result isn't being set to anything now so no validation issues are being outputted.
0

Got it to work. The logic within nestedKeys.forEach needed to be modified to define result[entry] as an object before anything could be added to it. Here is the looping part of the code with the update -

  parentKeys.forEach(function(entry) { 
    if(data[entry] !== undefined) {
      let i = 0;
      nestedKeys.forEach(function(element) {
        if(data[entry][element] === undefined) {
          if(result[entry] === undefined) result[entry] = {};
          result[entry][i++] = element;
        }
      })
    }
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.