Mermade / oas-kit

Convert Swagger 2.0 definitions to OpenAPI 3.0 and resolve/validate/lint
https://mermade.org.uk/openapi-converter
BSD 3-Clause "New" or "Revised" License
698 stars 129 forks source link

Understanding the opinionated nature of this library #633

Open JaredCE opened 1 year ago

JaredCE commented 1 year ago

Checklist

Detailed Description

I'm trying to understand the opinionated nature of the validator library. It seems like it forces some things on to you like:

{
  anyOf: [
    { type: 'array', items: { type: 'string' } },
    {
      type: 'object',
      additionalProperties: { enum: [ 'commonjs', 'module' ] }
    }
  ],
  default: [ 'cjs', 'mjs', 'js' ]
}

will fail because it is missing a type field, even though the specs don't seem to suggest it's required. Equally if i try to massage an if/then/else into a valid draft-04 from:

"if": {
        "properties": {
            "country": {
                "const": "United States of America"
            }
        }
    },
    "then": {
        "properties": {
            "postal_code": {
                "pattern": "[0-9]{5}(-[0-9]{4})?"
            }
        }
    },
    "else": {
        "properties": {
            "postal_code": {
                "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"
            }
        }
    }

to

oneOf: [
  {
  allOf: [
    {
       "properties": {
            "country": {
                "const": "United States of America"
            }
        }
    },
    "properties": {
            "postal_code": {
                "pattern": "[0-9]{5}(-[0-9]{4})?"
            }
        }
  ]
  },
  {
  allOf: [
    {
      not: {
        "properties": {
            "country": {
                "const": "United States of America"
            }
        }
      }
    },
    "properties": {
            "postal_code": {
                "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"
            }
        }
  ]
  }
]

it will complain that

"properties": {
            "postal_code": {
                "pattern": "[0-9]{5}(-[0-9]{4})?"
            }
        }

needs to be in its own schema components reference.

Other stuff

MikeRalphson commented 1 year ago

Are you sure it's not indentation / { [ mismatches (as above in red) which are causing your issues. This project isn't opinionated about type being present AFAIR.

JaredCE commented 1 year ago

Hey @MikeRalphson. I don't believe there are any mismatches there, if i copy and paste the first example into runkit.com as:

const a = {
  anyOf: [
    { type: 'array', items: { type: 'string' } },
    {
      type: 'object',
      additionalProperties: { enum: [ 'commonjs', 'module' ] }
    }
  ],
  default: [ 'cjs', 'mjs', 'js' ]
}

It doesn't complain about mismatching brackets.

I'm seeing a similar issue from a user of my library here: https://github.com/JaredCE/serverless-openapi-documenter/issues/89

Where type is not defined in the object but in the allOf. Running the as-is schema here https://www.jsonschemavalidator.net/s/EexTxvSB shows it to be valid, but it seems your openAPI validator claims the schema is invalid.

I'm messing about right now, but if i get time later i'll try and put together a small reproducible...

MikeRalphson commented 1 year ago

I wonder if it's the presence of default without a direct sibling type that is triggering a rule for OAS 3.0 that defaults must be of the right type. What version of the library are you using?

JaredCE commented 1 year ago

"oas-validator": "^5.0.8",

JaredCE commented 1 year ago

this is a reproducible, but runkit.com is terrible at outputting the error, so you might want to run it locally:

https://runkit.com/jaredce/openapi-scenario

MikeRalphson commented 1 year ago

I've corrected the OpenAPI syntax and confirmed that it is the presence of default that is triggering the error. In this case you/the OP should move the default into the anyOf subschemas.

https://runkit.com/mikeralphson/63f2530128ff200008363ec7

MikeRalphson commented 1 year ago

One thing you could do is set options.laxDefaults to true, as per https://github.com/Mermade/oas-kit/blob/main/docs/options.md#options-documentation