最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - How to handle state codes in a case insensitive way in your OpenAPI V3 Spec - Stack Overflow

programmeradmin2浏览0评论

One of the objects in our model is a "State" which is normally put in as a state code. As such we have defined the following in our schema:

StateOrProvince:
  type: string
  enum:
    - AL
    - AK
    - AZ
    - AR
    - AS
    ...

This has proven to be incredibly problematic for our Swift app. Since we don't have complete control of the input form this data is coming from it is entirely possible for someone to put in "Az" instead of "AZ" and that lowercase Z will mess up the parsing of the larger object that contains a StateOrProvince.

One obvious solution is to define this as just a string. In Swift handle parsing this to a state code. We could also have the server turn this data into all caps on its way out.

But in an effort to get better with OpenAPI I did want to explore if there were some other options for this. The only one I have seen was mentioned here. However it is a much simpler case, it is solved by regex that would be horrific to write/read in this file with this many cases and doesnt allow for it to be parsed into an Enum.

I was curious about a few options. In swift we have a way to have a enum like

enum State {
  case AL
  case AK
  ....
  case Unknown(stateCode: String)
}

This would allow us to gracefully put either states we don't have in this list or inputs like "Az" if we had to into an .Unknown(stateCode: "Az")

Does OpenAPI have this sort of concept and or is there a way to handle enum cases in a case insensitive way within your API spec?

发布评论

评论列表(0)

  1. 暂无评论