AJV <> Postman Tests <> OpenAPI 3.0 questions

Hi there! Apologies for the “vagueness” of the title, however, I have a few questions regarding AJV and Postman Tests vis-a-vis OpenAPI 3.0.

Context
So, with our OpenAPI schema (YAML), we make a lot of use of the nullable: true and format: parameters to ensure consistency in what our API returns (and to give developers using our API a good indication of what they should expect to receive).

We now want to further boost our Public Workspace collections with a whole host of tests so that developers can validate, during each call, that what we return is exactly what we say (mistakes happen, and the best QAs are the developers that use our API and let us know that something is awry).

Issue #1 - AJV validation with "nullable": true fields

Our first issue, however, is when we use the built-in AJV validation.

It seems that the library doesn’t support the nullable: true field. The only way to indicate to the AJV library that a field can be null is by adding an array to the type field, like so: "type": ["string", "null"]. This is a bit weird, as in the AJV documentation they write that this field is supported “by default, without additional options.”. I know that they indicate that you can use either "nullable": true or "type": ["string", "null"], however, they do mention that the first option is better supported. As we write everything in the YAML and then convert it to JSON (if necessary), we prefer not to use a regex to later convert everything to the AJV form that Postman supports. Anyone have any ideas of where I’m going wrong maybe?


{
  "title": "Nullable issues with AJV - #1",
  "type": "object",
  "description": "Schema with 'nullable': true",
  "required": [
    "external_id"
  ],
  "properties": {

    "external_id": {
      "description": "The external_id you provided.",
      "type": "string",
      "nullable": true, // This will cause an error if the response is anything but a string
      "pattern": "^[a-zA-Z0-9_.-]*$"
    }
  },
  "additionalProperties": false
}
{
  "title": "Nullable issues with AJV - #1",
  "type": "object",
  "description": "Schema with 'nullable': true",
  "required": [
    "external_id"
  ],
  "properties": {

    "external_id": {
      "description": "The external_id you provided.",
      "type": ["string", "null"] // This one will not cause an error if the response is a string or null - our desired effect
      "pattern": "^[a-zA-Z0-9_.-]*$"
    }
  },
  "additionalProperties": false
}
// Code we use to load AJV and run the validation
pm.test("Response schema is compliant", function() {
      var Ajv = require('ajv');
      const ajv = new Ajv({allErrors: true});
      const valid = ajv.validate(schema, pm.response.json());
      if (!valid) console.log(ajv.errors)
      pm.expect(valid).to.be.true;
  });

Issue #2 - AJV validation with "format": "float" fields

A similar issue is with using the "format" fields with numbers or integers:


We use this field quite a lot, again, to indicate to our clients how “big” of a number they should be expecting… Do I need to import an additional AJV library to be able to do this? (As, once again, we’d prefer not to have to use a regex to remove all this from the JSON we generate).

I’d really like to hear your feedback!

Oh and BTW Postman team: I LOVE your “Create test suite” feature (which generates based on your openapi spec - it’s amazing. But I still quite like to use AJV as I can update the tests a lot quicker, while with Postman test feature I need to edit the API and generate a new suite of tests each time I make a correction :frowning: ).

Cheers,

Dom