I am trying to implement an endpoint definition with a comma separated path parameter, a List[String] (actually each String should be validated with a specific regex).
I was yet unabled to build a proper, validated path parameter definition for this. I could use a regex but I was hoping to have a better list+regex validator.
Should it be supported? Does anyone have examples?
Assuming that MyId is a value class (String). Let me know if you need clarifiaction on how to adjust it to different representation of the path param list element.
If I don’t provide the schema directly it would just show as type: string. The validation pattern is not even there… Why is the validation pattern from validateIterable not being used? And why isn’t a schema for List[Name] required?
.validateIterable has a caveat described in its scaladoc:
* Should only be used if the schema hasn't been created by `.map`ping another one, but directly from `Schema[U]`. Otherwise the shape of
* the schema doesn't correspond to the type `T`, but to some lower-level representation of the type. This might cause invalid results at
* run-time.
so I’m guessing this may be why it has issues in your case?
I tried a simple approach with parsedString and it seems to work as expected and produce proper specs:
opaque type Name = String
val idPatternValidator: Validator[String] = Validator.pattern("^[A-Z]*")
given myIdListCodec: Codec[String, List[Name], TextPlain] = Codec
.parsedString(_.split(",").toList)
.validateIterable(idPatternValidator)
val getUserEndpoint =
endpoint.get
.in(
"user" / path[List[Name]]("id")
)
.out(stringBody)
With ‘one-way’ I mean it is only for decoding. If I use the endpoint definition as a client it does a direct .toString on the List. So I need to specify how to encoding and mkString with ‘,’