Last active
March 2, 2018 00:59
-
-
Save adamcharnock/b1a5d63e718a4359f2de77fadedf8895 to your computer and use it in GitHub Desktop.
Extracting a json scheme from Python 3 typing hinting (examples)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://github.com/adamcharnock/lightbus/blob/master/experiments/types_to_jsonschema.py | |
// $ python experiments/types_to_jsonschema.py | |
// Example: | |
// def check_password(username: str, password: str) -> bool: pass | |
// Parameter schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "check_password() parameters", | |
"type": "object", | |
"additionalProperties": false, | |
"properties": { | |
"username": { | |
"type": "string" | |
}, | |
"password": { | |
"type": "string" | |
} | |
}, | |
"required": [ | |
"username", | |
"password" | |
] | |
} | |
// Response schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "check_password() parameters", | |
"type": "boolean" | |
} | |
// Example: | |
// def get_user(username: str) -> User: pass | |
// Parameter schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "get_user() parameters", | |
"type": "object", | |
"additionalProperties": false, | |
"properties": { | |
"username": { | |
"type": "string" | |
} | |
}, | |
"required": [ | |
"username" | |
] | |
} | |
// Response schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "get_user() parameters", | |
"type": "object", | |
"title": "User", | |
"properties": { | |
"username": { | |
"type": "string" | |
}, | |
"password": { | |
"type": "string" | |
}, | |
"is_admin": { | |
"type": "boolean", | |
"default": false | |
} | |
}, | |
"required": [ | |
"username", | |
"password" | |
], | |
"additionalProperties": false | |
} | |
// Example: | |
// def get_user(username: str) -> User: pass | |
// Parameter schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "get_user() parameters", | |
"type": "object", | |
"additionalProperties": false, | |
"properties": { | |
"username": { | |
"type": "string" | |
} | |
}, | |
"required": [ | |
"username" | |
] | |
} | |
// Response schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "get_user() parameters", | |
"type": "object", | |
"title": "User", | |
"properties": { | |
"username": { | |
"type": "string" | |
}, | |
"password": { | |
"type": "string" | |
}, | |
"is_admin": { | |
"type": "boolean", | |
"default": false | |
} | |
}, | |
"required": [ | |
"username", | |
"password" | |
], | |
"additionalProperties": false | |
} | |
// Example: | |
// def tuple_return_type(username: str) -> Tuple[str, int, bool]: pass | |
// Parameter schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "tuple_return_type() parameters", | |
"type": "object", | |
"additionalProperties": false, | |
"properties": { | |
"username": { | |
"type": "string" | |
} | |
}, | |
"required": [ | |
"username" | |
] | |
} | |
// Response schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "tuple_return_type() parameters", | |
"type": "array", | |
"maxItems": 3, | |
"minItems": 3, | |
"items": [ | |
{ | |
"type": "string" | |
}, | |
{ | |
"type": "number" | |
}, | |
{ | |
"type": "boolean" | |
} | |
] | |
} | |
// Example: | |
// def union_parameter(weird_value: Union[str, int]) -> list: pass | |
// Parameter schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "union_parameter() parameters", | |
"type": "object", | |
"additionalProperties": false, | |
"properties": { | |
"weird_value": { | |
"oneOf": [ | |
{ | |
"type": "string" | |
}, | |
{ | |
"type": "number" | |
} | |
] | |
} | |
}, | |
"required": [ | |
"weird_value" | |
] | |
} | |
// Response schema: | |
{ | |
"$schema": "http://json-schema.org/draft-04/schema#", | |
"description": "union_parameter() parameters", | |
"type": "array" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment