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

SchemaObject type: 'string' should allow pattern?: string

Open Apollo314 opened this issue 2 years ago • 4 comments

https://github.com/drwpow/openapi-typescript/blob/206f28517f1707ec0d97ad72251323a0da6ad2f1/src/types.ts#L436

image

type: 'string' should allow pattern?: string as per the standard:

https://swagger.io/docs/specification/data-models/data-types/#string

otherwise it falsely assumes it is supposed to be type 'object' and gives an error that way.

image

it also should allow minLength and maxLength.

image

this fixes the errors.

Apollo314 avatar Apr 02 '23 06:04 Apollo314

Feel free to open a PR

mitchell-merry avatar Apr 07 '23 05:04 mitchell-merry

turns out they were 3.0.x keywords. couldn't find them in 3.1.x specs.

Apollo314 avatar Apr 07 '23 07:04 Apollo314

I've run into a similar problem - wanting to represent pattern for strings via openapi-typescript - and I think I found where the validation keywords are defined for OpenAPI 3.1.x, if that's sufficient encouragement to reopen this issue.

The validation keywords are no longer in the OpenAPI 3.1.x specification explicitly, but the Schema Object section of the OpenAPI 3.1.x spec starts with:

The Schema Object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. This object is a superset of the JSON Schema Specification Draft 2020-12.

For more information about the properties, see JSON Schema Core and JSON Schema Validation.

Unless stated otherwise, the property definitions follow those of JSON Schema and do not add any additional semantics.

and the JSON Schema Validation link contains almost all of the validation keywords that were explicitly called out in the Schema Object section of the 3.0.x OpenAPI spec. (title is not included, but the others all are, including pattern for strings.)

EDIT: That said, I appreciate that adding all this validation is a large amount of work, and so I understand if this is something you'd want a PR submission for.

isobelhooper avatar Oct 31 '23 10:10 isobelhooper

@isobelhooper thanks for adding that! To be honest I didn’t really take a close look at this issue since the original author closed it. I’m not opposed to adding this, but what would the proposed generated types look like? Keeping in mind that this library intentionally does NOT validate at runtime, and though string template literals are supported in TS, I’m not aware of a way to enforce RegEx.

This library also does enforce minLength and maxLength for arrays (tuples), which is a bit off-topic but just pointing out if there’s a clear implementation, supporting this for strings is aligned with the project goals provided it works as most users predict it will.

If you or anyone could provide proposed types to generate (even as a trivial example) we could look into it, but just at first glance there didn’t seem to be a clear implementation path (to me).

drwpow avatar Nov 01 '23 14:11 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 15 '24 02:08 github-actions[bot]