# Validate response format

**URL:** <https://community.postman.com/t/validate-response-format/40561>\
**Category:** Help Hub\
**Created:** [October 28, 2022, 9:14am UTC](https://community.postman.com/t/validate-response-format/40561 "2022-10-28T09:14:19Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 28, 2022, 9:14am UTC](https://community.postman.com/t/validate-response-format/40561/1 "2022-10-28T09:14:19Z")

</div>

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:

```auto
],
    "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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 28, 2022, 10:08am UTC](https://community.postman.com/t/validate-response-format/40561/2 "2022-10-28T10:08:39Z")

</div>

Hi @shaga85

> [@shaga85](#):
>
> but that’s not what i want, as these values may change.

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

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 28, 2022, 10:11am UTC](https://community.postman.com/t/validate-response-format/40561/3 "2022-10-28T10:11:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 28, 2022, 12:10pm UTC](https://community.postman.com/t/validate-response-format/40561/4 "2022-10-28T12:10:23Z")

</div>

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)

```auto
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:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/d/cdd3112fb8857f5fa9f225b048e34152c905a988.png)

* * *

Failing output on the format (1 character missing)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/3/635a3c2fbdbcb6bb79ea4b100f86ce4db562d359.png)

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

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 29, 2022, 12:55pm UTC](https://community.postman.com/t/validate-response-format/40561/5 "2022-10-29T12:55:35Z")

</div>

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

Again, thanks 🙏

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 30, 2022, 3:52pm UTC](https://community.postman.com/t/validate-response-format/40561/6 "2022-10-30T15:52:00Z")

</div>

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?

 ![postman](https://us1.discourse-cdn.com/getpostman/original/3X/1/b/1b6f64bf6bdc966115f36c359074afc628a8958c.png)

Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 30, 2022, 4:47pm UTC](https://community.postman.com/t/validate-response-format/40561/7 "2022-10-30T16:47:25Z")

</div>

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

let idVal = pm.globals.get(“id\_format”);

😅

Thanks!

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 30, 2022, 5:42pm UTC](https://community.postman.com/t/validate-response-format/40561/8 "2022-10-30T17:42:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 30, 2022, 5:56pm UTC](https://community.postman.com/t/validate-response-format/40561/10 "2022-10-30T17:56:52Z")

</div>

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.

 ![Screenshot 2022-10-30 at 17.49.19@2x](https://us1.discourse-cdn.com/getpostman/original/3X/9/d/9dcaea1e971484c2ac18a62fb388c8e4932ec752.png)

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 30, 2022, 7:45pm UTC](https://community.postman.com/t/validate-response-format/40561/11 "2022-10-30T19:45:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 30, 2022, 10:55pm UTC](https://community.postman.com/t/validate-response-format/40561/12 "2022-10-30T22:55:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 31, 2022, 10:22am UTC](https://community.postman.com/t/validate-response-format/40561/13 "2022-10-31T10:22:47Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [November 4, 2022, 4:52pm UTC](https://community.postman.com/t/validate-response-format/40561/14 "2022-11-04T16:52:53Z")

</div>

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/](https://regex101.com/)

```auto
^[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!

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [November 4, 2022, 6:19pm UTC](https://community.postman.com/t/validate-response-format/40561/15 "2022-11-04T18:19:50Z")

</div>

Try formatting it like this;

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [September 30, 2023, 8:05am UTC](https://community.postman.com/t/validate-response-format/40561/16 "2023-09-30T08:05:38Z")

</div>

Closed on September 30, 2023, 8:05am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
