Here is a JavaScript that will search all selected listings and all attributes (key and values) for a specific key or a specific value, OR a MISSING key or a MISSING value.
Neal
// <https://manual.iwascoding.com/gs8/en/Custom_JavaScript_Actions-Running_Scripts.html>
const newLine = ""; // "\n"
const blankLine = "\n"; // "\n\n"
const searchKey = "";
const searchNoKey = "";
const searchValue = "";
const searchNoValue = "";
function run() {
var errors = [];
var errorsCount = 0;
for (const listing of selectedListings) {
var validateErrors = validateListingProperties(listing);
if (validateErrors.length > 0) {
errorsCount++;
var listingTitle = "";
if (listing.title) {
listingTitle = listing.title;
}
errors.push(blankLine + "Title: " + listingTitle + ": " + newLine);
errors.push(... validateErrors);
}
}
consoleLog("selectedListings: " + selectedListings.length);
consoleLog("validateListing errors: " + errorsCount + newLine + errors + newLine);
}
function validateListing(listing) {
var errors = [];
var validateErrors = validateListingProperties(listing);
errors.push(... validateErrors);
return errors;
}
function validateListingProperties(listing) {
var errors = [];
const listingTitleLengthMinimum = 2;
const listingTitleLengthMaximum = 80;
var listingTitle = "";
if (listing.title && (listing.title.length > listingTitleLengthMinimum) && (listing.title.length <= listingTitleLengthMaximum)) {
listingTitle = listing.title;
}
if (!listing.attributes) {
errors.push("No attributes" + newLine);
}
if (listing.attributes) {
// consoleLog("title: " + listing.title);
var logHeader = true;
var hasSearchNoKey = false;
var hasSearchNoKeyOnceThruLoop = false;
var hasSearchNoValue = false;
var hasSearchNoValueOnceThruLoop = false;
const attributes = listing.attributes;
for (const key in attributes) { // use in, not of
// consoleLog("key: " + key);
if (!key || (key.length <= 0)) {
errors.push("No key" + newLine);
continue; // for loop
}
const values = attributes[key];
if (!values || (values.length <= 0)) {
errors.push("No values" + newLine);
continue; // for loop
}
for (const constForLoopValue of values) {
value = constForLoopValue;
// consoleLog("value: " + value);
if ((searchValue != "") && (value == searchValue)) {
errors.push("Key: " + key + ", Value: " + value + newLine);
}
hasSearchNoValueOnceThruLoop = true;
if ((searchNoValue != "") && (value == searchNoValue)) {
hasSearchNoValue = true;
}
}
if (logHeader) {
logHeader = false;
// consoleLog("key, values: ");
}
// consoleLog(key + ", " + values);
if ((searchKey != "") && (key == searchKey)) {
errors.push("Key: " + key + ", Values: " + values + newLine);
}
hasSearchNoKeyOnceThruLoop = true;
if ((searchNoKey != "") && (key == searchNoKey)) {
hasSearchNoKey = true;
}
}
if (hasSearchNoKeyOnceThruLoop && !hasSearchNoKey && (searchNoKey != "")) {
errors.push("No key: " + searchNoKey + newLine);
}
if (hasSearchNoValueOnceThruLoop && !hasSearchNoValue && (searchNoValue != "")) {
errors.push("No value: " + searchNoValue + newLine);
}
}
// consoleLog("validateListingProperties: " + errors);
return errors;
}