Skip to content

Commit

Permalink
Address review
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Jul 18, 2021
1 parent 36704ea commit c71a8a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/middlewares/__snapshots__/one-of.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Array [
]
`;

exports[`should let the user to choose between multiple error types legacy error type 1`] = `
exports[`should let the user to choose between multiple error types flat error type 1`] = `
Array [
Object {
"msg": "Invalid value(s)",
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/one-of.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('error message', () => {
});

describe('should let the user to choose between multiple error types', () => {
let errors: OneOfErrorType[] = ['grouped', 'legacy'];
let errors: OneOfErrorType[] = ['grouped', 'flat'];
it.each(errors)(`%s error type`, async errorType => {
const req: InternalRequest = {
body: { foo: true },
Expand Down
19 changes: 11 additions & 8 deletions src/middlewares/one-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ const dummyItem: ContextItem = { async run() {} };

export type OneOfCustomMessageBuilder = (options: { req: Request }) => any;

export type OneOfErrorType = 'grouped' | 'leastErroredOnly' | 'legacy';

export interface OneOfOptions {
message?: OneOfCustomMessageBuilder | any;
errorType?: OneOfErrorType;
}
export type OneOfErrorType = 'grouped' | 'leastErroredOnly' | 'flat';

export function oneOf(
chains: (ValidationChain | ValidationChain[])[],
options: OneOfOptions = {},
options: { message: OneOfCustomMessageBuilder; errorType?: OneOfErrorType },
): Middleware & { run: (req: Request) => Promise<void> };
export function oneOf(
chains: (ValidationChain | ValidationChain[])[],
options: { message: any; errorType?: OneOfErrorType },
): Middleware & { run: (req: Request) => Promise<void> };
export function oneOf(
chains: (ValidationChain | ValidationChain[])[],
options = {},
): Middleware & { run: (req: Request) => Promise<void> } {
options = { errorType: 'grouped', ...options };

Expand Down Expand Up @@ -63,7 +66,7 @@ export function oneOf(
error = allErrors[leastErroredIndex];
break;
default:
// legacy
// flat
error = _.flatMap(allErrors);
}

Expand Down

0 comments on commit c71a8a5

Please sign in to comment.