-
-
Notifications
You must be signed in to change notification settings - Fork 877
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
Representing union type in schema #2081
Comments
I believe this can be solved with anyOf, instead of type inside the "typez", please confirm and this can be closed |
the correct syntax for
anyOf can be cleaner indeed. |
The following doesn't seem to compile either. Is it supposed to? const schema: JSONSchemaType<Test> = {
type: 'object',
properties: {
typez: {
type: {
anyOf: [ // or oneOf
{ type: 'string' },
{ type: 'array', items: { type: 'string' } },
],
},
},
},
}; I could be completely off, but it looks to me like https://github.com/ajv-validator/ajv/blob/master/lib/types/json-schema.ts#L63 is incorrectly handling the case where |
For that matter, neither does const schema: JSONSchemaType<Test> = {
type: 'object',
properties: {
typez: {
type: {type: ["string", "array"], items: {type:"string"}}
},
},
}; |
We are facing the same issue, why is that closed? |
The issue is closed because the original question was answered. The question in the comment is different - JSONSchemaType utility probably does not support a union array and scalar type – it is a limitation. Both JSON Schema and Ajv support such unions, it is only related to JSONSchemaType - you can use Ajv without it though. Arguably, an untagged union of array and scalar should not be used in JSON APIs, as it can't be supported by many programming languages, for that reason it also won't be supported by JTD (JSON Type Definition - an alternative JSON schema language specification supported by Ajv, that maps well to type systems of a large number of languages). Instead it is better to use a discriminated union with a tag. |
Thanks for the update |
Sadly I'm facing a situation where it would be reasonable to use a union. That being a union of string or null, something basically every language can handle. In my case it makes it almost impossible to use ajv in typescript without using Additionally I find it odd that nullable doesn't cover null within a union, which would've solved my problem otherwise. |
What version of Ajv are you using? Does the issue happen if you use the latest version?
8.11
Your typescript code
Typescript compiler error messages
Describe the change that should be made to address the issue?
How shall I be able to represent the current type with ajv?
I have not understood how this PR helped: #1302
Or this: #134
Are you going to resolve the issue?
I will resolve when I get some feedback
The text was updated successfully, but these errors were encountered: