-
-
Notifications
You must be signed in to change notification settings - Fork 877
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
validating and coercing comma separated values #2220
Comments
I just found this module open-api-coercer that has some overlap with ajv but is still probably the best option available. |
I keep the conversation going with myself :) I just saw this code in ajv-keywords that shows that it should indeed be possible to perform some transformation based on a keyword. Can this transformation change the type to an array or an object and still let ajv validate this array/object afterward without breaking everything, I don't know yet. I also saw that what I was trying to achieve with a close integration of ajv in express routing is extremely similar to what fastify promotes. I am convinced that a keyword that would perform some advanced coercion/parsing compatible with various strategies of query string serialization could bring some value. For openapi users a small tool converting openapi parameter definitions to and from json schemas with this keyword would close the loop. |
Yeah, same here also, when use coerTypes: 'array' this will convert to array base on first param. for example https://localhost:3000/search?item=1,2,3,4,5 wiil transform item query string to item: [1,2,3,4,5] but skipped the validation like maxItems and uniqueItems property doens't apply properly. |
I agree a custom keyword or external lib is the way to go, definitely not something we'll add to ajv core. |
What version of Ajv you are you using?
8.12.0
What problem do you want to solve?
Elegant validation and coercion of query parameters with comma separated values or other serialization rules.
Ok, I realize that this is probably a long shot and maybe not in the scope of ajv, but I still think the use-case is interesting and maybe somebody has something useful to reply.
I use query parameters serialized using open-api rules (https://swagger.io/docs/specification/serialization/), then in an express server I use ajv to validate the query parameters and coerce them. To do this I create a schema to match req.query that also serves as a type guard for a matching typescript Type. This way I get validation, coercion and type guarding in a single step in ajv based on the initial query-string parsing performed by express. This works great for simple types, but for arrays in non-exploded format (?param=val1,val2) not so much.
What do you think is the correct solution to problem?
I see a few options:
Will you be able to implement it?
I might be able to implement a plugin, but I don't know if ajv is extensible enough to do it properly. Can a user-defined keyword be used to extend type coercion ?
Also I might implement it as a separate tool complementary with ajv instead of a plugin, but I wanted to try and get some feedback here first.
The text was updated successfully, but these errors were encountered: