Skip to content

Commit

Permalink
print key when guarding object
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Sep 30, 2020
1 parent 966305d commit 1f02527
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ export default class PowerGuardError extends Error {
this.details = details;
}
}

export class PowerGuardKeyError extends Error {
key: string;
gut: PowerGuardError;

constructor(key: string, gut: PowerGuardError) {
super(`Guard "${key}" failed: ${gut.message}`);

this.key = key;
this.gut = gut;
}
}
15 changes: 11 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isObject } from './types';
import { GuardFunction } from './global';
import PowerGuardError from './error';
import PowerGuardError, { PowerGuardKeyError } from './error';

export type DataConfig = {
[key: string]: GuardFunction<unknown>;
Expand All @@ -10,7 +10,7 @@ export type GuardedData<T extends DataConfig> = {
[key in keyof T]: ReturnType<T[key]>;
};

export const guard = <T extends DataConfig>(config: T): GuardFunction<GuardedData<T>> => (
const guard = <T extends DataConfig>(config: T): GuardFunction<GuardedData<T>> => (
data: unknown,
) => {
if (!isObject(data)) {
Expand All @@ -23,7 +23,14 @@ export const guard = <T extends DataConfig>(config: T): GuardFunction<GuardedDat
// @ts-ignore
const value = data[key];

guarded[key] = config[key](value) as ReturnType<T[typeof key]>;
try {
guarded[key] = config[key](value) as ReturnType<T[typeof key]>;
} catch (error) {
if (error instanceof PowerGuardError) {
throw new PowerGuardKeyError(String(key), error);
}
throw error;
}
});

return guarded;
Expand All @@ -35,7 +42,7 @@ export * from './global';

export * from './types';

export { default as PowerGuardError } from './error';
export { default as PowerGuardError, PowerGuardKeyError } from './error';

export { default as array } from './array';

Expand Down
7 changes: 4 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import guard, { number, string, enumGuard, boolean, array, PowerGuardError } from '../src';
import guard, { number, string, enumGuard, boolean, array } from '../src';
import { expect } from 'chai';
import PowerGuardError, { PowerGuardKeyError } from '../src/error';

enum Foo {
key1 = 'key1',
Expand Down Expand Up @@ -50,7 +51,7 @@ describe('Power guard', () => {
bla: 1,
bnd: [{ foo: '123' }],
}),
).throws(PowerGuardError);
).throws(PowerGuardKeyError);

expect(() =>
guardFunc({
Expand All @@ -60,7 +61,7 @@ describe('Power guard', () => {
bla: 1,
bnd: [{ foo: 123 }],
}),
).throws(PowerGuardError);
).throws(PowerGuardKeyError);

expect(() =>
guardFunc(`{
Expand Down

0 comments on commit 1f02527

Please sign in to comment.