yassun7010 / serde_valid

JSON Schema based validation tool using serde.
MIT License
45 stars 10 forks source link

This add the possibility to define minimum and maximum for Duration #60

Closed StivenAigleIxxi closed 3 months ago

StivenAigleIxxi commented 4 months ago

With the help of some crate like serde_with, it is possible to serialize and deserialize Duration.

With modification serde_valid is able to define minimum and maximum for this type. To define such minimum, we use the attribute validate with the parameter minimum_duration like this #[validate(minimum_duration="10ms")], "10ms" is transformed into Duration::from_millis(10).

We can also use s and ns prefix

yassun7010 commented 4 months ago

Thanks for your suggestion!

As you wrote in lib.rs, I would like to maintain crates' policy of supporting validation as described in jsonschema.

I think it would be better to have a helper function like #[validate(custom(minimum_duration("10ms"))] to clearly indicate that the validation is not a Jsonschema specification.

StivenAigleIxxi commented 3 months ago

@yassun7010 Is this something achievable with the current implementation of custom, after a brief search I think not but you know better than me.

May be I could try to implement something like #[validate(not_json_schema(minimum_duration("10ms")))], with such things, the custom attribute will remain a tool for the user to implement custom function. And we will have a dedicated attribute to implement, inside the library, validation that are not in json

B14ck6u4rd commented 3 months ago

@yassun7010 @StivenAigleIxxi

I think the not_json_schema is irrelevant in a crate above Serde (that can (de)serialize something other than JSON).

yassun7010 commented 3 months ago

@StivenAigleIxxi @B14ck6u4rd

I've written my idea in https://github.com/yassun7010/serde_valid/pull/63;

Does the method using custom meet your requirements?

The way of the limitation is redundant. But, the resolution by custom maintains the position of this crate (based on JsonSchema).

My concern is that the JsonSchema specification will support equivalent validations in the future under different names.

B14ck6u4rd commented 3 months ago

@StivenAigleIxxi @B14ck6u4rd

I've written my idea in #63;

Does the method using custom meet your requirements?

The way of the limitation is redundant. But, the resolution by custom maintains the position of this crate (based on JsonSchema).

My concern is that the JsonSchema specification will support equivalent validations in the future under different names.

I very like your solution @yassun7010 !

yassun7010 commented 3 months ago

I released v0.20.0.

B14ck6u4rd commented 3 months ago

Thanks for your work @yassun7010 !