0

I am building a simple validation library and I need to use Proxy because I want to accept custom validation rules as chain object. I build something and its works properly on modern browsers but not works on IE11, I tried with proxy-polyfill but its also not work properly. My proxy code is below.

function contextProxy(context) {
 return new Proxy(context, {
  get(obj, prop) {
    if (prop in obj) {
      return obj[prop];
    }

    const newContext = contextProxy(context._clone());

    if (definedRules.hasOwnProperty(prop)) {
     return newContext._takeRule(definedRules[prop]);
    }
    if (customRules.hasOwnProperty(prop)) {
     return newContext._takeRule(customRules[prop]);
    }
   },
  });
}

And I use that proxy;

function validationL() {
  return contextProxy(new ValidationLContext());
}

And I have definedRules object;

const definedRules = {
 numeric: function () {
   return function (text) {
    return /^\d+$/.test(text);
  };
 },

 lowercase: function () {
   return function (text) {
    return /^([a-z]+\s*)+$/.test(text);
   };
 },

 uppercase: function () {
   return function (text) {
     return /^([A-Z]+\s*)+$/.test(text);
   };
 },

 minLength: function (min) {
  return function (text) {
    return text.length >= min;
  };
 },

maxLength: function (max) {
  return function (text) {
    return text.length <= max;
  };
},

alphaNumeric: function () {
  return function (text) {
    return /^([a-zA-Z0-9 _-]+)$/i.test(text);
  };
},

specialChars: function () {
  return function (text) {
    return !/^([a-zA-Z0-9 _-]+)$/i.test(text);
  };
},

email: function () {
  return function (text) {
    return /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(
      text
    );
  };
 }
};

ValidationLContext.js

function ValidationLContext(isNot = false, chain = []) {
 this.chain = chain;
 this.isNot = isNot;
}

ValidationLContext.prototype.not = function () {
 this.isNot = true;
 return this;
};

ValidationLContext.prototype._takeRule = function (ruleFn) {
 return (...args) => {
   this.chain.push({ fn: ruleFn.apply(this, args), isNot: this.isNot });
    if (this.isNot) {
     this.isNot = false;
    }
    return this;
   };
 };

 ValidationLContext.prototype.validate = function (text) {
  return this.chain.every((c) =>
   c.isNot ? !c.fn.call(this, text) : c.fn.call(this, text)
  );
 };

 ValidationLContext.prototype._clone = function () {
   return new ValidationLContext(this.isNot, this.chain);
 };

 export default ValidationLContext;

So library usage like this;

validationL().numeric().minLength(3).validate("123"); 
validationL().not().numeric().minLength(3).validate("123"); 

I can use like above on modern browsers like Chrome but when I try on IE11 only not() function works so only objects functions can work. Can anyone help me with this.

zblash
  • 133
  • 2
  • 11
  • `=>` and `...` do not work at all in IE – mplungjan Nov 04 '20 at 16:05
  • I know and webpack convert them so the problem is not about them – zblash Nov 04 '20 at 16:11
  • 1
    That would be useful info to include in your question. So what are the errors in the console? – mplungjan Nov 04 '20 at 16:12
  • IE11 not throw any error and also it does not work – zblash Nov 04 '20 at 16:15
  • Given the rules are known beforehand, you don't need proxies for this. Getters (or even just dynamically defined methods) will suffice. – Bergi Nov 04 '20 at 16:41
  • @Bergi sorry I don't understrand how can I use getters for this usage validationL().numeric().minLength(3).validate("123") – zblash Nov 04 '20 at 22:44
  • No, you don't even need getters for that. You'd need them for something like `validationL().not.numeric.validate(123)`. – Bergi Nov 04 '20 at 22:49
  • I suggest you try to print some info in the console at every step of your code and try to debug the JS code using the developer tools that may help you to find the problematic code where execution gets stuck. Then you can check that line of code and see whether the methods and properties that you had used in a code are supported in the IE browser or not. It can help to narrow down the issue. – Deepak-MSFT Nov 05 '20 at 02:17
  • Also perhaps post a [mcve] using the bableised code from the IE11 developer tools source – mplungjan Nov 05 '20 at 06:32
  • I tried with console log for catch exception and I found problem. Problem is about external functions like numeric Proxy throw error on numeric but Chrome does not throw any error. IE error is Reference error. I think polyfill not working properly like builtin Proxy – zblash Nov 05 '20 at 06:56
  • I need to use like validationL().numeric().minLength(3).validate("123"); because. I give argument to functions like minLength – zblash Nov 05 '20 at 06:58
  • If possible then try to provide a picture of the error message that may help to find some hint about the issue. If possible then explicitly transpile the code and try to make a test with it in the IE browser. See whether it works or not. – Deepak-MSFT Nov 05 '20 at 08:02
  • I can't take a picture right now because I'am on Linux but I think I found the problem. Problem is proxy-polyfill does not provide all functionality builtin Proxy and I think its imposible, so I decided to change my library's structure. Thank you all for helping me – zblash Nov 05 '20 at 08:26
  • From the previous comment, it looks like you had found the root cause of the issue. After changing the library structure, you can try to post your solution as an answer to this question may help other community members in the future for similar kinds of issues. Thanks for your understanding. – Deepak-MSFT Nov 06 '20 at 02:25

0 Answers0