-
-
Notifications
You must be signed in to change notification settings - Fork 882
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
Type of sibling property of a ref property is unknown when validated via JTDSchemaType #2167
Comments
you need to pass interface for definitions as a second parameter: https://github.com/ajv-validator/ajv/blob/master/spec/types/jtd-schema.spec.ts#L356 |
ah actually it is passed... cc @erikbrinkman |
@ento Thanks for catching this, I'm surprised that that you're the first person to report it! The current code path for compiling existing schemas looks like this: Line 374 in 490eb8c
Due to the default types, it seems that typescript is reinterpreting Changing it to infer the refs like: compile<T = unknown, R extends Record<string, unknown> = Record<string, unknown>>(schema: JTDSchemaType<T, R>, _meta?: boolean): ValidateFunction<T> seems to get typescript to propagate the refs you specified appropriately. I'll create a diff to fix this, hoping that it doesn't break anything else. Unfortunately, the typing of |
There seems to be some weird behavior with typescript when refs was left to default. By specifying it manually the behavior was fixed. fixes ajv-validator#2167
After writing the fix, I realized that I was a little wrong. It' picking up the ref type appropriately, but not the non-ref type. This fix seems to work and makes sense, so I'm not going to dig into this any more, but it is odd. I'll also note than when I tried to stub out compile on its own, typescript didn't let me without refs, so there's so more complications due to the overloading and member function nature. |
There seems to be some weird behavior with typescript when refs was left to default. By specifying it manually the behavior was fixed. fixes ajv-validator#2167
There seems to be some weird behavior with typescript when refs was left to default. By specifying it manually the behavior was fixed. fixes ajv-validator#2167
What version of Ajv are you using? Does the issue happen if you use the latest version?
8.11.2 (latest as of this writing)
Ajv options object
JTD Schema
Sample data
Your code
https://replit.com/@ento/IntrepidIndolentOutput#index.ts
^
strictNullChecks
is enabled intsconfig.json
Validation result, data AFTER validation, error messages
TypeScript's typecheck errors:
What results did you expect?
The type of
stringProp
to bestring
after being narrowed byvalidate(data)
Are you going to resolve the issue?
My current workaround is to hold the definition in a const and use it to compose the schema object. (The actual code is a bit more complex than this.)
The text was updated successfully, but these errors were encountered: