-
Notifications
You must be signed in to change notification settings - Fork 26
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
2.1.0 breaks TS build #39
Comments
I will look into it ASAP. |
Can you please provide a minimal reproduction repo or example? |
sure https://github.com/mmouterde/bugDemo
|
Basically app.post('/template', validate({ body: schemas.templateCreate as ValidateFunction }), templateController.create_template); |
@vacekj I'm having the same problem. @rmolinamir is correct in that the error is triggered by The strange thing is that passing an object directly to the import { Validator } from 'express-json-validator-middleware'
const validator = new Validator({ allErrors: true })
const mySchema = { type: 'object', properties: {} }
// Passing the schema inline works
validator.validate({ body: { type: 'object', properties: {} } })
// Passing the variable throws an error
validator.validate({ body: mySchema })
// Even expanding the variable throws an error
validator.validate({ body: { ...mySchema } }) I'm completely stumped. Any thoughts? UpdateThe following functions as a workaround. import { JSONSchema7 } from 'json-schema'
validator.validate({ body: mySchema as JSONSchema7 }) |
I experienced the same issue, what worked for me was to put {
type: 'object' as const
...
} unfortunately this meant not being able to use .json files anymore, had to migrate them to TS I think the advantage of this approach is that TS will complain when a invalid value for type is entered, which it won't do when explicit type casting to JSONSchema7. |
Hi folks 👋 I'm the new maintainer of this project. I've just started looking into this issue and will attempt to reproduce it. I'll let you know how I get on, but any further insights as to what the issue might be are very welcome in the meantime! |
It looks like this issue was actually introduced by adding types, period - |
@NGTOne That makes sense. I think this issue needs to be resolved as removing the types completely isn't a viable option. I'm hoping to have time to do some digging on this later this week. |
I've been trying to resolve this issue, but it's proving tricky. I've created a minimal reproducible example demonstrating the problem: https://bit.ly/3oIwQ9z (thanks to @monooso for sharing their workaround above). |
@simonplend You need to use It works good here: https://tsplay.dev/WJ96lm @monooso can you provide a minimal working example in CodeSandbox with json? However, as I understand, even with |
@Beraliv That's great, thank you! It seems similar to the solution which @senorpedro mentioned above. This raises a couple of questions for me:
I really appreciate everyone's help with this issue. I only have a little experience with TypeScript, and I want to make sure I resolve this issue correctly, even if it just means adding a section to the README with some guidance on using this library with TypeScript. |
I checked the
Check out the example here: https://tsplay.dev/wee2Ew
You need type ValidateParameters = { type: 'object', parameters: Record<string, unknown> } | { type: 'string'; parameters: string; }; If you use
It you call
You can check examples here: https://tsplay.dev/wj53YW |
@Beraliv Thank you so much for this detailed detective work, the thorough explanation, and for all of the examples too. They are really helpful. I think I understand everything you've explained, but I'm going to read it all again with fresh eyes tomorrow. I'm planning to add a short section to the project README with tips on usage with TypeScript. If you are happy to, I would really appreciate you looking over these documentation changes before I merge them. Would you mind me tagging you in the PR? |
Not at all, I'm fine with it 👍 |
@simonplend do you still need help? |
When defining JSON schemas in TypeScript, you will receive a compilation error unless you cast the schema object with `as const`. Related issue: #39
I was looking at the documentation for the json-schema-to-ts package recently. I noticed their documentation also mentions the need to use |
I'm planning to merge #89 tomorrow so that I can close this issue and get v2.2.0 released. Thanks for your help everyone! |
When defining JSON schemas in TypeScript, you will receive a compilation error unless you cast the schema object with `as const`. Related issue: #39
Adding My error:
Solution:const schema: JSONSchema7 = {
type: 'object',
properties: {
mustHaveField: { type: 'boolean' },
},
required: ['mustHaveField'],
}; Also updated to the latest json-schema type definition to avoid type mismatch error. At time of writing that's HTH |
2.0.0 works great but once updated to 2.1.0 I got strange errors from ts compiler.
The text was updated successfully, but these errors were encountered: