Serving schema with regular expression as static files

Hi,

I have a local file called schema.json which contains all the schema of our endpoints. I am running a local http-server to serve this file. Postman will send a request to this server (through pre-request tab) and the response will be saved as an environment variable. All is well except in this file, I have a key called “pattern” and the value for this key will contain \ (slashes) because I am checking for a . so i need to escape this in regular expression context.

eg. {
	"endpoint1" : {
		"type" : "object",
		"properties" : {
			"key1" : {
				"type" : "string",
				"pattern" : "^ABC\.ABC$"
			}
		}
	},
	
	//more endpoints here
}

when I call the service in the pre-request tab, I get an error in the console saying 'JSONError | Unexpected token '.' at 7:23 "pattern" : "^ABC\.ABC$" ^ '

Now if I put another slash in the file to escape the slash, the error will go away. But "pattern" : "^ABC\\.ABC$" is different than "pattern" : "^ABC\.ABC$" in regular expression point of view. And I want to avoid escaping of characters as its going to be a bit messy and confusing.

So question is, is there a way to somehow serve the contents of the files as it is (without the need to escape the slashes)?

Pre request script
const postRequest = {
url: ‘http://XX.XX.XX.XX:8080/test.json’,
method: ‘GET’
};
pm.sendRequest(postRequest, (error, response) => {
console.log(error ? error : response.json());
});

Postman console
image

Any help is appreciated.

Thanks.

Hi @allen.vargas,

First I’d like to applaud your setup. When adding extra http-servers that host resources for you, you really expand the capabilities of Postman.

To help with this issue, my mind first went to raw strings, to see if JSON supports it. This is something I know from Python, where using a ‘r’ in front of a string, makes it raw, and you don’t have to escape everything, like so:

test = r"No need to escape me"

Well, I googled “raw string support in JSON” and didn’t find that it supports raw strings. However, this did show up, and it makes perfect sense.

Store your regular expression base64 encoded. This is highly used for things of this nature when data is traveling across the network, so this should help. Then when you actually need to use the regular expression, base64 decode it, and it should work as expected.

Hope this helps!

Orest

Hi Orest,

Thanks for your reply. I tried what you suggested but unfortunately, it did not solve my problem. I tried encoding the whole file. Then when I received the encoded string on postman side, I decoded it and I used JSON.parse() but i got the same error as the original post.

If I will just encode and decode the regular expression part, probably I will get the same error when I use JSON.parse() to make it an object again?

Anyway, I found a trick to solve my problem. Once I received the response, I saved it as plain text. And do a replace of single backlashes to double backslashes. This way it wont throw the error on JSON.parse(). I tested it and it looks fine.

var raw = response.text();
var res= raw.replace(/\\/g,"\\\\");
var schema= JSON.parse(res);

This is OK for me now. At least I don’t have to escape it on the file itself. The escaping is hidden on the user’s eyes.

Regards,
Allen

2 posts were split to a new topic: Validate A Schema With Dynamic Keys