0

I have a string list(error message list) looks like following

[
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.firstName of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.lastName of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.mobilePhone of required type PhoneInput! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.email of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.gender of required type Gender! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.username of required type String! was not provided."
]

how can check if the in that string list include ...any substring.... "firstName" ...any substring... "was not provided" ....any substring... thanks

Yu Jia
  • 323
  • 1
  • 5
  • 14
  • I have added a solution below. Just so you know, there is already a question like this on stack overflow located at https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript – Spangle Feb 20 '19 at 04:42

2 Answers2

1

One solution is using Array.some() testing each item with a regular expression:

const input = [
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.firstName of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.lastName of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.mobilePhone of required type PhoneInput! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.email of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.gender of required type Gender! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.username of required type String! was not provided."
];

let res = input.some(x => /.*firstName.*was not provided.*/.test(x));
console.log("At least one message match? " + (res ? "YES" : "NO"));
Shidersz
  • 16,846
  • 2
  • 23
  • 48
1

Array.includes() is an easy way to search each array item for the sub string. Example for firstName below.

var errorArray = [
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.firstName of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.lastName of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.mobilePhone of required type PhoneInput! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.email of required type String! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.gender of required type Gender! was not provided.",
  "Variable \"$staffMemberInput\" got invalid value {\"dateOfBirth\":\"1980-01-01\"}; Field value.username of required type String! was not provided."
];

let nameError = 'firstName';
let errorDescription = 'was not provided';
for(var i = 0; i < errorArray.length; i++){
 if(errorArray[i].includes(nameError) && errorArray[i].includes(errorDescription)){
  console.log(nameError  + " " + errorDescription);
 }
}

Simply check for any other values while looping through the array. Hope this helps.

Spangle
  • 762
  • 1
  • 5
  • 14