-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Validation fails when running via JsDom in NodeJs #5151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@bartbutenaers |
@archmoj I'm a little wary of that test for node, as anyone can set their window name to whatever they want. And anyway it's not node per se, but the jsDom VM that's causing issues. But anyway if we do find a reasonable test we can put it up above in the special imagetest block: plotly.js/src/lib/is_plain_object.js Lines 19 to 22 in 783de52
That comment alludes to the reason for the second part of the test: that we don't want to accept more complex objects function f() {}
var a = new f();
Object.prototype.toString.call(a)
> "[object Object]" This isn't a real huge worry, since the majority of our users are coming in via JSON one way or another (plotly.py, dash, etc) but I still don't think we want to completely drop that condition. @bartbutenaers TBH I don't know anymore what the condition plotlyServerDom.window.process = {versions: 1}; Alternatively you might be able to go through JSON as well: var data2 = plotlyServerDom.window.JSON.parse(JSON.stringify(data));
var layout2 = plotlyServerDom.window.JSON.parse(JSON.stringify(layout)); |
Hello @archmoj and @alexcjohnson, Thanks for the fast and kind feedback!
And that works like a charm. Much cleaner than my own workaround. So this workaround is more than enough for my use case! Thanks a lot for your time!! |
Great, I'm glad that worked for you @bartbutenaers 🎉 But let's leave this issue open until we either (1) find a more robust way to test for objects so this isn't needed, (2) create and document a more pleasant way to use the looser test, or (3) add this opaque workaround to the official docs. |
I'm starting to think it would be OK to just drop the second part of this test - again, the only concern is if someone passes in an object created with I did a little digging, and I did find one test that pretty reliably distinguishes a plain object from a more complex constructed object: look for a property that is usually inherited from Object.getPrototypeOf(obj).hasOwnProperty('hasOwnProperty') Which you can trick by overriding Anyway either removing the second condition or replacing it with something like the test above could be the way to go. |
Dear,
we are integrating the powerful Plotly library into the open-source Node-RED iot framework, which is running on NodeJs.
Running Plotly on the NodeJs server (via JsDom) works fine, but calling the
validate
function fails.A small program to reproduce the problem (remark: the jsdom and plotly.js-dist NPM packages need to be installed):
The result is:
Which means that Plotly doesn't recognize my input parameters as Javascript objects, which is checked in the isPlainObject function:
The second check fails, so the input is not considered as a Javascript object...
Seems that the problem is caused by this:
You can find a prove of this theory in the next program. Here a global function
createObject
is declared inside the vm, and called from outside the vm. This allows us to create an empty Javascript object inside the vm, return it back to us so we can add data to the object, and then we pass the object to Plotly (which is running in the Jsdom vm):And that works fine, and the validation is succesful.
We could use this latter program as a workaround, but it becomes harder when the number of nested levels increases. Because all objects that we receive from anywhere in the NodeJs application, we need to create an new object for and copy all the properties. Which is of course not a very neat solution ...
So we would like to ask if it is possible to remove the second condition from the IF statement, since we think that the first condition is enough:
Thanks for your time!
Bart Butenaers
The text was updated successfully, but these errors were encountered: