I’m afraid I’m not understanding you. If the input for containerId is empty string yes, it’s invalid and it will being taken care of by !Object.hasOwn(manifest, “containerId”) ||
manifest.containerId === “” as far as I’m seeing.
I discovered I’m using my outdated code. Here’s my new one but its still not passing these two:
If the input manifest object is valid, your validateManifest function should return an empty object {}.
17.If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.
const manifestSample = {
containerId: "string", // allows string and number
destination: "0", // allows string
weight: "string", // valid number that is not a float or negative or 0
unit: "string", //only allows "kg"
hazmat: "string", //only allows
}const
function validateManifest(manifest){
const errors = {};
const id = manifest.containerId;
const des = manifest.destination;
const we = manifest.weight;
// CONTAINER ID
if (!Object.hasOwn(manifest, "containerId") || (typeof id === "string" && id.trim() === "")){
errors.containerId = "Missing";
}else if(id == undefined || id <= 0){
errors.containerId = "Invalid";
}else if(typeof id === "number" && !Number.isInteger(id)){
errors.containerId = "Invalid";
}
// DESTINATION
if(!manifest.destination){
errors.destination = "Missing";
}else if (typeof des === "string" && des.trim() === ""){
errors.destination = "Invalid";
}else if(typeof manifest.destination === "number"){
errors.destination = "Invalid";
};
// WEIGHT
if(manifest.weight <= 0 ||Number.isNaN(we) || typeof we === "string"){
errors.weight = "Invalid";
}else if(!manifest.weight || manifest.weight === "Undefined"){
errors.weight = "Missing";
};
// UNIT
if(!manifest.unit){
errors.unit = "Missing";
}else if (manifest.unit !== "kg"){
errors.unit = "Invalid";
};
// HAZMAT
if(manifest.hazmat === undefined){
errors.hazmat = "Missing";
}else if(typeof manifest.hazmat === "string" || typeof manifest.hazmat === "number" || manifest.hazmat === null){
errors.hazmat = "Invalid";
}
if(Object.keys(errors).length === 0){
return {};
};
return errors;
};
console.log(validateManifest(manifestSample));
console.log(typeof(manifestSample));