简体   繁体   中英

Pydantic model for JSON Meta Schema

I have a use case where user needs to define some JSON Schema for later usage. Right know I am using Pydantic parse user configs and check if they are ok.

Does any one know if there exist an library with Pydantic Model for JSON Meta Schema? Even better would be if it could parse JSON Schema into Pydantic Model at runtime.

If I understand correctly, you are looking for a way to generate Pydantic models from JSON schemas. Here is an implementation of a code generator - meaning you feed it a JSON schema and it outputs a Python file with the Model definition(s). It is not "at runtime" though. For this, an approach that utilizes the create_model function was also discussed in this issue thread a while back, but as far as I know there is no such feature in Pydantic yet.

If you know that your models will not be too complex, it might be fairly easy to implement a crude version of this yourself. Essentially the properties in a JSON schema are reflected fairly nicely by the __fields__ attribute of a model. You could write a function that takes a parsed JSON schema (ie a dictionary) and generates the Field definitions to pass to create_model .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM