-
-
Notifications
You must be signed in to change notification settings - Fork 615
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
Rename field with wildcard support #1215
Open
anasshakil
wants to merge
4
commits into
express-validator:master
Choose a base branch
from
anasshakil:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { Context } from '../context'; | ||
import { ContextBuilder } from '../context-builder'; | ||
import { Meta } from '../base'; | ||
import { RenameFieldContextItem } from './rename-field'; | ||
|
||
let context: Context; | ||
let validator: jest.Mock; | ||
let validation: RenameFieldContextItem; | ||
let meta: Meta; | ||
|
||
beforeEach(() => { | ||
jest.spyOn(context, 'addError'); | ||
validator = jest.fn(); | ||
}); | ||
|
||
const createSyncTest = (options: { returnValue: any; isWildcard: boolean }) => async () => { | ||
validator.mockReturnValue(options.returnValue); | ||
await validation.run(context, options.returnValue, meta); | ||
if (options.isWildcard) { | ||
expect(context.getData()).toStrictEqual([ | ||
{ | ||
location: 'body', | ||
path: 'bar.foo', | ||
originalPath: 'foo.bar', | ||
value: 'Hello World!', | ||
originalValue: 123, | ||
}, | ||
]); | ||
} else { | ||
expect(context.getData()).toStrictEqual([ | ||
{ | ||
location: 'body', | ||
path: 'bar', | ||
originalPath: 'foo', | ||
value: 'Hello World!', | ||
originalValue: 123, | ||
}, | ||
]); | ||
} | ||
}; | ||
|
||
describe('Rename wildcard paths', () => { | ||
beforeAll(() => { | ||
meta = { | ||
req: { body: { foo: { bar: 'foobar' } } }, | ||
location: 'body', | ||
path: 'foo.bar', | ||
}; | ||
context = new ContextBuilder().setFields(['foo.bar']).setLocations(['body']).build(); | ||
}); | ||
beforeEach(() => { | ||
context.addFieldInstances([ | ||
{ | ||
location: 'body', | ||
path: 'foo.bar', | ||
originalPath: 'foo.bar', | ||
value: 'Hello World!', | ||
originalValue: 123, | ||
}, | ||
]); | ||
validation = new RenameFieldContextItem(validator, false); | ||
validation.message = 'nope'; | ||
}); | ||
it( | ||
'Renames the field foo.bar to bar.foo', | ||
createSyncTest({ returnValue: 'bar.foo', isWildcard: true }), | ||
); | ||
it('Renames the wildcard field with nested objects and arrays', async () => { | ||
meta = { | ||
req: { body: { bar: [{ foo: { end: 'Hello World!' } }] } }, | ||
location: 'body', | ||
path: 'bar.*.foo.end', | ||
}; | ||
context = new ContextBuilder().setFields(['bar.*.foo.end']).setLocations(['body']).build(); | ||
context.addFieldInstances([ | ||
{ | ||
location: 'body', | ||
path: 'bar.*.foo.end', | ||
originalPath: 'bar.*.foo.end', | ||
value: 'Hello World!', | ||
originalValue: 123, | ||
}, | ||
]); | ||
validator.mockReturnValue('foo.*.bar.*.child.new_field'); | ||
await validation.run(context, 'foo.*.bar.*.child.new_field', meta); | ||
expect(context.getData()).toStrictEqual([ | ||
{ | ||
location: 'body', | ||
path: 'foo[0].bar[0].child.new_field', | ||
originalPath: 'bar.*.foo.end', | ||
value: 'Hello World!', | ||
originalValue: 123, | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('Rename non-wildcard fields', () => { | ||
beforeAll(() => { | ||
meta = { | ||
req: { body: { foo: 'Hello World!' } }, | ||
location: 'body', | ||
path: 'foo', | ||
}; | ||
context = new ContextBuilder().setFields(['foo']).setLocations(['body']).build(); | ||
}); | ||
beforeEach(() => { | ||
context.addFieldInstances([ | ||
{ | ||
location: 'body', | ||
path: 'foo', | ||
originalPath: 'foo', | ||
value: 'Hello World!', | ||
originalValue: 123, | ||
}, | ||
]); | ||
validation = new RenameFieldContextItem(validator, false); | ||
validation.message = 'nope'; | ||
}); | ||
it('Renames the field foo to bar', createSyncTest({ returnValue: 'bar', isWildcard: false })); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { CustomValidator, Meta } from '../base'; | ||
import { Context } from '../context'; | ||
import { ContextItem } from './context-item'; | ||
|
||
export class RenameFieldContextItem implements ContextItem { | ||
message: any; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you move |
||
|
||
constructor(private readonly evaluator: CustomValidator, private readonly negated: boolean) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto for |
||
|
||
async run(context: Context, value: any, meta: Meta) { | ||
try { | ||
const result = this.evaluator(value, meta); | ||
const actualResult = await result; | ||
const isPromise = result && result.then; | ||
const failed = this.negated ? actualResult : !actualResult; | ||
|
||
// A promise that was resolved only adds an error if negated. | ||
// Otherwise it always suceeds | ||
if ((!isPromise && failed) || (isPromise && this.negated)) { | ||
return; | ||
} | ||
// rename field if return type is string | ||
if (typeof actualResult === 'string') { | ||
context.renameFieldInstance(actualResult, meta); | ||
} | ||
} catch (err) {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this is better!
A few comments here though:
Validators
interface.ContextHandler
sounds more like it.CustomValidator
can evolve independently from the rename functionality.rename('banana')
automatically renames that field tobanana
.