ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

feat: support dependencies json schema field

Open bytemain opened this issue 2 years ago • 9 comments

in JSON Schema Specification 7, it support dependencies in object type that can be used for data validate.

reference: https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#page-14

CleanShot 2023-05-25 at 11 07 35@2x

draft 7: https://json-schema.org/specification-links.html#draft-7


more info: In JSON Schema 2019-09, dependencies has been split into dependentSchemas and dependentRequired

image

but currently we used the types from JSON Schema 7, so we can only support dependencies now.

bytemain avatar May 25 '23 03:05 bytemain

Thanks. I marked this as a draft.

domoritz avatar May 25 '23 08:05 domoritz

@domoritz Hello, This pull request is ready for review.

I'm sorry for updating this PR so late, I was a little busy recently.

bytemain avatar Jun 21 '23 03:06 bytemain

All good. Do you think it makes sense to update to a newer version of json schema? Also, is there some way we could use this functionality for some specific ts patterns rather than just for Jsdoc comments? Are the jadoc comments used anywhere else ow is this just a json schema thing?

domoritz avatar Jun 21 '23 14:06 domoritz

  1. Do you think it makes sense to update to a newer version of json schema?

I don't think there's a need to do that.

  1. is there some way we could use this functionality for some specific ts patterns rather than just for Jsdoc comments.

Currently, it seem that TypeScript do not has a conception about dependency? In my use case, I'm using JSON Schemas to validate user input data, so this code already meets my needs.

bytemain avatar Jun 22 '23 11:06 bytemain

Thanks for the pull request. I think I would like to hear from someone else who wants this feature before adding it since there is no equivalent support in TS. Does that make sense?

domoritz avatar Jun 22 '23 22:06 domoritz

Thanks for the pull request. I think I would like to hear from someone else who wants this feature before adding it since there is no equivalent support in TS. Does that make sense?

Ok, But in my opinion, the dependentRequired is the same kind of title/format in BasicAnnotationsReader class, it also read the jsdoc and save it to JSON Schemas:

https://github.com/vega/ts-json-schema-generator/blob/6b0e62e9e4794a28f60fa74ee332d03b026ca105/src/AnnotationsReader/BasicAnnotationsReader.ts#L10

it is a descriptor about this field.

So I think jsdoc is acceptable.

bytemain avatar Jun 23 '23 07:06 bytemain

@domoritz I have moved the dependencies related code to ExtendedAnnotationsReader.

I add a new function called getTypeAnnotations for AnnotationsReader interface:

export interface AnnotationsReader {
    getAnnotations(node: ts.Node): Annotations | undefined;
+    getTypeAnnotations?(type: BaseType): Annotations | undefined;
}

and then in the ExtendedAnnotationsReader, it can read things its need from the ObjectType, then create the dependencies we need.

bytemain avatar Sep 11 '23 05:09 bytemain

Can you resolve the conflicts @bytemain. Thanks for your patience with my review!

domoritz avatar Oct 16 '23 01:10 domoritz

Can you resolve the conflicts @bytemain. Thanks for your patience with my review!

Done.

bytemain avatar Oct 17 '23 02:10 bytemain