question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error: Maximum call stack size exceeded with schemas having ~510 properties or more

See original GitHub issue

While this was originally posted on https://github.com/ajv-validator/ajv-cli/issues/134 it proved to not be caused by the cli itself, but being more of a generic issue with ajv schema loader.

I recently discovered that bigger generated schemas can easily produce a a error: Maximum call stack size exceeded, even when the checks are quite simple.

ajv -- validate --strict=false --errors=json -s f/ansible-playbook.json -d /Users/ssbarnea/c/a/schemas/examples/playbooks/run.yml
schema f/ansible-playbook.json is invalid
error: Maximum call stack size exceeded

Based on my initial tests 500 properties worked but 1000 generated this error, which is a very low number. My expectation that that I will likely need 10k in the end. I tested with 510 and job only one schema failing instead of both, with 550 both failed. Clearly the moment it starts to fail is just after 500.

I am not sure what is confusing the loader because in the end the entries are very simple.

Here is one file that has ~1000 entries https://sbarnea.com/ss/ansible-playbook.json and is only ~100kb, just removing some of the properties will make it pass.

I also observed that if I reduce the number of properties to be near the limit, i also get an interesting behavior where it randomly fails with Maximum call stack size exceeded on each run. As soon you increase the number bit more it fails all the time.

I tried to use the API directly in order to to some of the options, like loopRequired as I was told by @epoberezkin but I was unable to find any configuration option that would prevent this bug from occurring.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
epoberezkincommented, May 9, 2021

The above suggestions work: see https://runkit.com/esp/6097b403293f88001a7fbb96

Using allErrors: true makes validation function shallow, so it doesn’t exceed the stack regardless of the number of properties (I tried 10000 too).

Using $ref with inlineRefs: false option substantially reduces the size of the validation function.

0reactions
epoberezkincommented, May 4, 2021

Generating code from the 500K JSON schema file resulted in a 4.3 Mb standalone parser javascript file.

That’s not that bad, given that JSON Schema is a much higher level (however restrictive) language than JS 😃

The problem is that with “properties” keyword each property is compiled into its own code, but if there is a substantial similarity between them, it would be highly repetitive…

Some optimisation ideas:

  1. Try using allErrors: true option - it should results in the code with less nesting of conditions (which is what causing deep recursion stack). It would collect all errors, though, rather than bail on the first one.
  2. Try extracting (programmatically) any share parts into definitions and use inlineRefs: false (to make sure that the definition is compiled into a separate function and not just inlined every time it is used).

As a library improvement, there might be a possibility to break recursion into async code, but not sure how widely it is needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Maximum call stack size exceeded error
It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you...
Read more >
JavaScript RangeError: Maximum Call Stack Size Exceeded
The JavaScript RangeError: Maximum call stack size exceeded is an error that occurs when there are too many function calls, or if a...
Read more >
Changelog - joi.dev
Strict object schema with a boolean property expects alternative, not boolean ... Maximum call stack size exceeded​​ error for dataUri validation.
Read more >
Maximum call stack size exceeded when filtering objects with ...
Giving the following schema --> const Person = { name: "User", primaryKey: "_id", properties: { _id: "objectId", name: "string", ...
Read more >
Maximum call stack size exceeded - Discuss - ProseMirror
... except on copy paste cut operations i sometimes get “Maximum call stack size exceeded” errors. ... I have defined it in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found