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

Range for standard number types #298

Open
jakob0 opened this issue Jun 6, 2024 · 2 comments
Open

Range for standard number types #298

jakob0 opened this issue Jun 6, 2024 · 2 comments

Comments

@jakob0
Copy link

jakob0 commented Jun 6, 2024

Is there a reason, why no default minimum and maximum values are generated with schema_for! for rust built-in number types, like i8-i128, u8-u128, isize, usize, f32, f64?

@GREsau
Copy link
Owner

GREsau commented Nov 22, 2024

I try to strike a balance simplicity/clarity with specificity - including minimum/maximum would definitely make the schemas more specific, but could also increase noise in the output.

I suppose that the smaller the range of possible values, the more valuable it would be to specify the actual min/max. Considering each type:

  • u8/i8 - including minimum/maximum for these would probably be useful, so I'm very open to it
  • u16/i16 - I'm open to including minimum/maximum for these types too
  • u32/i32 - here's where I'm less sure. I'm a little concerned that some people choose these types for their integers somewhat arbitrarily, rather than making a conscious choice about the actual range. Although maybe that's not the case, and even if it is maybe it shouldn't really be relevant
  • u64/i64 - similar to above, and also -9223372036854775808/9223372036854775807/18446744073709551615 are large enough that they're starting to add noticeable noise to the schema which reduces human-readability. They also can't be accurately represented as a double-precision floating point number, which is allowed by JSON (and serde_json and JSON schema) but can cause interoperability problems with other languages/frameworks
  • u128/i128 - as above, plus these can't be serialized by serde_json without the arbitrary_precision feature
  • f32/f64 - I'm concerned that including minimum/maximum for these could be misleading due to rounding and imprecision

I'm curious what your use-case is for wanting these values, as that may help make a decision as to how schemars should behave

@jakob0
Copy link
Author

jakob0 commented Nov 25, 2024

You are right, it would definitely be much more relevant for the small types. I just thought, it might help guard against overflow errors - especially for people who choose their integers 'somewhat arbitrarily'. I must admit, I did not really care about the simplicity in human-readable schemas, but I get your point.
About my use-case: I'm using the schemas to generate ui-forms (like jsonforms.io) to initialize my rust structs. For the input, I need to set the limits, so I don't get dangerous errors later on.
For me it's not that relevant anymore, since I have my custom implementation now (taking into account the limits of JSON and f64, like you mentioned). It has never been a real issue anyways, I was just curious.
Thanks for the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants