Skip to content

Commit

Permalink
Merge pull request #92 from d-ganchar/4.3.1
Browse files Browse the repository at this point in the history
4.3.1
  • Loading branch information
d-ganchar authored Jan 10, 2024
2 parents 3934835 + 4ef4e29 commit 01685fc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@

[![Slava Ukraini](https://img.shields.io/badge/Slava-Ukraini-FFD500?style=flat&labelColor=005BBB)](https://bank.gov.ua/en/news/all/natsionalniy-bank-vidkriv-spetsrahunok-dlya-zboru-koshtiv-na-potrebi-armiyi) [![Support Ukraine](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB)](https://opensource.fb.com/support-ukraine)

[![Python versions](https://img.shields.io/pypi/pyversions/flask_request_validator.svg?logo=python&logoColor=81B441)](https://docs.python.org/3/)
[![Python versions](https://img.shields.io/pypi/pyversions/flask_request_validator.svg?logo=python&logoColor=81B441)](https://github.com/d-ganchar/flask_request_validator/blob/master/.travis.yml#L3)
[![Downloads](https://static.pepy.tech/badge/flask_request_validator)](https://pypi.org/project/flask-request-validator/)
[![Code size](https://img.shields.io/github/languages/code-size/d-ganchar/flask_request_validator.svg?logo=Dropbox&logoColor=ACD2F6)](https://github.com/d-ganchar/flask_request_validator/tree/master/flask_request_validator)
[![PyPI version](https://img.shields.io/pypi/v/flask_request_validator.svg?logo=pypi&logoColor=FFE200)](https://pypi.org/project/flask-request-validator/)
[![PyPI version](https://img.shields.io/pypi/v/flask_request_validator.svg?logo=pypi&logoColor=FFE200)](https://pypi.org/project/flask-request-validator/#history)

[![PyPI downloads](https://img.shields.io/pypi/dm/flask_request_validator.svg?logo=docusign&logoColor=FFE200)](https://pypi.org/project/flask-request-validator/)
[![Coverage Status](https://img.shields.io/coveralls/d-ganchar/flask_request_validator/badge.svg?branch=master&logo=google-analytics)](https://coveralls.io/github/d-ganchar/flask_request_validator?branch=master)
[![Build Status](https://api.travis-ci.com/d-ganchar/flask_request_validator.svg?branch=master)](https://www.travis-ci.com/github/d-ganchar/flask_request_validator)

Package provide possibility to validate of Flask request.
[![Upload Python Package](https://github.com/d-ganchar/flask_request_validator/actions/workflows/python-publish.yml/badge.svg)](https://github.com/d-ganchar/flask_request_validator/actions/workflows/python-publish.yml)

Key features
------------
- Easy and beautiful
- Type conversion
- Extensible
- Header, GET, FORM and nested JSON validation
- GET, FORM, nested JSON, headers and files validation
- Post validation hooks
- Custom error messages
- Flask v2 / v1 support
- Supports [Flask-RESTful](https://flask-restful.readthedocs.io/en/latest/)

#### How to install:
Expand Down
11 changes: 0 additions & 11 deletions README.rst

This file was deleted.

3 changes: 2 additions & 1 deletion flask_request_validator/validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import types
from copy import deepcopy
from functools import wraps
from typing import Tuple

Expand Down Expand Up @@ -193,7 +194,7 @@ def __get_request_errors(
errors = {GET: dict(), FORM: dict(), JSON: dict(), HEADER: dict(), PATH: dict(), FILES: []}
for param in params:
if isinstance(param, JsonParam):
value, json_errors = param.validate(request.get_json())
value, json_errors = param.validate(deepcopy(request.get_json()))
if json_errors:
errors[JSON] = json_errors
else:
Expand Down
14 changes: 9 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import io
from setuptools import setup

with io.open('README.rst', 'rt', encoding='utf8') as f:
long_description = f.read()
DESCRIPTION = """Flask Request Validator
=======================
See `readme`_
.. _readme: https://github.com/d-ganchar/flask_request_validator#flask-request-validator
"""

setup(
name='flask_request_validator',
version='4.3.0',
version='4.3.1',
description='Flask request data validation',
long_description=long_description,
long_description=DESCRIPTION,
url='https://github.com/d-ganchar/flask_request_validator',
author='Danila Ganchar',
author_email='[email protected]',
Expand Down
52 changes: 41 additions & 11 deletions tests/test_nested_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from copy import deepcopy

from parameterized import parameterized
import flask

from flask_request_validator import (
JsonParam as P,
Expand All @@ -15,6 +16,8 @@
IntRule,
FloatRule,
BoolRule,
validate_params,
ValidRequest,
)
from flask_request_validator.exceptions import *

Expand Down Expand Up @@ -218,26 +221,26 @@ def test_root_list_invalid(self):
@parameterized.expand([
# IntRule
(
P(dict(age=[Min(27), IntRule()], day=[Min(1), IntRule()])),
dict(age=27, day='1'),
dict(age=27, day=1),
P(dict(age=[Min(27), IntRule()], day=[Min(1), IntRule()])),
dict(age=27, day='1'),
dict(age=27, day=1),
),
(
P(dict(age=[Min(27), IntRule(False)])),
dict(age='27'),
P(dict(age=[Min(27), IntRule(False)])),
dict(age='27'),
"[JsonError(['root'], {'age': RulesError(TypeConversionError())}, False)]",
),
# FloatRule
(
P(dict(price=[Min(0.69), FloatRule()], size=[Min(0.25), FloatRule({','})])),
dict(price=0.69, size='0,25'),
dict(price=0.69, size=0.25),
P(dict(price=[Min(0.69), FloatRule()], size=[Min(0.25), FloatRule({','})])),
dict(price=0.69, size='0,25'),
dict(price=0.69, size=0.25),
),
# BoolRule
(
P(dict(yes=[BoolRule()], no=[BoolRule()])),
dict(yes=True, no=False),
dict(yes=True, no=False),
P(dict(yes=[BoolRule()], no=[BoolRule()])),
dict(yes=True, no=False),
dict(yes=True, no=False),
),
])
def test_type_checkers(self, param: P, value: dict, expected: dict or str):
Expand All @@ -247,3 +250,30 @@ def test_type_checkers(self, param: P, value: dict, expected: dict or str):
return

self.assertEqual(new_val, expected)


_app = flask.Flask(__name__)

_app.testing = True


@_app.route('/issue-90', methods=['POST'])
@validate_params(P(dict(amount=[IntRule()], price=[FloatRule({','})])))
def issue_90(valid: ValidRequest):
return flask.jsonify(dict(
json_after_validation=valid.get_json(),
raw_json=valid.get_flask_request().json,
))


class TestNestedJson(unittest.TestCase):
def test_nested_json(self):
data = dict(amount='99', price='101,101')
with _app.test_client() as client:
result = client.post('/issue-90', json=data).json

self.assertDictEqual(result['raw_json'], data)
self.assertDictEqual(
result['json_after_validation'],
dict(amount=99, price=101.101),
)

0 comments on commit 01685fc

Please sign in to comment.