Skip to content

Instantly share code, notes, and snippets.

@rafaelcaricio

rafaelcaricio/api.py

Created Apr 25, 2016
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
@jewei1997

This comment has been minimized.

Copy link

@jewei1997 jewei1997 commented Jun 25, 2020

Why do you return True if the val is NOT a string?

@rafaelcaricio

This comment has been minimized.

Copy link
Owner Author

@rafaelcaricio rafaelcaricio commented Jun 27, 2020

Good question. I don't remember, that was many years ago now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.