Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Shared Instance of Ajv #6590

Open
MitchellMcKenna opened this issue May 24, 2019 · 3 comments
Open

Add a Shared Instance of Ajv #6590

MitchellMcKenna opened this issue May 24, 2019 · 3 comments

Comments

@MitchellMcKenna
Copy link

MitchellMcKenna commented May 24, 2019

Describe the problem
I read tv4 is deprecated in favor of Ajv [1][2], however the way you access Ajv is different than how you access tv4. With Ajv you have to require the package and new up a new instance, whereas with tv4 it is available as a shared instance across collection/folder pre-request scripts as well as the requests tests.

I find the shared instance particularly valuable when using the Runner/Newman to fetch/load the schema one time inside the collection pre-request script and use it over and over again across all my requests/tests, which is much faster than fetching the schemas over and over again inside each request's tests. Here's an example of how I'm able to have schemas only loaded once via the collection pre-request script:

if (Object.keys(tv4.getSchemaMap()).length === 0) {
    const openapiUrl = 'http://example.com/openapi.json';

    pm.sendRequest(openapiUrl, function (err, res) {
        tv4.addSchema(openapiUrl, res.json())
    });
}

Describe the solution you'd like
Please add a shared instance of ajv to be used between requests.

Additional benefits:

  • Makes it easier to migrate users over from tv4 to Ajv because it makes accessing it similar.
  • No need to require the package and new up a new instance in every single test script.
  • Since Ajv is already instantiated there wouldn't be the issue of users forgetting to pass {logger: console} in the constructor and wondering why Ajv doesn't work for them. ReferenceError | console is not defined when using console.log #6400
  • Users who like to new up a new instance of Ajv could continue to do so.
@shamasis
Copy link
Member

The shared instance concept is something that we deprecated since it was getting in the way of visibility and conflicts with other scripts. It was also causing debugging issues. Having said that, the I understand the pain of logger issue and we are working to wrap AJV constructor to pre-inject console.

Additionally, two scripts in sandbox are isolated from each other, hence even though the older module variable (_, etc) appears global, but they are separate instances. Hence, this would be tricky. For now, it seems that adding schema in top level scripts and pass the instance to other scripts, is the only viable features.

@MitchellMcKenna
Copy link
Author

MitchellMcKenna commented May 28, 2019

@shamasis How would one "pass the instance to other scripts" as you suggest?

@shamasis
Copy link
Member

Yes. Exactly. That’s the problem. The way we can achieve that is break script execution isolation while running scripts from collection level and then figure out a way to pass functions using “some method”. Both needs to be built. I’m just throwing ideas and possibilities. What comes out finally would be what fits best and adds most value. So, more feedback here would be great.

@codenirvana codenirvana self-assigned this Jun 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants