Created
April 25, 2016 12:34
-
-
Save rafaelcaricio/6e67286a522f747405a7299e6843cd93 to your computer and use it in GitHub Desktop.
Custom type format - Connexion
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
import logging | |
import re | |
from connexion import App | |
from jsonschema import draft4_format_checker | |
MONEY_RE = re.compile('^\$\s*\d+(\.\d\d)?') | |
logging.basicConfig(level=logging.DEBUG) | |
@draft4_format_checker.checks('money') | |
def is_money(val): | |
if not isinstance(val, str): | |
return True | |
return MONEY_RE.match(val) | |
def convert_to_money(value=0): | |
return "$ {}.00".format(str(value)) | |
if __name__ == '__main__': | |
app = App(__name__) | |
api = app.add_api('api.yaml', validate_responses=True) | |
app.run(port=8080) |
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
swagger: "2.0" | |
info: | |
title: "Product Price Format API" | |
version: "1.0" | |
paths: | |
/format-money: | |
get: | |
summary: Get some money | |
operationId: api.convert_to_money | |
parameters: | |
- name: value | |
in: query | |
type: integer | |
responses: | |
200: | |
description: OK | |
schema: | |
type: string | |
format: money |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if we dont define
format: money
in schema, it wont work ?https://gist.github.com/rafaelcaricio/6e67286a522f747405a7299e6843cd93#file-api-yaml-L21