Skip to content
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

generate schema from models? #55

Closed
hampsterx opened this issue Oct 1, 2021 · 11 comments
Closed

generate schema from models? #55

hampsterx opened this issue Oct 1, 2021 · 11 comments

Comments

@hampsterx
Copy link

is there any way to generate a data dict of the model structure (field + type)? This would allow to generate typescript definitions for example ala poor mans openAPI generator type tool?

@Fatal1ty
Copy link
Owner

Fatal1ty commented Oct 4, 2021

Hi. There is get_type_hints function you can use for this.

@hampsterx
Copy link
Author

ahh thats true, yeah I had a quick look into your code afterwards and realised it should be quite straightforward. Cheers~

@adriangb
Copy link

adriangb commented Oct 7, 2021

Are there any plans for a non "poor mans" version? Or is there a compatible library that can generate jsonschema from a data class?
In the same vein, is there any plan to express types like {"type": "string", "pattern": "someregex"} which is a part of jsonschema?
Thanks!

@Fatal1ty
Copy link
Owner

Fatal1ty commented Oct 7, 2021

Are there any plans for a non "poor mans" version? Or is there a compatible library that can generate jsonschema from a data class?

Unfortunately, I don't know of any compatible library but you could try to reuse field metadata to generate a simple json schema.

In the same vein, is there any plan to express types like {"type": "string", "pattern": "someregex"} which is a part of jsonschema?

To do this we need extensible validation mechanism which is not ready yet.

@hampsterx
Copy link
Author

sorry only tangently related but..

To do this we need extensible validation mechanism which is not ready yet.

Any plans to support late initialization?

eg

foo = Bar()
foo.bar = 123
...
# validate..

This along with schema would be perfect. In our situation we have quite large response models that are built up and would be nicer than passing the full dict to the constructor.

@adriangb
Copy link

adriangb commented Oct 7, 2021

I do agree that separating construction from validation would be best (vs. pydantic where the constructor validates by default and you have to use construct to build a model without validation).

@Fatal1ty
Copy link
Owner

Fatal1ty commented Oct 7, 2021

In our situation we have quite large response models that are built up and would be nicer than passing the full dict to the constructor.

@hampsterx How do you build that models? If you want to deserialize dataclasses with another library and validate with mashumaro I wouldn’t say it’s a good idea.

I do agree that separating construction from validation would be best (vs. pydantic where the constructor validates by default and you have to use construct to build a model without validation).

@adriangb If I read you right, you want to be able to choose either to validate or not during the deserialization process. Or do you want to trigger validation manually at any time after an instance is created?

@hampsterx
Copy link
Author

Currently we just build up a big response dict but want to use models so we can lock it down and ideally generate client libraries (eg typescript, dart, etc) from the (json) schema. would be much nicer to start with Foo() and then build it up as we go rather than Foo(ret) at the end.

@adriangb
Copy link

adriangb commented Oct 8, 2021

I think manually triggering validation is more valuable:

  1. It encompasses a the former approach since running validation immediately after construction is equivalent to validating during construction.
  2. It allows more flexibility, e.g. the builder pattern shown above.

@Fatal1ty
Copy link
Owner

Are there any plans for a non "poor mans" version? Or is there a compatible library that can generate jsonschema from a data class?
In the same vein, is there any plan to express types like {"type": "string", "pattern": "someregex"} which is a part of jsonschema?
Thanks!

Since mashumaro has grown and become quite stable I’ve revised my opinion about jsonschema generation. This is what I’m currently working on as part of the following issue:

I don’t have a lot of experience in using jsonschema in the wild, so any help and suggestions are very welcome!

@Fatal1ty
Copy link
Owner

Fatal1ty commented Apr 6, 2023

Well, the pull request is merged, documentation is here. Everyone is welcome to give it a try 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants