Skip to content

Commit

Permalink
feat: assume no wildcards in old path
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Jan 18, 2021
1 parent 55097d4 commit db52eb4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
19 changes: 5 additions & 14 deletions src/context-items/rename.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Meta } from '../base';
import { Rename } from './rename';

it('the new path is identical to the old one', () => {
const context = new ContextBuilder().build();
const context = new ContextBuilder().setFields(['foo']).build();
const meta: Meta = { req: {}, location: 'body', path: 'foo' };

expect(new Rename('foo').run(context, 'value', meta)).resolves;
Expand All @@ -16,20 +16,11 @@ it('throws an error if trying to rename more than one field', async () => {
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' };
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();
});
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 () => {
Expand Down
4 changes: 2 additions & 2 deletions src/context-items/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class Rename implements ContextItem {
if (context.fields.length !== 1) {
throw new Error('Cannot rename multiple fields.');
}
const field = context.fields[0];
if (field.includes('*') || this.newPath.includes('*')) {

if (this.newPath.includes('*')) {
throw new Error('Cannot use rename() with wildcards.');
}

Expand Down

0 comments on commit db52eb4

Please sign in to comment.