Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Trying to finish the “Build a Cargo Manifest Validator” section on Objects. Step 14 asks that if the manifest passed into validateManifest contains errors that it should return an object with information about those errors. My function is set up to always return a validatedManifest, containing only the error messages, so an empty object if no errors are present. I think this should pass the test, but it fails. Any help is appreciated.

Your code so far

const normalizeUnits = (manifest) => {
  let copy = {
    containerId: manifest.containerId,
    destination: manifest.destination,
    weight: manifest.unit === "kg" ? manifest.weight : Number(parseFloat(manifest.weight * 0.45)),
    unit: "kg",
    hazmat: manifest.hazmat
  };
  return copy;
};

const validateManifest = (manifest) => {
  let validatedManifest = {};
  if (manifest.containerId === undefined) {
    validatedManifest.containerId = "Missing"
  } else if (typeof(manifest.containerId) !== "number" || !Number.isInteger(manifest.containerId) || manifest.containerId <= 0) {
    validatedManifest.containerId = "Invalid"
  };
  if (manifest.destination === undefined) {
    validatedManifest.destination = "Missing"
  } else if (typeof(manifest.destination) !== "string" || (manifest.destination).trim() === "") {
    validatedManifest.destination = "Invalid"
  };
  if (manifest.weight === undefined) {
    validatedManifest.weight = "Missing"
  } else if (typeof(manifest.weight) !== "number" || manifest.weight < 0 || Number.isNaN(manifest.weight)) {
    validatedManifest.weight = "Invalid"
  };
  if (manifest.unit === undefined) {
    validatedManifest.unit = "Missing"
  } else if (manifest.unit !== "lb") {
    if (manifest.unit !== "kg") {
      validatedManifest.unit = "Invalid"
    }
  };
  if (manifest.hazmat === undefined) {
    validatedManifest.hazmat = "Missing"
  } else if (typeof(manifest.hazmat) !== "boolean") {
    validatedManifest.hazmat = "Invalid"
  };
  return validatedManifest;
};

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

processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false });


Your browser information:

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

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Ensure each validation check first verifies the property exists and has the correct type before performing any type-specific operations like .trim() or numeric comparisons, and use simple, clear conditions for unit validation.

I ran into almost the same issue on that task. The tricky part isn’t just returning an object, it’s when you return it. Right now your function always returns an object, even when there are no errors, but the challenge expects a different behavior depending on validation.

What “different behavior” do you think it’s expecting other than returning an empty object if there are no errors?

You should implement a function named validateManifest with a manifest parameter.

  • The function must not mutate the original manifest object and must always return a new object.

Hi @jacobbschwarz ,

Should a weight of 0 be valid?

Happy coding!

Thank you for your help! I won’t spoil my updated code here, but both of your hints helped me reevaluate my approach. I also will admit I was much more careful to test my code line by line to make my validations actually validated the way they were supposed to.

1 Like