How to use the fastjsonschema.compile_to_code function in fastjsonschema

To help you get started, we’ve selected a few fastjsonschema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github horejsek / python-fastjsonschema / tests / test_compile_to_code.py View on Github external
def test_compile_to_code():
    code = compile_to_code({
        'properties': {
            'a': {'type': 'string'},
            'b': {'type': 'integer'},
            'c': {'format': 'hostname'},  # Test generation of regex patterns to the file.
        }
    })
    with open('temp/schema_1.py', 'w') as f:
        f.write(code)
    from temp.schema_1 import validate
    assert validate({
        'a': 'a',
        'b': 1, 
        'c': 'example.com',
    }) == {
        'a': 'a',
        'b': 1,
github horejsek / python-fastjsonschema / tests / test_compile_to_code.py View on Github external
def test_compile_to_code_ipv6_regex():
    code = compile_to_code({
        'properties': {
            'ip': {'format': 'ipv6'},
        }
    })
    with open('temp/schema_2.py', 'w') as f:
        f.write(code)
    from temp.schema_2 import validate
    assert validate({
        'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
    }) == {
        'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
    }
github horejsek / python-fastjsonschema / performance.py View on Github external
def fast_not_compiled(value, json_schema):
    fastjsonschema.compile(json_schema)(value)


validator_class = jsonschema.validators.validator_for(JSON_SCHEMA)
validator = validator_class(JSON_SCHEMA)


def jsonschema_compiled(value, _):
    validator.validate(value)


with tempfile.NamedTemporaryFile('w', suffix='.py') as tmp_file:
    tmp_file.write(fastjsonschema.compile_to_code(JSON_SCHEMA))
    tmp_file.flush()
    spec = importlib.util.spec_from_file_location("temp.performance", tmp_file.name)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)


def fast_file(value, _):
    module.validate(value)


jsonspec = load(JSON_SCHEMA)


def t(func, valid_values=True):
    module = func.split('.')[0]