Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterns for structs with single unnamed field #572

Open
johnperry-math opened this issue Apr 13, 2023 · 5 comments
Open

Patterns for structs with single unnamed field #572

johnperry-math opened this issue Apr 13, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@johnperry-math
Copy link

For the following struct,

struct Thing(T);

I would like to restrict T using some of the options available for structs with named fields, such as restricting a String to a pattern, or a numerical type to a minimum and maximum. Is there a way to specify this using the #[schema(...)] macro?

@juhaku
Copy link
Owner

juhaku commented Apr 13, 2023

In theory it could be possible for primitive types like str, String, i32 etc. But not for $ref types (Types that are a reference to another ToSchema type). Thus its is not implemented and supported at the moment.

Thing is that the validation rules are to be added on schema properties yet struct Thing(T) is not a struct with fields but will render a schema for T within the Thing struct. When T is a String it will render the schema for the String and validation rules are not exactly placed on a type but alongside of a type on a property. But unnamed field structs do not have properties.

This is something that could be explored in future but at the moment the priority for the support is not very high.

@johnperry-math
Copy link
Author

In theory it could be possible for primitive types like str, String, i32 etc.

FWIW that happens to be my use case, but I can understand the priority being low.

If I can follow up, though: in point of fact I am annotating it this way:

#[derive(ToSchema)]
#[schema(example = "mymommatoldme", value_type = String)]
struct Thing(T);
// a function exists to convert String to Thing(T)

Would some workaround like this allow me to define a pattern? I thought about defining a new type, but I couldn't work out anything specific, because patterns apply only to fields.

@ValentinColin
Copy link
Contributor

ValentinColin commented Jun 19, 2024

A lot of people use newtypes on primitives. Moreover, since the fields pattern, min, max, ... exist on named structures, wouldn't it be nice to add this feature to unnamed structures?

@juhaku
Copy link
Owner

juhaku commented Oct 8, 2024

Such functionality could be evaluated in future.

@juhaku
Copy link
Owner

juhaku commented Oct 22, 2024

Duplicate #1150

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

3 participants