Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

My code logs the expected output on the console but the test refuses to pass and throws error on 10 and 15, which are: Calling validateManifest() with { containerId: 0, destination: 405, weight: -84, unit: “pounds”, hazmat: “no” } should return the new object { containerId: “Invalid”, destination: “Invalid”, weight: “Invalid”, unit: “Invalid”, hazmat: “Invalid” } without mutating the source input.
15. If the input manifest object is not valid, your validateManifest function should return a

Your code so far

function normalizeUnits(manifest){
  const newManifest = {...manifest};


 const {weight,unit} = newManifest;
  if(unit === 'lb'){
    newManifest.weight = weight * 0.45;
    newManifest.unit = 'kg';
  }
  return newManifest;
};


function validateManifest(manifest){
  if(manifest === null || manifest === undefined)return {}
  const errors = {};
  const schema = {
  containerId: 'number',
  destination: "string",
  weight: 'number',
  unit: "string",
  hazmat: 'boolean'
} ;
for(const key in schema){

  if(!(key in manifest)){
  errors[key] = "Missing";
  }else{
 const values =manifest[key];
    let isInvalid = false;
    if(key === 'containerId' || key === 'weight'){
      if(typeof values !==  'number' || !Number.isInteger(values) || values <= 0 || Number.isNaN(values)){
        isInvalid = true;
      }
    }else if(key === 'destination' || key === "unit"){
      if(typeof values !== 'string' || values.trim().length === 0){
        isInvalid = true;
      }
    }else if(key === 'hazmat'){
      if(typeof values !== 'boolean'){
        isInvalid = true;
      }
    }
    if(isInvalid){
       errors[key] = "Invalid"
    }
  }
}

   return errors;
}

console.log(validateManifest({ containerId: 0, destination: 405, weight: -84, unit: "pounds", hazmat: "no" }))

function processManifest(manifest){


  const validateM = validateManifest(manifest);
  if(Object.keys(validateM).length <= 0){
   console.log( `Validation success: ${manifest.containerId}`);
   console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`)
  }else{
      console.log( `Validation error: ${manifest.containerId}`);
   console.log(validateM)
  }
};

processManifest({ destination: "Watsonville", hazmat: true })

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Hi @Chirovofiel,

I see that you logged the function call from Test #10. How does what your code returns compare to what is expected in that test?

Happy coding!