Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Created February 23, 2021 16:36
Show Gist options
  • Save thanakijwanavit/96a89fa7aac43cd765aaa4d6c786f94c to your computer and use it in GitHub Desktop.
Save thanakijwanavit/96a89fa7aac43cd765aaa4d6c786f94c to your computer and use it in GitHub Desktop.
convert json-dataclass to json schema yaml for use in validation
from dataclasses_json import dataclass_json, Undefined, CatchAll
from dataclasses_jsonschema import JsonSchemaMixin
import yaml
@dataclass_json(undefined=Undefined.INCLUDE)
@dataclass
class Product(JsonSchemaMixin):
iprcode: int
cprcode: int
@dataclass_json
@dataclass
class ValueUpdate(JsonSchemaMixin):
items: List[Product]
print(yaml.dump(Product.json_schema(), indent=2, sort_keys=True))
print(yaml.dump(ValueUpdate.json_schema(), indent=2, sort_keys=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment