Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom type format - Connexion
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)
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