Skip to content
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

Closed
albanm opened this issue Feb 16, 2023 · 4 comments
Closed

validating and coercing comma separated values #2220

albanm opened this issue Feb 16, 2023 · 4 comments

Comments

@albanm
Copy link

albanm commented Feb 16, 2023

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:

  • Outside of ajv scope, use a parameterized query parser that interprets open api parameter definitions and fills req.query accordingly. This seems like a pretty natural thing to do but I haven't found the right module for it yet and there would be a lot of overlap with what ajv does pretty well already.
  • Add a custom keyword (maybe stringCoercion) to JSON schema (probably an object whose content would closely match open api vocabulary: form, explode, spaceDelimited, etc) and use these rules in ajv to perform advanced coercion from string to array.
  • same as the previous one but done in a plugin

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.

@albanm
Copy link
Author

albanm commented Feb 16, 2023

I just found this module open-api-coercer that has some overlap with ajv but is still probably the best option available.

@albanm
Copy link
Author

albanm commented Feb 17, 2023

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.

@alrasyidin
Copy link

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.

@jasoniangreen
Copy link
Collaborator

I agree a custom keyword or external lib is the way to go, definitely not something we'll add to ajv core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants