1

I understand that the typical response to this is to use

"type":"["string", "null"]

But the OpenAPI version we have is 3.0.x, not 3.1 so the type of null is not available. I am trying to find a way to get our JSON to validate a field that can be either a string OR null

Current field is

  "type":"string",
  "pattern": "...actual pattern is kind of long...",
  "example": "2018-08-28",
  "description": "Description of the field here.",
  "nullable":true,

But this doe snot validate a null entry as valid. If I set

"type": ["string","null"],

It does validate it correctly, however this displays funky when compile the swagger docs so I am trying to find a way to allow a string or null, validate it AND get it to display correctly. With the above it displays all as one word stringnull for the type in the docs.

I have tried with JSON schema versions

OpenAPI version 3.0.1

1 Answer 1

3

OAS 3.0.x has a nullable property they have defined for this purpose

Which validator are you using? It may not have implemented this keyword which is why you're having trouble using it.

You can validate it works with hyperjump-io

swap out the data and data2 to try the different instances.

import { validate } from "@hyperjump/json-schema/openapi-3-0"
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);


let data = {
  "port": null
}

let data2 = {
  "port": "test"
}

try {

  
  let valid = await validate(`file://${__dirname}/test.openapi.json#/components/schemas/some_prop`, data)


  console.log({ valid, errors: { ...valid.errors } })
} catch (err) {
  console.error(err.message)
}

#test.openapi.json
{
    "openapi": "3.0.3",
    "info": {
        "title": "blah",
        "version": "1.1.0"
    },
    "paths": {
        "/v1/apis": {
            "get": {
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/some_prop"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "some_prop": {
                "type": "object",
                "properties": {
                    "port": {
                        "type": "string",
                        "nullable": true,
                        "pattern": "^[a-z]$",
                        "description": "description here",
                        "example": "jeremy"
                    }
                }
            }
        }
    }
}

it shows up correctly on Swagger-UI free version.

swagger-ui

1
  • 1
    there is a Gem called json_schemer that we use. So I will look at if that maybe does not have this built in, and perhaps there is something we can switch to, thanks for the response! Commented Nov 6, 2023 at 15:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.