Skip to content
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

Recursive types #23

Open
zackify opened this issue Jul 30, 2019 · 3 comments
Open

Recursive types #23

zackify opened this issue Jul 30, 2019 · 3 comments

Comments

@zackify
Copy link

zackify commented Jul 30, 2019

Any plans or ideas on how to get this to work with recursive types? I get an error when attempting to use it, Maximum call stack size exceeded I guess for now the alternative is to flatten my data for validation purposes

@ForbesLindesay
Copy link
Owner

Do you get that error at runtime or at compile time?

I would guess if the error happens at compile time it's probably an issue with typescript-json-schema, which is one of our dependencies. An actual small example that fails would be really helpful for debugging this.

If it happens at runtime, I wonder if your data structure is actually recursive in the sense of referencing itself, which is not the same as a recursive type. i.e. the following should be fine

interface Leaf {
  value: string;
}
interface Branch {
  left: Leaf | Branch;
  right: Leaf | Branch;
}
{"left": {"value": "hello"}, "right": {"left": {"value": "beautiful"}, "right": {"value": "world"}}}

but not if you pass it:

const tree = {left: {value: 'hello'}};
tree.right = tree;
validate(tree);

@zackify
Copy link
Author

zackify commented Jul 31, 2019

it is at compile time.

Here's an example:

type Question = {
  name: string;
  options?: Option[];
};

export type Form = {
  questions: Question[];
};

type Option = {
  name: string;
  questions: Question[];
};

npx typescript-json-validator Example.ts Form

In my case I have a schema that has questions, which can have options. Each option can have more questions inside if that option is selected.

Edit: looks like its been mentioned before in that repo: YousefED/typescript-json-schema#33

@ntr-808
Copy link

ntr-808 commented Nov 24, 2020

Just checking in to say this issue still occurs, here is a stack trace:

/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:39425
                    chain = ts.Debug.checkDefined(getSymbolChain(symbol, meaning, /*endOfChain*/ true));
                                                  ^

RangeError: Maximum call stack size exceeded
    at lookupSymbolChainWorker (/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:39425:51)
    at lookupSymbolChain (/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:39418:24)
    at symbolToTypeNode (/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:39590:29)
    at typeToTypeNodeHelper (/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:38764:28)
    at /home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:38585:106
    at withContext (/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:38635:37)
    at Object.typeToTypeNode (/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:38585:28)
    at typeToString (/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:38552:40)
    at Object.typeToString (/home/ntr/synth-library/node_modules/typescript-json-schema/node_modules/typescript/lib/typescript.js:35213:24)
    at JsonSchemaGenerator.getClassDefinition (/home/ntr/synth-library/node_modules/typescript-json-schema/dist/typescript-json-schema.js:578:32)

and the offending type:

import GeoJSON from 'geojson';

type Uuid = string;

export type Scene = {
    id: Uuid,
    name: string,
    parent: Uuid | null,
    children: Scene[],
    features: GeoJSON.Feature[],
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants