how to support complex anyOf / oneOf conditions
👋 hey there, thank you very much for this package. We have really enjoyed using it so far. I had a question / possible feature request. We are using a type / spec pattern in our yaml where the type acts as a discriminator field and the spec field stores the type specific data.
Here is an example yaml (note that this is heavily simplified)
steps:
- type: foo
spec:
foo: true
- type: bar
spec:
bar: true
Here is an example object:
interface Step {
type: "foo" | "bar";
spec: Foo | Bar
}
interface Foo {
foo: string;
}
interface Bar {
bar: string;
}
Is it possible to create If / Else conditions to enforce the spec based on the type, such as this:
"oneOf": [
{
"allOf": [
{
"properties": {
"type": {
"enum": [ "foo" ]
}
}
},
{
"properties": {
"spec": {
"$ref": "#/definitions/Foo"
}
}
}
]
},
{
"allOf": [
{
"properties": {
"type": {
"enum": [ "bar" ]
}
}
},
{
"properties": {
"spec": {
"$ref": "#/definitions/Bar"
}
}
}
]
}
]
I am guessing annotations are not robust enough for this use case, but was wondering if there could be some advanced configuration to enable this use case? If this requires an enhancement, we would be happy to contribute, however, I might kindly ask that you provide some direction as to how you would like this implemented. Thanks!
First, looks like your interface def is not reflecting what you want, it should be
type Step = fooStep | barStep
interface fooStep {
type: 'foo'
spec: { foo: Foo}
}
interface barStep {
type: 'bar'
spec: { bar: Bar}
}
Second, if above doesn't help, you can cosntruct your own complex allOf/oneOf through annotation
/**
* @oneOf <JSON section of oneOf>
interface step {
...
}
Second, if above doesn't help, you can cosntruct your own complex allOf/oneOf through annotation
/** * @oneOf <JSON section of oneOf> interface step { ... }
This oneOf annotation does not seem to work. It is commented out in the validationKeywords struct (see typescript-json-schema.ts). Can you elaborate please?