Validate response format

Hello!

You guys have been really helpful, so i’m gonna hit you with another doubt.

I want to validate that a parameter response has the correct format:

],
    "status": "BURNING",
    "createdAt": "2022-10-14",
    "nftContract": {
        "id": "caa6df98-e398-4862-bcb6-A558c8f1c306",
        "name": "nftMint",
        "address": "0x9e86339G950E0584a439FDr198654e718e0EC447",
..

Lets say i want to validate that “id” and “address” responses have the correct format. i used to use:

pm.expect(id).to.eql("caa6df98-e398-4862-bcb6-A558c8f1c306")

but that’s not what i want, as these values may change.

Is it possible?

Hi @shaga85

What is it that you do want? … validate on string length? not ‘null’?
What is the requirement for it to be considered successful?

What i want is to validate that if the parameter response has the same format as “caa6df98-e398-4862-bcb6-A558c8f1c306” for example, then its OK.

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);
});

Passing output:


Failing output on the format (1 character missing)

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

1 Like

Man you have been truly amazing! Thanks for all the help. I’ve been learning a lot from you!

Again, thanks :pray:

Hey,

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?

Thanks a bunch!

Nevermind, it was easier than i thought. I simply used:

let idVal = pm.globals.get(“id_format”);

:sweat_smile:

Thanks!

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.

1 Like

I think I found a way to do it:

… [A-Za-z0-9]{8,8}…

Essentially just add another value setting a minimum and a maximum.

I did try that based on some instructions online, but I had mixed results with the first match.

If it works for you, then fantastic!!

Hey,
you were right, that regex wasnt working as i wanted. i came up with this one that seems good when tested through: https://regex101.com/

^[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}$

but for some reason its not working with the script that you provided me.

Am i missing something?

thanks!

Try formatting it like this;

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)