jacksmith15 / statham-schema

Statham is a Python Model Parsing Library for JSON Schema.
https://statham-schema.readthedocs.io/en/latest/
MIT License
38 stars 11 forks source link

Support serialization of DSL definitions to raw JSON Schema #64

Closed jacksmith15 closed 4 years ago

jacksmith15 commented 4 years ago

Problem Statement

Currently, parsing JSONSchema's to DSL objects is supported, but not the other direction. DSL Elements should expose methods which produce the equivalent JSON Schema dictionaries.

Example

class Choice(Object):
    choice_text: str = Property(String(maxLength=200), required=True)
    votes: int = Property(Integer(default=0))

class Poll(Object):
    question: str = Property(String(maxLength=200), required=True)
    choices: List[Choice] = Property(Array(Choice), required=True)

Poll.serialize() == {
  "type": "object",
  "title": "Poll",
  "required": ["question", "choices"],
  "properties": {
    "question": {"type": "string", "maxLength": 200},
    "choices": {
      "type": "array",
      "items": {
        "type": "object",
        "title": "Choice",
        "required": ["choice_text"],
        "properties": {
          "choice_text": {"type": "string", "maxLength": 200},
          "votes": {"type": "integer", "default": 0},
        }
      }
    }
  }
}

Technical notes

Some problem areas will likely include: