Skip to content

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
@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.

@theodoresi

This comment has been minimized.

Copy link

@theodoresi theodoresi commented Oct 27, 2020

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

I think you should return False here

@copdips

This comment has been minimized.

Copy link

@copdips 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