anatine / zod-plugins

Plugins and utilities for Zod
658 stars 91 forks source link

[zod-openapi] positive() number property doesn't adhere to OpenAPI 3.1.0 specification #170

Open jgravois opened 1 year ago

jgravois commented 1 year ago

in the same vein as #159

hi there, thanks for maintaining this library! 👋 i just encountered an external validation tool that expects exclusiveMinimum to be a boolean instead of a number.

i'm having a little trouble following the thread, but i'm under the impression that the spec for this changed between 3.0.1 and 3.1.

related: https://github.com/tiangolo/fastapi/issues/240

import z from 'zod'
import { generateSchema } from '@anatine/zod-openapi'

const foo = z.object({ bar: z.number().int().positive() })
const schema = generateSchema(foo)

console.log(JSON.stringify(schema, null, 2))
/*
{
  "type": "object",
  "properties": {
    "bar": {
      "type": "integer",
      "minimum": 0,
      "exclusiveMinimum": 0 // should be boolean
    }
  },
  "required": [
    "bar"
  ]
}
*/

https://github.com/anatine/zod-plugins/blob/cbea74de929355ccfcf11264b27627197eb94141/packages/zod-openapi/src/lib/zod-openapi.ts#L155-L160

i'm not blocked by this. but i assumed logging would be helpful to resolve the issue eventually.

danilofuchs commented 12 months ago

OpenAPI 3.1 follows JSON Schema 2020-12, which states that exclusiveMinimum and exclusiveMaximum MUST be numbers.

Thus, the behaviour here is correct.

https://json-schema.org/draft/2020-12/json-schema-validation#name-exclusivemaximum

danilofuchs commented 12 months ago

The issue here is that this library does not explicitly support OpenAPI 3.1, yet it outputs a v3.1 schema and typings, which are not backwards compatible.