json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Schema type inference should take `format` into account

Open boneskull opened this issue 1 year ago • 3 comments

I'm not sure if support for the ipv6 format was implemented or if it was intended to be implemented, but using a string prop with an anyOf [{format: 'hostname'}, {format: 'ipv6'}], the result is string and {[key: string]: unknown}, where hostname converts to string.

ipv6 should also convert to string.

ref: https://github.com/appium/appium/pull/18690

boneskull avatar Jun 01 '23 18:06 boneskull

We don't take format into account when inferring a schema's type. Without a full example, I'm not sure why you're getting string at all.

To get string, add "type": "string" to your schema:

{
  "type": "object",
  "properties": {
    "a": {
      "format": "hostname",
      "type": "string"
    }
  }
}

Separately, we should improve type inference to select the appropriate type for common values of format.

bcherny avatar Jun 10 '23 06:06 bcherny

@bcherny Thanks. The code was:

{
  "type": "string",
  "anyOf": [
    {"format": "hostname"},
    {"format": "ipv6"}
  ]
}

Changing it to this solved the problem:

{
  "type": "string",
  "anyOf": [
    {"format": "hostname", "type": "string"},
    {"format": "ipv6", "type": "string"}
  ]
}

So it looks like the type didn't get "inherited". I'm not sure if that's what should be happening?

boneskull avatar Jun 29 '23 21:06 boneskull

though, curiously, hostname did get converted to string, but ipv6 did not. (I can speculate that there's a bug where only the first item in an anyOf inherits its type from its parent)

boneskull avatar Jun 29 '23 21:06 boneskull