# How to validate a response field having a regex pattern

**URL:** <https://community.postman.com/t/how-to-validate-a-response-field-having-a-regex-pattern/47093>\
**Category:** Help Hub\
**Tags:** variables, regex\
**Created:** [May 4, 2023, 5:54pm UTC](https://community.postman.com/t/how-to-validate-a-response-field-having-a-regex-pattern/47093 "2023-05-04T17:54:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerospace-operator15](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@aerospace-operator15](https://community.postman.com/u/aerospace-operator15)\
**Post date:** [May 4, 2023, 5:54pm UTC](https://community.postman.com/t/how-to-validate-a-response-field-having-a-regex-pattern/47093/1 "2023-05-04T17:54:12Z")

</div>

Hello there,

How can i validate a response field having a regex pattern?

Thank you!!

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [May 5, 2023, 9:45am UTC](https://community.postman.com/t/how-to-validate-a-response-field-having-a-regex-pattern/47093/2 "2023-05-05T09:45:19Z")

</div>

Using the match assertion.

The following is an example from a previous question.

```auto
var array = ["123", "ABC", "123ABC"]

// following regex match allows only Alphanumeric
// It doesn't allow 'only alpha' or 'only numbers'.
array.forEach(string => 
    pm.test(`${string} is alphanumeric`, () => {
        pm.expect(string).to.match(/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+[0-9a-z]+$/i);
    })
);

// following regex allows alphanumeric, only alpha, and only numbers.
array.forEach(string => 
    pm.test(`${string} is alphanumeric v2`, () => {
        pm.expect(string).to.match(/^([0-9]|[a-z])+([0-9a-z]+)$/i);
    })
);

```

---

<div class="post-metadata">

**Author:** ![aerospace-operator15](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@aerospace-operator15](https://community.postman.com/u/aerospace-operator15)\
**Post date:** [May 5, 2023, 1:52pm UTC](https://community.postman.com/t/how-to-validate-a-response-field-having-a-regex-pattern/47093/3 "2023-05-05T13:52:05Z")

</div>

Thanks mdjones for the pointer.

I am planning to validate the response schema as a whole instead of applying field level validations.  
because of different fields having different regex patterns.  
Not sure if schema validation supports the regex pattern? if yes - then my problem is solved  
else i may proceed with the additional logic that you supplied.

Thanks,  
Itishree

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [May 5, 2023, 2:07pm UTC](https://community.postman.com/t/how-to-validate-a-response-field-having-a-regex-pattern/47093/4 "2023-05-05T14:07:10Z")

</div>

The problem with trying to validate multiple elements in one go, is that when you have failures, its not always easy to work out which element failed.

Hence the advice for tests to be singular.

If you have a pm.test block with multiple pm.expect assertions. The first assertion that fails will be what is reported in the test results tab. It won’t evaluate the remaining assertions, and you won’t get feedback on those elements until you fix the initial assertion. You may have multiple bugs and not know it. So the advice would be to test each element separately, and in its own pm.test block.

Postman can validate a schema, that the required elements are being returned etc and the type of data. (number, string, boolean) but it can’t do a regex match at this level as far as I’m aware.

For example…

```auto
const schema = {
    "type": "object",
    "properties": {
        "training_details": {"type": "object",
            "properties": {
                "id": { "type": "string"},
                "start_date": { "type": "string"},
                "end_date": { "type": "string"},
            },
        "required": ["id", "start_date","end_date"]
        }
    },
};

pm.test('API response schema is valid', () => {
    pm.expect(response).to.have.jsonSchema(schema);
});

```
