Test for format of a UUID

In a pre-request script, I would like to ensure a particular environment variable’s value has the format of a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

What’s the best way to do this in Postman? Thanks!

By using a regex match.

Postman uses JavaScript under the hood, so this is easily searchable on the web.

javascript - How to test valid UUID/GUID? - Stack Overflow

How to use regular expression in postman script · Issue #4334 · postmanlabs/postman-app-support · GitHub

Which will give you something like the following. I’ve put it in a loop, and I’m getting a random guid using Postman dynamic variables. They have the same format as UUID, but please read the first link for more info on this.

for (let i = 0; i < 10; i++) {
    let uuid = pm.variables.replaceIn('{{$guid}}');
    pm.test(`${uuid} is a valid UUID`, () => {
        console.log(uuid);
        pm.expect(uuid).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
    });
}

image

As always, please remember to make the test fail to ensure its not giving a false positive.

1 Like

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