openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Missing Types for Pattern, MaxLength, MinLength

Open joshuamil opened this issue 11 months ago • 2 comments

Missing Type Definitions for Pattern, MaxLength, MinLength

When trying to validate an OpenAPI 3 specification that is using Patterns, MaxLength, and MinLength for string and other data types, I get type errors when trying to use the aforementioned properties. I'm using 42Crunch for security audit and validation of generated API specifications, and it complains about these being missing. In the short-term, I've used x-pattern and the like, and then simply replace the text before writing the file programmatically. Obviously, that's not ideal, and is only a workaround in one specific use-case. If you're trying to validate a specification that has these supported properties, it will fail.

Paste full error message
Name Version
openapi-typescript ^2.0.0
Node.js 21.5.0
OS + version macOS Sonoma (14.4)

Reproduction

You can reproduce this by defining an API request, response, or schema and using the following property:

someParamName:
  type: string
  example: Here is a sample
  maxLength: 50
  pattern: ^.{1,50}$
  description: Sample Param Name
  nullable: false

Expected result

Should not get type errors when using supported properties maxLength, minLength, and pattern.

Checklist

joshuamil avatar Mar 19 '24 14:03 joshuamil

Here's the TypeScript error as it occurs when using the SchemaObject type image

joshuamil avatar Mar 19 '24 14:03 joshuamil

I have a couple questions:

  1. Is 2.x the correct openapi-typescript version you’re using (not 6.x)? 6.x is recommended for OpenAPI 3 (which isn’t even supported below 5.x)
  2. type: string will always generate into a string type in TypeScript; there won’t be any maxLength property on a string. If you look at the generated output for someParamName, What does it generate now? What is the expected TypeScript you would like it to generate?

drwpow avatar Mar 19 '24 16:03 drwpow

This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.

github-actions[bot] avatar Aug 06 '24 12:08 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.

github-actions[bot] avatar Aug 14 '24 02:08 github-actions[bot]