The following is a conceptual issue:
I am doing some practices with JS and NodeJS , and I came across a point in an exercise that asks that once a CSV file has been read and parsed to JSON , it must be verified that the data truly corresponds to the Product type . This means that I should check it by creating a Product class and check that each property is defined?
At first I thought to check the array obtained with a for to verify that the data exists and meets the expected data type, but the exercise tells me that this is not the (regular) solution.
The data received is the following, as an example:
[
{
name: 'König',
description: '',
imageURL: 'https://image.jpg',
brandName: 'Pilsener',
'attributes/style': 'classic',
'attributes/ibu': '5',
'attributes/abv' : '2',
price: '250',
inStock: 'true' ,
isVisible: 'true' ,
launchDate: ''
},{
name: 'Paulaner',
description: '',
imageURL: 'https://image.jpg',
brandName: 'Paulaner',
'attributes/style': 'red',
'attributes/ibu': '8' ,
'attributes/abv' : '4' ,
price: '400' ,
inStock: 'true' ,
isVisible: 'false' ,
launchDate: ''
}
]
At first I thought this:
const checkType = async data => {
function Product(name, description, imageUrl, brandName, style, ibu, abv, price, inStock, isVisible, launchDate) {
this.name = name;
this.description = description;
this.imageUrl = imageUrl;
this.brandName = brandName;
this.style = style;
this.ibu = ibu;
this.abv = abv;
this.price = price;
this.inStock = inStock;
this.isVisible = isVisible;
this.launchDate = launchDate;
}
const check = [];
for(let n = 0; n < data.length; n++) {
const products = new Product(data[n].name, data[n].description, data[n].imageUrl,
data[n].brandName, data[n]['attributes/style'], data[n]['attributes/ibu'],
data[n]['attributes/abv'], data[n].price, data[n].inStock,
data[n].isVisible, data[n].launchDate);
check.push(products.name, products.description, products.imageUrl, products.brandName,
products.style, products.ibu, products.abv, products.price, products.inStock,
products.isVisible, products.launchDate);
}
const listProducts = [];
if(!check.includes(undefined)) {
for(let n = 0; n < data.length; n++) {
if(data[n]['attributes/ibu'] === '' || data[n]['attributes/abv'] === '' || data[n].price === '' || data[n].inStock === '')
return false;
const prods = {
name: data[n].name,
description: data[n].description,
imageUrl: data[n].imageUrl,
brandName: data[n].brandName,
attributes: {
style: data[n]['attributes/style'],
ibu: Number(data[n]['attributes/ibu']),
abv: Number(data[n]['attributes/abv'])
},
price: Number(data[n].price),
inStock: data[n].inStock,
isVisible: data[n].isVisible,
launchDate: data[n].launchDate
};
listProducts.push(prods);
}
const list = await verify(listProducts);
if(list) {
write(list);
return list;
}
return false;
} return false;
};
In these kinds of cases, TypeScript is where it really shines, at least during the development environment.
Your proposal to make a class is the first thing that came to my mind while reading your question.
You can go through the array, instantiate the class by passing each iteration as a parameter and within the constructor, unstructure said parameter and if one is missing, JS will take care of throwing an error.
The problem with this is that it will check that the keys exist, but not the type, for that you have to validate the type, and this already takes more work. You can use a module called jsonschema that does just that, validate.
Content validation (no instance)
However, I see that your object is completely made up of strings, so it is as simple as going through it and checking the type of each value, I leave you this example that I have prepared based on promises:
And if you need to check that the string is not empty, change the if inside the check function to:
Content validation (with instance)
This was the main idea, although it doesn't validate the type, if it checks its length is not 0.
It first loops through the object and checks that the property is not empty. It then deconstructs the object passed as a parameter and assigns it.
product class
Iteration