-
-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Rename functionality separated from CustomValidation
- Wildcard support - Test cases added for wildcard and non-wildcard field rename
- Loading branch information
1 parent
c2a7195
commit 5d4dafc
Showing
8 changed files
with
194 additions
and
8 deletions.
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
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; | ||
|
||
constructor(private readonly evaluator: CustomValidator, private readonly negated: boolean) {} | ||
|
||
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