-
-
Notifications
You must be signed in to change notification settings - Fork 617
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
feat: add 'rename' context-item #971
base: master
Are you sure you want to change the base?
Changes from all commits
7a1f079
a663a4b
d19d8d3
55097d4
db52eb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,58 @@ | ||||||
import { ContextBuilder } from '../context-builder'; | ||||||
import { Meta } from '../base'; | ||||||
import { Rename } from './rename'; | ||||||
|
||||||
it('the new path is identical to the old one', () => { | ||||||
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. nitpicking, but I usually like to make test names read nicely with the
Suggested change
|
||||||
const context = new ContextBuilder().setFields(['foo']).build(); | ||||||
const meta: Meta = { req: {}, location: 'body', path: 'foo' }; | ||||||
|
||||||
expect(new Rename('foo').run(context, 'value', meta)).resolves; | ||||||
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.
Suggested change
though I'm afraid that |
||||||
}); | ||||||
|
||||||
it('throws an error if trying to rename more than one field', async () => { | ||||||
const context = new ContextBuilder().setFields(['foo', 'bar']).build(); | ||||||
const meta: Meta = { req: {}, location: 'body', path: 'foo' }; | ||||||
|
||||||
await expect(new Rename('_foo').run(context, 'value', meta)).rejects.toThrow(); | ||||||
}); | ||||||
|
||||||
it('throws an error if using wildcards in new path', async () => { | ||||||
const context = new ContextBuilder().setFields(['foo']).build(); | ||||||
const meta: Meta = { req: {}, location: 'body', path: 'foo' }; | ||||||
|
||||||
await expect(new Rename('foo.*').run(context, 'value', meta)).rejects.toThrow(); | ||||||
}); | ||||||
|
||||||
it('throws an error if the new path is already assigned to a property', async () => { | ||||||
const context = new ContextBuilder().setFields(['foo']).build(); | ||||||
const meta: Meta = { | ||||||
req: { | ||||||
body: { | ||||||
foo: 'value', | ||||||
_foo: 'bar', | ||||||
}, | ||||||
}, | ||||||
location: 'body', | ||||||
path: 'foo', | ||||||
}; | ||||||
|
||||||
await expect(new Rename('_foo').run(context, 'value', meta)).rejects.toThrow(); | ||||||
}); | ||||||
|
||||||
it('throws an error if trying to rename to an existing property', () => { | ||||||
const context = new ContextBuilder().setFields(['foo']).build(); | ||||||
const meta: Meta = { | ||||||
req: { | ||||||
body: { | ||||||
foo: 'value', | ||||||
}, | ||||||
}, | ||||||
location: 'body', | ||||||
path: 'foo', | ||||||
}; | ||||||
|
||||||
expect(new Rename('_foo').run(context, 'value', meta)).resolves; | ||||||
expect(meta.req.body).toEqual({ | ||||||
_foo: 'value', | ||||||
}); | ||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as _ from 'lodash'; | ||
import { Meta } from '../base'; | ||
import { Context } from '../context'; | ||
import { ContextItem } from './context-item'; | ||
|
||
export class Rename implements ContextItem { | ||
constructor(readonly newPath: string) {} | ||
|
||
async run(context: Context, value: any, meta: Meta) { | ||
const { req, location, path } = meta; | ||
|
||
if (path !== this.newPath) { | ||
if (context.fields.length !== 1) { | ||
throw new Error('Cannot rename multiple fields.'); | ||
} | ||
|
||
if (this.newPath.includes('*')) { | ||
throw new Error('Cannot use rename() with wildcards.'); | ||
} | ||
Comment on lines
+17
to
+19
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. WDYT of moving this to the constructor/ Just thinking that failing on server start might be a better developer experience than on request. |
||
|
||
if (_.get(req[location], this.newPath) !== undefined) { | ||
throw new Error(`Cannot rename to req.${location}.${path} as it already exists.`); | ||
Comment on lines
+21
to
+22
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. I'm guessing that a client might, due to a bug, send more information than required and litter logs with 5XX errors (as I think it'll probably flow all the way to Perhaps it's best to remove this? |
||
} | ||
|
||
_.set(req[location], this.newPath, value); | ||
_.unset(req[location], path); | ||
Comment on lines
+25
to
+26
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. These should obey the |
||
} | ||
return Promise.resolve(); | ||
} | ||
} |
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.
Is it correct to change
instance.path
?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.
I'll guess no because of the below:
express-validator/src/context.ts
Lines 66 to 70 in 6715a6a
but we can always change behaviour.
And side comment, I'd just find it a bit more classy if we could use context's APIs instead of checking for the specific implementation of
Rename
here.