Replies: 1 comment
-
@cykl I'm sorry that I wasn't aware of this thread. I would write as follows (using YAVI 0.14) Arguments1Validator<String, Integer> limitValidatgor = StringValidatorBuilder.of("limit", c -> c.isInteger())
.build(s -> s != null && !s.isBlank() ? Integer.parseInt(s) : 1_000)
.andThen(IntegerValidatorBuilder.of("limit", c -> c.notNull().positive().lessThanOrEqual(1_000)).build());
Arguments1Validator<MultiValueMap<String, String>, PaginationParam> validator = Yavi.arguments()
._string("cursor", c -> c.notBlank())
._string(limitValidatgor)
.apply(PaginationParam::new)
.compose(queryParams -> Arguments.of(queryParams.getFirst("cursor"), queryParams.getFirst("limit"))); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm evaluating YAVI in the context of query parameter validation in a JAX-RS application. I'm struggling a bit a find a good design for this use case and some guidance would be greatly appreciated.
The problem can be reduced to this scenario:
Validating the record itself is easy (ex: check than limit is positive and smaller than a given number). However, I wonder how to handle the String -> int conversion part. I want to report a ConstraintViolation if
limit
parameter is not a number, and set it to a default value if not set as query param.My understanding is that ArgumentValidator could be used for this task but I don't see and elegant way to perform the string validation -> string to int conversion- > int validation sequence. I currently have something like this that I don't really like:
Issues:
Is there a better way to do it? Can I move the
.constraint( PaginationParam::maxPageSize, "limit", c -> c.positive( ).lessThanOrEqual( 1_000 ) )
afterandThen
and ideally avoid field name duplication?My challenge is that limit is an integer
Beta Was this translation helpful? Give feedback.
All reactions