-
Notifications
You must be signed in to change notification settings - Fork 408
required ignored for properties referencing definitions with allOf #395
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
Comments
Could fix with something like rules.set('Set required on allOf', (schema) => {
if (schema.allOf && schema.required) {
schema.allOf.forEach(subSchema => {
Object.assign(subSchema, { required: schema.required })
});
}
}) in src/normaliser.ts |
@ackl Were you able to find a work around for this? Running into the same issue. |
This should have the behavior you're looking for: {
"title": "Example Schema",
"type": "object",
"properties": {
"reference": {
"allOf": [
{ "$ref": "#/definitions/defined" },
{
"type": "object",
"properties": {
"subproperty": {
"type": "integer"
}
},
"required": [
"subproperty"
]
}
],
}
},
"definitions": {
"defined": {
"type": "object",
"properties": {
"subproperty": {
"type": "integer"
}
}
}
},
"additionalProperties": false
} Or if you don't want to use {
"title": "Example Schema",
"type": "object",
"properties": {
"reference": {
"$ref": "#/definitions/Defined",
"required": [
"subproperty"
]
}
},
"definitions": {
"defined": {
"type": "object",
"properties": {
"subproperty": {
"type": "integer"
}
}
}
},
"additionalProperties": false
} |
Actually, according to json-schema-org/json-schema-spec#672,
is equivalent to:
And most of the schema keywords can independently validate values, including
is perfectly valid. So I think, @ackl is correct that this is a bug. |
(Source) So when you put |
@goodoldneon The thing is See spec: https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.5.3 It mentions NOTHING about the strings inside Therefore,
This is a valid schema by itself, it just validates that an object must have a property named interface A {
something: unknown
} except that typescript allows |
Oh interesting, so each of the "validation keywords" has an implicit {
"required": ["foo"]
} Is the same as this: {
"type": "object",
"required": ["foo"]
} In an |
@goodoldneon nope. |
Thank you for being patient with me, @chbdetta. We're getting into some advanced JSON Schema stuff I haven't dealt with. Given the schema that @ackl originally posted, I think that the following types would work: export interface ExampleSchema {
reference?: Defined & {
subproperty: any;
};
}
export interface Defined {
subproperty?: number;
[k: string]: unknown;
} I was able to get that working by modifying this switch case: View git diffdiff --git a/src/parser.ts b/src/parser.ts
index 6cb6cef..0abb5b3 100644
--- a/src/parser.ts
+++ b/src/parser.ts
@@ -124,11 +124,44 @@ function parseNonLiteral(
switch (type) {
case 'ALL_OF':
+ const astParams = schema.allOf!.map(_ => parse(_, options, undefined, processed, usedNames))
+
+ // Required properties have been specified alongside `allOf`.
+ if (Array.isArray(schema.required)) {
+ // This block will add an anonymous interface whose keys are the
+ // required properties. Every type is `any`, since this interface doesn't
+ // care what the type is, but rather is only concerned that the
+ // properties exist.
+
+ const requiredParams = schema.required.map(
+ (propertyName: string): TInterfaceParam => {
+ return {
+ ast: {
+ keyName: propertyName,
+ type: 'ANY'
+ },
+ isPatternProperty: false,
+ isRequired: true,
+ isUnreachableDefinition: false,
+ keyName: propertyName
+ }
+ }
+ )
+
+ astParams.push({
+ comment: undefined,
+ keyName: undefined,
+ params: requiredParams,
+ superTypes: [],
+ type: 'INTERFACE'
+ })
+ }
+
return {
comment: schema.description,
keyName,
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
- params: schema.allOf!.map(_ => parse(_, options, undefined, processed, usedNames)),
+ params: astParams,
type: 'INTERSECTION'
}
case 'ANY': If @bcherny is OK with my approach, I can work on a PR. |
Sorry for the delay! I don't think a normalizer rule would be sufficient here. We also need to consider:
My hunch is the right solution is emitting some sort of intersection type, similar to what @goodoldneon suggested. I'd love to see this fleshed out in a PR, preferably without the |
Whats the status of this bug in 2024? |
Hi,
I've noticed what seems like a bug with the generated types when the
required
keyword is used in a property which references a definition usingallOf
Here's an example schema:
and the generated typings
I understand that the
interface Defined
is probably just pulled straight from the definitions section which is why thesubproperty
key is optional, but I would expect that the generated types would know that theDefined
interface being used byExampleSchema
hassubproperty
as mandatory.If you explore this in a json schema validator you will see that given the example schema above, this object:
is invalid since
Required properties are missing from object: subproperty.
but with the current json-schema-to-typescript implementation,
is valid
The text was updated successfully, but these errors were encountered: