Rename field with wildcard support#1215
Rename field with wildcard support#1215anasshakil wants to merge 4 commits intoexpress-validator:masterfrom
Conversation
| if (typeof actualResult === 'string') { | ||
| context.renameFieldInstance(actualResult, meta); |
There was a problem hiding this comment.
May I ask why you thought of making use of the custom validators?
Changing the meaning of the return of a custom validator will be quite disruptive for everybody, and is certainly a breaking change.
Perhaps a new function in the chain would be a better idea?
There was a problem hiding this comment.
We ran into a problem where we needed to rename the search field on the bases of the input. I thought of the custom validator as a solution because it solves both of the requirements. To avoid the breaking change, we can accept the return type of an object containing a field, for example { express_rename: 'foo' }.
| if (this.fields.length !== 1) { | ||
| throw new Error('Attempt to rename multiple fields.'); | ||
| } | ||
| if (/\.|\*/g.test(originalPath)) { | ||
| throw new Error('Attempt to rename a wildcard field'); | ||
| } |
There was a problem hiding this comment.
Nice thinking here. I suppose that the callback should be able to handle the name to give to a field though -- as it has access to the full original path.
There was a problem hiding this comment.
I had some success on renaming wildcard paths but need to do more work for wildcard support
- Wildcard support - Test cases added for wildcard and non-wildcard field rename
gustavohenke
left a comment
There was a problem hiding this comment.
General question: should the old property be removed when renaming the item?
renameFieldInstance will work for setting the new property with the same value as the old field instance, but it won't touch the old one.
express-validator/src/chain/context-runner-impl.ts
Lines 50 to 54 in eba0cfe
src/chain/validators.ts
Outdated
| * @param evaluator the custom evaluator based on the `CustomValidator` | ||
| * @returns the current validation chain | ||
| */ | ||
| rename(evaluator: CustomValidator): Return; |
There was a problem hiding this comment.
Nice, this is better!
A few comments here though:
- This shouldn't stay in the
Validatorsinterface.ContextHandlersounds more like it. - Let's add a type specific for the rename callback, so that
CustomValidatorcan evolve independently from the rename functionality. - WDYT of accepting a string value too, which works as a shortcut? For example,
rename('banana')automatically renames that field tobanana.
src/context-items/rename-field.ts
Outdated
| import { ContextItem } from './context-item'; | ||
|
|
||
| export class RenameFieldContextItem implements ContextItem { | ||
| message: any; |
There was a problem hiding this comment.
When you move rename out of Validators, I think that this won't be necessary anymore
src/context-items/rename-field.ts
Outdated
| export class RenameFieldContextItem implements ContextItem { | ||
| message: any; | ||
|
|
||
| constructor(private readonly evaluator: CustomValidator, private readonly negated: boolean) {} |
- Shifted rename functionality to context handler
3177f64 to
6e160b1
Compare
Description
This allows to rename fields using rename context item.
Example
Request Body
Output
closes #785
To-do list
Wildcard support
I have added tests for what I changed.