orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
3k stars 327 forks source link

Zod: oneOf with regex and string().email() missing 'regex' variable type #1594

Closed Poylar closed 1 month ago

Poylar commented 2 months ago

this is json

{
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "required": [
            "login",
            "password",
            "remember"
          ],
          "properties": {
            "login": {
              "description": "",
              "oneOf": [
                {
                  "type": "string",
                  "maxLength": 18,
                  "minLength": 18
                },
                {
                  "type": "string",
                  "format": "email"
                }
              ]
            },
            "password": {
              "type": "string",
              "minLength": 8
            },
            "remember": {
              "type": "boolean",
              "example": false
            }
          },
          "type": "object"
        },
        "example": {
          "login": "+7 (982) 556-29-77",
          "password": "t9NB5Z4H8Q_V",
          "remember": false
        }
      }
    }
  }
}

This is how it is generated, usersPassportLoginBodyLoginMinOne and usersPassportLoginBodyLoginMaxOne are not defined so this type of variables is simply not generated, if you remove the min and max check in the json then everything is fine, also it does not work with 'regex'

export const usersPassportLoginBody = zod.object({
  login: zod
    .string()
    .min(usersPassportLoginBodyLoginMinOne)
    .max(usersPassportLoginBodyLoginMaxOne)
    .or(zod.string().email()),
  password: zod.string().min(usersPassportLoginBodyPasswordMin),
  remember: zod.boolean()
});

Any logs, error output, etc?

i have no errors while generated zod, but have some warnings when generate fetch

🎉 elevators - Your OpenAPI spec has been converted into ready to use orval!
zod: Cleaning output folder
⚠️  SyntaxError: Swagger schema validation failed.
  #/components/securitySchemes/sanctum must have required property '$ref'
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum/type must be equal to one of the allowed values
  #/components/securitySchemes/sanctum must have required property 'flows'
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum/type must be equal to one of the allowed values
  #/components/securitySchemes/sanctum must have required property 'openIdConnectUrl'
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum must NOT have additional properties
  #/components/securitySchemes/sanctum/type must be equal to one of the allowed values
  #/components/securitySchemes/sanctum must match exactly one schema in oneOf
  #/components/securitySchemes/sanctum must match exactly one schema in oneOf

What versions are you using?

  System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor
    Memory: 4.12 GB / 15.92 GB
  npmPackages:
    @tanstack/react-query: ^5.51.23 => 5.51.23
    axios: ^1.7.4 => 1.7.4
    orval: ^7.0.1 => 7.0.1
    react: ^18 => 18.3.1
    zod: ^3.23.8 => 3.23.8
melloware commented 2 months ago

I think this is similar to one or two other "zod" labeled issues open.