ajv icon indicating copy to clipboard operation
ajv copied to clipboard

ajv.compile not working in react native but working in react web after v7.0.0 release.

Open sahuhardik opened this issue 3 years ago • 13 comments

ajv.compile doesn't works on mobile (On web it is working fine) and crashes the app, looks like it goes in infinite recursion. It was working fine before version 7.0.0

react: 17.0.2 react-native: 0.67.2 ajv: 8.11.0

import Ajv from 'ajv';
const ajv = new Ajv();

const IPasswordRecoveryTokenSchema = {
    type: 'object',
    properties: {
        token: { type: 'string' },
        expiresAtMs: { type: 'number' },
    },
    required: ['token', 'expiresAtMs'],
    additionalProperties: false,
};

export const IPasswordRecoveryTokenValidator = ajv.compile(IPasswordRecoveryTokenSchema);

RangeError: Maximum call stack size exceeded (native stack depth), js engine: hermes

sahuhardik avatar Apr 01 '22 06:04 sahuhardik

I am facing the same issue. Any idea how we can address this or if there are any plans to fix this?

Thanks.

Saikedo avatar Apr 06 '22 16:04 Saikedo

Same issue here. RangeError: Maximum call stack size exceeded (native stack depth)

surushTodzhibekov avatar Nov 11 '22 15:11 surushTodzhibekov

Probably need to add note to the docs that this library doesn't support react native. We have it integrated in our quite huge web app which reuses validation logic with react native, and now when we enabled validation in react native we found this unpleasant fact.

pleerock avatar Nov 11 '22 16:11 pleerock

I have just run into this problem as well using the Hermes JavaScript engine on react-native.

Stepping through the code, it seems to loop on this line.

I suspect we need a polyfill of some sort for react-native. Perhaps the Regex implementation is not correct?

Missing features from ES9 include: Named capture groups. Unicode property escapes.

Also, I'm wondering if anyone has tried the standalone implementation?

@epoberezkin do you have any suggestions for what might be causing this problem?

kilbot avatar Nov 18 '22 17:11 kilbot

On further reading, it looks like it could be a problem with eval(). Hermes does not support local eval. I don't understand the local mode limitation and if this is something that can be worked around.

I guess AJV will not be compatible with react-native/hermes. Anyone working on a validator that does not use eval? 😬

kilbot avatar Nov 18 '22 18:11 kilbot

If Function constructor is not supported by some JavaScript environment, you should be able to compile the schemas during build time (as long as they are not dynamically generated at run time) - see standalone validation functions in docs.

I believe you can use a standard-compliant JS engine with react-native, so the problem is more narrow than react-native - it seems specific to Hermes from the comments.

epoberezkin avatar Nov 18 '22 19:11 epoberezkin

That they made a non-standard JS engine default is surprising…

epoberezkin avatar Nov 18 '22 19:11 epoberezkin

Same issue here. RangeError: Maximum call stack size exceeded (native stack depth)

@surushTodzhibekov this is unrelated - please search the issues - either you used self-referencing object for schema, or schema is too deep, or you used something to de-reference $refs

epoberezkin avatar Nov 18 '22 19:11 epoberezkin

Thanks for your quick reply @epoberezkin!

I think Hermes may be required for the new Fabric architecture (unsure), but it seems that react-native team is pushing everyone to use Hermes.

I'm going to look at standalone implementation for the immediate future.

re: RangeError: Maximum call stack size exceeded (native stack depth)

This is the same error message that I am seeing in Hermes in iOS. Note: it works fine on web and electron. I haven't tested on Android.

kilbot avatar Nov 18 '22 20:11 kilbot

AJV is truly not compatible with the Hermes engine, which is the de-facto new default of react-native. Would be awesome if this issue could be solved.

maximilize avatar Jan 11 '23 22:01 maximilize

After some debugging on my end, it seems that the Function.prototype.call(), Function.prototype.apply() along with Array.prototype.reduce() that is used in recursion by this library is limited by the call stack size of hermes. Which, at this point seems to be 128 calls.

antondalgren avatar May 12 '23 13:05 antondalgren

We are facing the same issue in react-native RangeError: Maximum call stack size exceeded (native stack depth)

arekm213 avatar Aug 22 '23 12:08 arekm213

I am stuck on this error, too. Did anyone come up with a work-around yet?

derwehr avatar Aug 30 '23 12:08 derwehr