orval icon indicating copy to clipboard operation
orval copied to clipboard

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

Open Poylar opened this issue 1 year ago • 1 comments

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

Poylar avatar Aug 22 '24 07:08 Poylar

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

melloware avatar Aug 26 '24 18:08 melloware