0

I was validating some options inside class constructor with Proxy but idk why there is resulting differently, because if I compare obj[prop] === value results true.

Code:

const regex = /\d{16,19}/gi
const id = "681219993911951360"

class Test {
    static validator = () => ({
        set(obj, prop, value) {
            switch (prop) {
            case "clientID":
                console.log(
                    typeof obj[prop], 
                    typeof value, 
                    obj[prop] === value, 
                    regex.test(value), 
                    regex.test(obj[prop])
                )

                if (!regex.test(obj[prop]))
                    throw new TypeError("error")
                break
            }

            obj[prop] = value
            return true
        }
    })

    constructor(options) {
        const proxy = new Proxy(options, Test.validator())

        proxy.clientID = options?.clientID
    }
}

new Test({
    clientID: id
})

Output of code above:

string string true true false

In the focus of part with console.log, the types are same, the values are same, but the results are different if I test the value with regex. (4th and 5th values)

Tested this piece of code with Nodejs v14.16.0 and v15.8.0 but there is no difference between them.

Any ideas to solve this?

Thanks

j08691
  • 197,815
  • 30
  • 248
  • 265
zero734kr
  • 1
  • 1

0 Answers0