mjavascript/practical-modern-javascript

Schema Validation with Proxies possible error

alexamy opened this issue · 1 comments

There is a code on p. 195 (6.1.3 Schema Validation with Proxies) that isnt working for me.

const validations = new Map()
const validator = {
set(target, key, value) {
if (validations.has(key)) {
return validations[key](value)
}
return Reflect.set(target, key, value)
}
}

I suggest to change return validations[key](value) to validations(key)(value), because of:

  1. Map object dont support accessing through [], only through get method.
  2. If we return result of validation, we dont actually reach setting value at target[key]. If we just validate value by direct method call of validations object, as I suggesting, we throw error in that method if value isnt valid, or bypass this method in case of valid value and reach setting the value in Reflect.set.
  1. You mean validations.get(key)(value), right? Could you issue a PR?