ajv-validator / ajv-validator/ajv
Ability for Formatters to modify data
- 主要语言
- TypeScript
- 星标
- 14.8k
- 派生
- 1k
- PR 合并指标
- 30 天内没有已合并 PR
描述
**What version of Ajv you are you using?**
Latest
**What problem do you want to solve?**
Along the same lines as how [other features can modify data](https://ajv.js.org/guide/modifying-data.html#modifying-data-during-validation), It would be very convenient if there as some way for "formatters" to transform data while validating it.
One use case is the `uuid` format. UUID equality is case-insenstive so it would be very helpful if it could be transformed to a consistent casing during validation.
**What do you think is the correct solution to problem?**
I think adding a `transform` function to the validator would be the best approach.
```ts
ajv.addFormat(
"my-format",
/^a-z\$_[a-z$_0-9]*$/,
(v) => v.toLocalLowerCase() // transform to lowercase
)
```
I think it would maybe make sense to run this BEFORE the format validation itself executes, that way you have a chance to do a little bit of cleanup if needed.
**Will you be able to implement it?**
Possibly... Would have to get more familiar with the codebase. I'm sure there are probably edge cases that make this difficult (like primitives being passed by value). If this is a feature that would be accepted, I could make an attempt.
贡献指南
评估
这个 Issue 还没有评估数据。