🟑 MediumFix the CodeπŸ› Debug Challenge

Proxy trap modifies original object directly

Buggy Code β€” Can you spot the issue?

function createValidated(obj, schema) {
  return new Proxy(obj, {
    set(target, prop, value) {
      if (schema[prop] && typeof value !== schema[prop]) {
        throw new TypeError(`${prop} must be ${schema[prop]}`);
      }
      target[prop] = value; // directly writing β€” misses setters!
      return true;
    },
    get(target, prop) {
      return target[prop]; // directly reading β€” misses getters!
    }
  });
}

class User {
  get displayName() { return 'User: ' + this._name; }
  set name(v) { this._name = v.trim(); }
}

const u = new User();
const validated = createValidated(u, { _name: 'string' });
validated.name = '  Alice  '; // setter should trim β€” but doesn't!

Fixed Code

function createValidated(obj, schema) {
  return new Proxy(obj, {
    set(target, prop, value, receiver) {
      if (schema[prop] && typeof value !== schema[prop]) {
        throw new TypeError(`${prop} must be ${schema[prop]}`);
      }
      // Reflect.set correctly handles prototype setters and receiver
      return Reflect.set(target, prop, value, receiver);
    },
    get(target, prop, receiver) {
      // Reflect.get correctly handles prototype getters and receiver
      return Reflect.get(target, prop, receiver);
    }
  });
}

const u = new User();
const validated = createValidated(u, { _name: 'string' });
validated.name = '  Alice  '; // setter trims: _name = 'Alice' βœ“
console.log(validated.displayName); // 'User: Alice' βœ“

Bug Explained

Bug: target[prop] = value bypasses setter methods on the prototype. target[prop] for get bypasses getter methods. Always use Reflect inside Proxy traps.

Explanation: Reflect methods are designed to work correctly with prototype chains, getters/setters, and the receiver (this). Using target[prop] directly bypasses the prototype.

Key Insight: Always use Reflect.get/set/has/deleteProperty inside Proxy traps. They handle the receiver correctly, making getters and setters work as expected.

More Fix the Code Debug Challenges

🟒 EasyDeep clone breaks with JSON.stringifyβ†’πŸŸ‘ Mediumthis lost in event listenerβ†’πŸŸ‘ MediumObject mutation in array mapβ†’πŸ”΄ HardPrototype pollution vulnerabilityβ†’

Practice spotting bugs live β†’

38 debug challenges with AI hints

πŸ› Try Debug Lab