ggicci/httpin

Parse and validate custom time format

mxp7064 opened this issue · 5 comments

Hi, is there a way to parse and validate custom time format? For example, I want a query parameter which doesn't have to have a timezone, like: 2022-05-07T12:45:13

Thanks in advance!

Hi @mxp7064, here I'd like to talk about two cases:

1. All your code/apis are receiving time parameters without timezone, or more generally speaking, in one format.

We could replace the builtin time.Time type decoder with a custom one. See:

2. In you project, different apis have different formats.

Currently, httpin's decoder interface doesn't support specifying extra parameters like "format". So it's not possible to achieve this.

However, there's a tricky way to handle this. Please take a look at the implementation of the builtin time.Time decoder, you may try different formats to parse the input value.

BTW, the original implementation of the builtin time.Time type decoder also can be enhanced by trying several commonly used time formats. @ggicci

@ggicci Think about using httpin.NewInput(...) to apply a custom type decoder by adding an optional parameter.

Hi @mxp7064, I've introduce a special decoder directive in httpin@v0.10.0. Take a look and I think this would help you.

Documentation: https://ggicci.github.io/httpin/directives/decoder

If it solved your problem, could you please close this issue?

@ggicci Think about using httpin.NewInput(...) to apply a custom type decoder by adding an optional parameter.

It would be more helpful if we can allow overriding the decoder for each field in the struct.