cloudflare / cloudflare/cabidela
String formats validation and custom formats?
- Dominant language
- TypeScript
- Stars
- 28
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
Hello, I'm using Cabidela in a Cloudflare Pages worker project but I don't think it handles any string-format validation yet does it?
I see that [pattern validation](https://github.com/cloudflare/cabidela/blob/d17218f718dd9836b491e0e74ecf50cab7edff64/src/index.ts#L377) is implemented, but I want to register a named string format and supply a validator function for it similar to how you do it for [TypeBox and Ajv](https://github.com/sinclairzx81/typebox?tab=readme-ov-file#formatregistry). Just curious if you could tell me how you might expose that feature?
It looks like the correct place to add calls to a validator would be around [here](https://github.com/cloudflare/cabidela/blob/d17218f718dd9836b491e0e74ecf50cab7edff64/src/index.ts#L346). Beyond that, I guess you would add a mapping of string-format validators in the `CabidelaOptions`, something like `formats: Record boolean | string>;`?
My main question about any possible future format validators is the function signature. I suppose I would be safe just returning a `boolean` to `Cabidela`, but that would limit the resulting error messages to `'${pathToString(needle.path)}' is not a valid '${format}'.` and it would be nice, if the validator returned a string, to do something like `'${pathToString(needle.path)}' is not a valid '${format}'. (${result})` where `result` would be the string returned from the validator.
For now I'll start by making my own validator functions just return booleans to be safe.
EDIT: The benefit of just returning a bool will be that I can use some code from [ajv-formats](https://www.npmjs.com/package/ajv-formats), so I guess that should be the standard anyway.
This addition was pretty straightforward, but if you're taking pull requests let me know and I'll send one.
Contributor guide
Assessment
This issue has not been assessed yet.