I think your best bet would be to use regular expressions.
You could do something like this:
(Note, you would need to grab the value from your response)
let idVal = '"id": "caa6df98-e398-4862-bcb6-A558c8f1c30",';
let formatMatch = new RegExp(/[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}/).exec(idVal)
pm.test("Validate id value", function () {
//Check the RegEx matches to the value
pm.expect(formatMatch, "Format does not match regular expression.").to.not.eql(null);
console.log("formatMatch = " + formatMatch);
});
Info about the RegEx;
[A-Za-z0-9] = Match any alphanumeric characters
{8} = Length is equal to 8 characters
Each match is separated by the expected hyphen
I am setting the id response as a variable. However i am having a hard time getting that variable value into the script you provided me. How do i do it?
This code could probably be tidied up a bit.
Do you use the value that is saved from the first RegEx?
Lines 1 and 4 essentially do the same thing but are being stored in separate variables.
Yeah, I changed it a bit.
I noticed something though, if i change the number of characters on the first set of values on the regex from {8} to {6} for example, the test still passes, so i guess its up to 8 and not exactly 8. Is there a way to be exactly 8? Also i have a doubt, that i would like to understand better. Why did you use:
pm.expect(formatMatch, “Format does not match regular expression.”).to.not.eql(null);
what exactly is the to.not.eql(null)?
sorry if all this are silly questions but im very new at this and you are really helping me understand and learn alot about postman tests.
Not sure about the RegEx, I would need time to research it.
The assertion is set to "to.not.eql(null) because if nothing is found by the RegEx then the value returned would be “null”. If something IS found then the value would be the match that is found.