Skip to content

.parse is not available on TsjsonParser #10

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

Closed
moltar opened this issue Nov 3, 2019 · 7 comments
Closed

.parse is not available on TsjsonParser #10

moltar opened this issue Nov 3, 2019 · 7 comments

Comments

@moltar
Copy link

moltar commented Nov 3, 2019

This:

  public parse = (text: string, skipValidation = false): Validated<T> => {
    const data: unknown = JSON.parse(text);

Gets compiled to:

class TsjsonParser {
    constructor(schema) {
        this.parse = (text, skipValidation = false) => {
            const data = JSON.parse(text);
            if (skipValidation) {
                return data;
            }
            this.validate(data);
            return data;
        };
        this.schema = schema;
        const ajv = new ajv_1.default();
        this.validator = ajv.compile(schema);
    }

And IDE cannot infer this as an available class method.

image

@ostrowr
Copy link
Owner

ostrowr commented Nov 3, 2019

That's odd. Which version of TypeScript are you using in your IDE? Intellisense should infer

  • schema
  • validate
  • parse

There's no data anywhere.

I've made a bunch of changes in the last 2 days, can you try latest master and see if you're still running into this issue?

The generated code looks right, but can you check if the generated types match as well? tsjson-parser.d.ts should have something like

export declare class TsjsonParser<T extends SchemaLike> {
    readonly schema: T;
    private readonly validator;
    constructor(schema: T);
    validate(data: unknown): asserts data is Validated<T>;
    parse: (text: string, skipValidation?: boolean) => T[typeof InternalTypeSymbol];
}

@moltar
Copy link
Author

moltar commented Nov 3, 2019

@moltar
Copy link
Author

moltar commented Nov 3, 2019

The IDE I'm using is VS Code (latest)

@ostrowr
Copy link
Owner

ostrowr commented Nov 3, 2019

Just cloned your repo and was able to replicate.

Looks like you're using Typescript 3.6.4 (which is a totally reasonable thing to be doing.)

However, ts-json-validator currently requires 3.7+ in order to work properly. (As I warn in the README)

When npm-installing in your repo, I get

npm WARN [email protected] requires a peer of typescript@>= 3.7.0 but none is installed. You must install peer dependencies yourself.

Which I think is enough warning – but if you have an idea to make this more obvious, I'd welcome a suggestion/PR!

I've confirmed that this works in the IDE using 3.7 in your repo.

Closing this but feel free to reopen if you think I've missed something.

@ostrowr ostrowr closed this as completed Nov 3, 2019
@moltar
Copy link
Author

moltar commented Nov 9, 2019

Ok, thanks for looking into this. What features of TS 3.7 is it relying on that are not available in 3.6.4? Thanks.

@moltar
Copy link
Author

moltar commented Nov 9, 2019

Interestingly, after the update, the tests are failing.

export function caseTsJsonValidator(data: Data) {
  return parser.validate(data)
}

It seems return type of the function cannot be inferred and is set to void for some odd reason.

Even though when hovering the return type of validate in IDE, it shows it as a correct return type.

Any ideas?

@ostrowr
Copy link
Owner

ostrowr commented Nov 9, 2019

That's due to microsoft/TypeScript#34596

I'm going to change this to a normal type guard for now (so .validate will return true if it validates, and will narrow the type, and false if not.)

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

2 participants