Skip to content

Instantly share code, notes, and snippets.

@rafaelcaricio
Created April 25, 2016 12:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rafaelcaricio/6e67286a522f747405a7299e6843cd93 to your computer and use it in GitHub Desktop.
Save rafaelcaricio/6e67286a522f747405a7299e6843cd93 to your computer and use it in GitHub Desktop.
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
Copy link

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

@rafaelcaricio
Copy link
Author

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

@theodoresi
Copy link

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

I think you should return False here

@copdips
Copy link

copdips commented Dec 15, 2020

if we dont define format: money in schema, it wont work ?
https://gist.github.com/rafaelcaricio/6e67286a522f747405a7299e6843cd93#file-api-yaml-L21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment