示例#1
0
def validate_input():
    headers, payload = request.headers, request.json
    if constants.WINDOW_BEGIN_KEY not in headers or not headers[
            constants.WINDOW_BEGIN_KEY]:
        raise JsonSchemaException('Header must contains "JANELA_INICIO"')
    if constants.WINDOW_END_KEY not in headers or not headers[
            constants.WINDOW_END_KEY]:
        raise JsonSchemaException('Header must contains "JANELA_FIM"')
    if payload is None or not payload:
        raise JsonSchemaException('Payload must not be empty')
    validation.validate(payload)
示例#2
0
def test_integer_is_not_number(asserter, value):
    asserter({
        'type': 'integer',
    }, value,
             JsonSchemaException('data must be integer',
                                 value='{data}',
                                 name='data',
                                 definition='{definition}',
                                 rule='type'))
示例#3
0
def test_number(asserter, number_type, value, expected):
    if isinstance(expected, JsonSchemaException):
        expected = JsonSchemaException(
            expected.message.format(number_type=number_type),
            value='{data}',
            name='data',
            definition='{definition}',
            rule='type')
    asserter({'type': number_type}, value, expected)
示例#4
0
def validate_client_authorization(client_authorization: dict):
    t = client_authorization["type"]
    validators_map = {
        "api_key": validate_client_authorization_api_key,
        "http_signature": validate_client_authorization_http_signature
    }
    if t in validators_map:
        validators_map[t](client_authorization)
    else:
        raise JsonSchemaException("type not valid")
示例#5
0
def test_exception_variable_path(value, expected):
    exc = JsonSchemaException('msg', name=value)
    assert exc.path == expected
示例#6
0
def validate(data):
    if not isinstance(data, (str)):
        raise JsonSchemaException("data must be string", value=data, name="data", definition={'type': 'string'}, rule='type')
    return data
import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be one of [1, 2, \'a\', "b\'c"]',
                          value='{data}',
                          name='data',
                          definition='{definition}',
                          rule='enum')


@pytest.mark.parametrize('value, expected', [
    (1, 1),
    (2, 2),
    (12, exc),
    ('a', 'a'),
    ('aa', exc),
])
def test_enum(asserter, value, expected):
    asserter({'enum': [1, 2, 'a', "b'c"]}, value, expected)


exc = JsonSchemaException('data must be string or number',
                          value='{data}',
                          name='data',
                          definition='{definition}',
                          rule='type')


@pytest.mark.parametrize('value, expected', [
    (0, 0),
import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be object')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    (False, exc),
    ('abc', exc),
    ([], exc),
    ({}, {}),
    ({
        'x': 1,
        'y': True
    }, {
        'x': 1,
        'y': True
    }),
])
def test_object(asserter, value, expected):
    asserter({'type': 'object'}, value, expected)


@pytest.mark.parametrize('value, expected', [
    ({}, {}),
    ({
        'a': 1
示例#9
0
import pytest

import fastjsonschema
from fastjsonschema import JsonSchemaDefinitionException, JsonSchemaException

exc = JsonSchemaException('data must be object',
                          value='{data}',
                          name='data',
                          definition='{definition}',
                          rule='type')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    (False, exc),
    ('abc', exc),
    ([], exc),
    ({}, {}),
    ({
        'x': 1,
        'y': True
    }, {
        'x': 1,
        'y': True
    }),
])
def test_object(asserter, value, expected):
    asserter({'type': 'object'}, value, expected)
示例#10
0
import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be one of [1, 2, \'a\']')


@pytest.mark.parametrize('value, expected', [
    (1, 1),
    (2, 2),
    (12, exc),
    ('a', 'a'),
    ('aa', exc),
])
def test_enum(asserter, value, expected):
    asserter({'enum': [1, 2, 'a']}, value, expected)


exc = JsonSchemaException('data must be string or number')


@pytest.mark.parametrize('value, expected', [
    (0, 0),
    (None, exc),
    (True, exc),
    ('abc', 'abc'),
    ([], exc),
    ({}, exc),
])
def test_types(asserter, value, expected):
    asserter({'type': ['string', 'number']}, value, expected)
示例#11
0
 ),
 (
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 42, 3],
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 42, 3],
 ),
 (
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
 ),
 (
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5, 'any'],
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5, 'any'],
 ),
 (
     [10, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     JsonSchemaException('data[0] must be smaller than 10'),
 ),
 (
     [9, 'xxx', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     JsonSchemaException('data[1] must be one of [\'hello\', \'world\']'),
 ),
 (
     [9, 'hello', [], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     JsonSchemaException('data[2] must contain at least 1 items'),
 ),
 (
     [9, 'hello', [1, 2, 3], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     JsonSchemaException('data[2][1] must be string'),
 ),
 (
     [9, 'hello', [1], {'a': 'a', 'x': 'x', 'y': 'y'}, 'str', 5],
示例#12
0
import datetime
import re

import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be date-time',
                          value='{data}',
                          name='data',
                          definition='{definition}',
                          rule='format')


@pytest.mark.parametrize('value, expected', [
    ('', exc),
    ('bla', exc),
    ('2018-02-05T14:17:10.00', exc),
    ('2018-02-05T14:17:10.00Z\n', exc),
    ('2018-02-05T14:17:10.00Z', '2018-02-05T14:17:10.00Z'),
    ('2018-02-05T14:17:10Z', '2018-02-05T14:17:10Z'),
])
def test_datetime(asserter, value, expected):
    asserter({'type': 'string', 'format': 'date-time'}, value, expected)


exc = JsonSchemaException('data must be hostname',
                          value='{data}',
                          name='data',
                          definition='{definition}',
                          rule='format')
示例#13
0
import pytest

from fastjsonschema import JsonSchemaException


exc = JsonSchemaException('data must be string')
@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    ('', ''),
    ('abc', 'abc'),
    ([], exc),
    ({}, exc),
])
def test_string(asserter, value, expected):
    asserter({'type': 'string'}, value, expected)


exc = JsonSchemaException('data must be shorter than or equal to 5 characters')
@pytest.mark.parametrize('value, expected', [
    ('', ''),
    ('qwer', 'qwer'),
    ('qwert', 'qwert'),
    ('qwertz', exc),
    ('qwertzuiop', exc),
])
def test_max_length(asserter, value, expected):
    asserter({
        'type': 'string',
import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be date-time')


@pytest.mark.parametrize('value, expected', [
    ('', exc),
    ('bla', exc),
    ('2018-02-05T14:17:10.00Z\n', exc),
    ('2018-02-05T14:17:10.00Z', '2018-02-05T14:17:10.00Z'),
    ('2018-02-05T14:17:10Z', '2018-02-05T14:17:10Z'),
])
def test_datetime(asserter, value, expected):
    asserter({'type': 'string', 'format': 'date-time'}, value, expected)


exc = JsonSchemaException('data must be hostname')


@pytest.mark.parametrize('value, expected', [
    ('', exc),
    ('LDhsjf878&d', exc),
    ('bla.bla-', exc),
    ('example.example.com-', exc),
    ('example.example.com\n', exc),
    ('localhost', 'localhost'),
    ('example.com', 'example.com'),
    ('example.de', 'example.de'),
    ('example.fr', 'example.fr'),
import pytest

from fastjsonschema import JsonSchemaException, compile_to_code
from fastjsonschema.generator import CodeGenerator
from fastjsonschema.ref_resolver import RefResolver
from fastjsonschema.formats import FormatManager
from fastjsonschema.config import Config


from .conftest import CONFIG


EXC = JsonSchemaException('data.a must be string')
@pytest.mark.parametrize('value, expected, filename', [
    ({'a': 'a', 'b': 1}, {'a': 'a', 'b': 1}, 'cc_test_1'),
    ({'a': 1, 'b': 1}, EXC, 'cc_test_2'),
])
def test_compile_to_code(asserter_cc, value, expected, filename):
    asserter_cc(
        {'properties': {
            'a': {'type': 'string'},
            'b': {'type': 'integer'},
        }}, value, expected, filename
    )


EXC = JsonSchemaException('data.a must match pattern [ab]')
@pytest.mark.parametrize('value, expected, filename', [
    ({'a': 'a', 'b': 1}, {'a': 'a', 'b': 1}, 'cc_test_3'),
    ({'a': 'c', 'b': 1}, EXC, 'cc_test_4'),
])
import pytest

from fastjsonschema import JsonSchemaException


exc = JsonSchemaException('data must be boolean', value='{data}', name='data', definition='{definition}', rule='type')
@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, True),
    (False, False),
    ('abc', exc),
    ([], exc),
    ({}, exc),
])
def test_boolean(asserter, value, expected):
    asserter({'type': 'boolean'}, value, expected)
import pytest

from fastjsonschema import JsonSchemaException


@pytest.mark.parametrize('value, expected', [
    (None, JsonSchemaException('data must be object')),
    ({}, {'a': '', 'b': 42, 'c': {}, 'd': []}),
    ({'a': 'abc'}, {'a': 'abc', 'b': 42, 'c': {}, 'd': []}),
    ({'b': 123}, {'a': '', 'b': 123, 'c': {}, 'd': []}),
    ({'a': 'abc', 'b': 123}, {'a': 'abc', 'b': 123, 'c': {}, 'd': []}),
])
def test_default_in_object(asserter, value, expected):
    asserter({
        'type': 'object',
        'properties': {
            'a': {'type': 'string', 'default': ''},
            'b': {'type': 'number', 'default': 42},
            'c': {'type': 'object', 'default': {}},
            'd': {'type': 'array', 'default': []},
        },
    }, value, expected)


@pytest.mark.parametrize('value, expected', [
    (None, JsonSchemaException('data must be array')),
    ([], ['', 42]),
    (['abc'], ['abc', 42]),
    (['abc', 123], ['abc', 123]),
])
示例#18
0
from builtins import ValueError

import datetime

import pytest

import re

from fastjsonschema import JsonSchemaException


exc = JsonSchemaException('data must be date-time')
@pytest.mark.parametrize('value, expected', [
    ('', exc),
    ('bla', exc),
    ('2018-02-05T14:17:10.00', exc),
    ('2018-02-05T14:17:10.00Z\n', exc),
    ('2018-02-05T14:17:10.00Z', '2018-02-05T14:17:10.00Z'),
    ('2018-02-05T14:17:10Z', '2018-02-05T14:17:10Z'),
])
def test_datetime(asserter, value, expected):
    asserter({'type': 'string', 'format': 'date-time'}, value, expected)


exc = JsonSchemaException('data must be hostname')
@pytest.mark.parametrize('value, expected', [
    ('', exc),
    ('LDhsjf878&d', exc),
    ('bla.bla-', exc),
    ('example.example.com-', exc),
    ('example.example.com\n', exc),
import pytest

from fastjsonschema import JsonSchemaException


@pytest.fixture(params=['number', 'integer'])
def number_type(request):
    return request.param


exc = JsonSchemaException('data must be {number_type}')
@pytest.mark.parametrize('value, expected', [
    (-5, -5),
    (0, 0),
    (5, 5),
    (None, exc),
    (True, exc),
    ('abc', exc),
    ([], exc),
    ({}, exc),
])
def test_number(asserter, number_type, value, expected):
    if isinstance(expected, JsonSchemaException):
        expected = JsonSchemaException(expected.message.format(number_type=number_type), value='{data}', name='data', definition='{definition}', rule='type')
    asserter({'type': number_type}, value, expected)


exc = JsonSchemaException('data must be smaller than or equal to 10', value='{data}', name='data', definition='{definition}', rule='maximum')
@pytest.mark.parametrize('value, expected', [
    (-5, -5),
    (5, 5),
示例#20
0
def test_exception_rule_definition(definition, rule, expected_rule_definition):
    exc = JsonSchemaException('msg', definition=definition, rule=rule)
    assert exc.rule_definition == expected_rule_definition
示例#21
0
import pytest

from fastjsonschema import JsonSchemaException


exc = JsonSchemaException('data must be string', value='{data}', name='data', definition='{definition}', rule='type')
@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    ('', ''),
    ('abc', 'abc'),
    ([], exc),
    ({}, exc),
])
def test_string(asserter, value, expected):
    asserter({'type': 'string'}, value, expected)


exc = JsonSchemaException('data must be shorter than or equal to 5 characters', value='{data}', name='data', definition='{definition}', rule='maxLength')
@pytest.mark.parametrize('value, expected', [
    ('', ''),
    ('qwer', 'qwer'),
    ('qwert', 'qwert'),
    ('qwertz', exc),
    ('qwertzuiop', exc),
])
def test_max_length(asserter, value, expected):
    asserter({
        'type': 'string',
        'maxLength': 5,
示例#22
0
import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be array',
                          value='{data}',
                          name='data',
                          definition='{definition}',
                          rule='type')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    (False, exc),
    ('abc', exc),
    ([], []),
    ([1, 'a', True], [1, 'a', True]),
    ({}, exc),
])
def test_array(asserter, value, expected):
    asserter({'type': 'array'}, value, expected)


exc = JsonSchemaException('data must contain less than or equal to 1 items',
                          value='{data}',
                          name='data',
                          definition='{definition}',
                          rule='maxItems')
示例#23
0
import datetime
import re

import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be date-time')


@pytest.mark.parametrize('value, expected', [
    ('', exc),
    ('bla', exc),
    ('2018-02-05T14:17:10.00', exc),
    ('2018-02-05T14:17:10.00Z\n', exc),
    ('2018-02-05T14:17:10.00Z', '2018-02-05T14:17:10.00Z'),
    ('2018-02-05T14:17:10Z', '2018-02-05T14:17:10Z'),
])
def test_datetime(asserter, value, expected):
    asserter({'type': 'string', 'format': 'date-time'}, value, expected)


exc = JsonSchemaException('data must be hostname')


@pytest.mark.parametrize('value, expected', [
    ('', exc),
    ('LDhsjf878&d', exc),
    ('bla.bla-', exc),
    ('example.example.com-', exc),
    ('example.example.com\n', exc),
示例#24
0
import pytest

from fastjsonschema import JsonSchemaException


@pytest.fixture(params=['number', 'integer'])
def number_type(request):
    return request.param


exc = JsonSchemaException('data must be {number_type}')


@pytest.mark.parametrize('value, expected', [
    (-5, -5),
    (0, 0),
    (5, 5),
    (None, exc),
    (True, exc),
    ('abc', exc),
    ([], exc),
    ({}, exc),
])
def test_number(asserter, number_type, value, expected):
    if isinstance(expected, JsonSchemaException):
        expected = JsonSchemaException(
            expected.message.format(number_type=number_type))
    asserter({'type': number_type}, value, expected)


exc = JsonSchemaException('data must be smaller than or equal to 10')
示例#25
0
import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be boolean')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, True),
    (False, False),
    ('abc', exc),
    ([], exc),
    ({}, exc),
])
def test_boolean(asserter, value, expected):
    asserter({'type': 'boolean'}, value, expected)
示例#26
0
def test_integer_is_not_number(asserter, value):
    asserter({
        'type': 'integer',
    }, value, JsonSchemaException('data must be integer'))
示例#27
0
import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be array')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    (False, exc),
    ('abc', exc),
    ([], []),
    ([1, 'a', True], [1, 'a', True]),
    ({}, exc),
])
def test_array(asserter, value, expected):
    asserter({'type': 'array'}, value, expected)


exc = JsonSchemaException('data must contain less than or equal to 1 items')


@pytest.mark.parametrize('value, expected', [
    ([], []),
    ([1], [1]),
    ([1, 1], exc),
    ([1, 2, 3], exc),
])
def test_max_items(asserter, value, expected):
示例#28
0
def test_number(asserter, number_type, value, expected):
    if isinstance(expected, JsonSchemaException):
        expected = JsonSchemaException(
            expected.message.format(number_type=number_type))
    asserter({'type': number_type}, value, expected)
示例#29
0
     }, 'str', 5, 'any'],
     [9, 'world', [1], {
         'a': 'a',
         'b': 'b',
         'c': 'xy'
     }, 'str', 5, 'any'],
 ),
 (
     [10, 'world', [1], {
         'a': 'a',
         'b': 'b',
         'c': 'xy'
     }, 'str', 5],
     JsonSchemaException('data[0] must be smaller than 10',
                         value=10,
                         name='data[0]',
                         definition=definition['items'][0],
                         rule='maximum'),
 ),
 (
     [9, 'xxx', [1], {
         'a': 'a',
         'b': 'b',
         'c': 'xy'
     }, 'str', 5],
     JsonSchemaException('data[1] must be one of [\'hello\', \'world\']',
                         value='xxx',
                         name='data[1]',
                         definition=definition['items'][1],
                         rule='enum'),
 ),
示例#30
0
import pytest

from fastjsonschema import JsonSchemaException

exc = JsonSchemaException('data must be null')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, None),
    (True, exc),
    ('abc', exc),
    ([], exc),
    ({}, exc),
])
def test_null(asserter, value, expected):
    asserter({'type': 'null'}, value, expected)