diff --git a/src/makeSecureDeepProxy.test.ts b/src/makeSecureDeepProxy.test.ts index e88e7c7..be31b64 100644 --- a/src/makeSecureDeepProxy.test.ts +++ b/src/makeSecureDeepProxy.test.ts @@ -139,4 +139,15 @@ describe('makeSecureDeepProxy', () => { proxy.a[1].b; }, /Property b is not defined in the config/); }); + + it('allows JSON.stringify on secure config', () => { + const config = { + a: 1, + nested: { b: 'text' }, + }; + + const proxy = makeSecureDeepProxy(config); + + assert.equal(JSON.stringify(proxy), '{"a":1,"nested":{"b":"text"}}'); + }); }); diff --git a/src/makeSecureDeepProxy.ts b/src/makeSecureDeepProxy.ts index ea78b4a..b207495 100644 --- a/src/makeSecureDeepProxy.ts +++ b/src/makeSecureDeepProxy.ts @@ -29,8 +29,10 @@ function makeSecureProxy(targetObject: T): T { // Handle Symbol and Array properties if ( typeof prop === 'symbol' || - // Don't throw error when using array methods - (Array.isArray(target) && Number.isNaN(Number(prop))) + /** Don't throw error when using array methods */ + (Array.isArray(target) && Number.isNaN(Number(prop))) || + /** Allow stringifying proxied config (because JSON.stringify calls this method) */ + prop === 'toJSON' ) { return Reflect.get(target, prop); }