-
-
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 2 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,67 @@ | ||||||
import { ContextBuilder } from '../context-builder'; | ||||||
import { Meta } from '../base'; | ||||||
import { Rename } from './rename'; | ||||||
|
||||||
it('the new path is identical to the old one', () => { | ||||||
const context = new ContextBuilder().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(); | ||||||
}); | ||||||
|
||||||
describe('throws an error if using wildcards', () => { | ||||||
it('in context fields', 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('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.'); | ||
} | ||
const field = context.fields[0]; | ||
if (field.includes('*') || this.newPath.includes('*')) { | ||
throw new Error('Cannot use rename() with wildcards.'); | ||
} | ||
|
||
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? |
||
} | ||
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 errors are thrown at runtime, should we use |
||
|
||
_.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.
nitpicking, but I usually like to make test names read nicely with the
it
.For example: