Esempio n. 1
0
def main():

    # data schema
    schema_path = Path("data-schema.yml").resolve()
    schema = load_compact_schema(schema_path)

    tojson(schema_path, schema)

    with schema_path.with_suffix(".py").open("wt") as fh:
        fh.write(fastjsonschema.compile_to_code(schema))

    # data
    data_path = Path("form-data.yml").resolve()
    with data_path.open() as fh:
        data = yaml.safe_load(fh)

    validate = fastjsonschema.compile(schema)
    validate(data)
    # jsonschema.validate(data, schema)

    tojson(data_path, data)

    # ui-schema
    ui_path = Path("ui-schema.yml").resolve()
    tojson(ui_path)
Esempio n. 2
0
def fast_file_not_comp(value, json_schema):
    with open('temp/speed.py', 'w') as fp:
        name, schema = fastjsonschema.compile_to_code(json_schema,
                                                      config=config)
        fp.write(schema)
    from temp.speed import validate
    validate(value)
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'
    }
Esempio n. 4
0
    def f(definition, value, expected, filename):
        config = Config(meta_schema='draft4', validate_schema=False)
        # When test fails, it will show up code.
        name, code = compile_to_code(definition, config=CONFIG)
        print(code)

        if not os.path.isdir('temp'):
            os.makedirs('temp')
        with open('temp/' + filename + '.py', 'w') as f:
            f.write(code)
        validator = getattr(importlib.import_module('temp.' + filename), name)
        if isinstance(expected, JsonSchemaException):
            with pytest.raises(JsonSchemaException) as exc:
                validator(value)
            assert exc.value.message == expected.message
        else:
            assert validator(value) == expected
Esempio n. 5
0
def check_config(
    config: Dict[str, Any],
    schemas: List[Dict[str, Any]],
    extra_formaters: Dict[str, Callable[[str], bool]] = None,
) -> None:
    """Check the configuration by provided list of validation schemas.

    :param config: Configuration to check
    :param schemas: List of validation schemas
    :param extra_formaters: Additional custom formaters
    :raises SPSDKError: Invalid validation schema or configuration
    """
    schema: Dict[str, Any] = {}
    for sch in schemas:
        deepmerge.always_merger.merge(schema, sch)
    formats = deepmerge.always_merger.merge(CUSTOM_FORMATERS, extra_formaters
                                            or {})
    try:
        if ENABLE_DEBUG:
            validator_code = fastjsonschema.compile_to_code(schema,
                                                            formats=formats)
            with open("validator_file.py", "w") as f:
                f.write(validator_code)
        else:
            validator = fastjsonschema.compile(schema, formats=formats)
    except (TypeError, fastjsonschema.JsonSchemaDefinitionException) as exc:
        raise SPSDKError(
            f"Invalid validation schema to check config: {str(exc)}") from exc
    try:
        config_to_check = copy.deepcopy(config)
        if ENABLE_DEBUG:
            # pylint: disable=import-error,import-outside-toplevel
            import validator_file  # type: ignore

            validator_file.validate(config_to_check)
        else:
            validator(config_to_check)
    except fastjsonschema.JsonSchemaValueException as exc:
        message = str(exc)
        if exc.rule == "required":
            missing = filter(lambda x: x not in exc.value.keys(),
                             exc.rule_definition)
            message += f"; Missing field(s): {', '.join(missing)}"
        raise SPSDKError(
            f"Configuration validation failed: {message}") from exc
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,
        'c': 'example.com',
    }
Esempio n. 7
0
    with open('temp/speed.py', 'w') as fp:
        name, schema = fastjsonschema.compile_to_code(json_schema,
                                                      config=config)
        fp.write(schema)
    from temp.speed import validate
    validate(value)


config = fastjsonschema.Config(meta_schema='draft4')
fastjsonschema_validate = fastjsonschema.compile(JSON_SCHEMA, config=config)
fast_compiled = lambda value, _: fastjsonschema_validate(value)

fast_not_compiled = lambda value, json_schema: fastjsonschema.compile(
    json_schema, config=config)(value)

name, code = fastjsonschema.compile_to_code(JSON_SCHEMA, config=config)
with open('temp/performance.py', 'w') as f:
    f.write(code)
from temp.performance import validate

fast_file = lambda value, _: validate(value)

jsonspec = load(JSON_SCHEMA)
jsonschema_validator = jsonschema.Draft4Validator(JSON_SCHEMA)
jsonschema_compiled = lambda value, _: jsonschema_validator.validate(value)


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

    setup = """from __main__ import (

def precisionlife_fast_not_compiled(value, json_schema):
    precisionlife_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+t', suffix='.py', dir='.', delete=False) 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)

with tempfile.NamedTemporaryFile('w+t', suffix='.py', dir='.', delete=False) as tmp_file:
    tmp_file.write(precisionlife_fastjsonschema.compile_to_code(JSON_SCHEMA))
    tmp_file.flush()
    spec = importlib.util.spec_from_file_location("precisionlife_temp.performance", tmp_file.name)
    precisionlife_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(precisionlife_module)


def fast_file(value, _):
    module.validate(value)
Esempio n. 9
0
for database_name, database in DATABASES.items():
    database_base_name = database_name.replace('.json', '')
    database_schema_name = database_base_name + '.schema.json'
    database_schema = DATABASE_SCHEMAS.get(database_schema_name)

    if not database_schema:
        print(f'No schema for {database_name}, skipping validation.')
        continue

    try:
        fjs.validate(database_schema, database, handlers=handlers)

    except fjs.JsonSchemaException as jsce:
        print(
            f'Validating database {database_name} with schema {database_schema_name}'
        )
        with open(os.path.join(SCRIPT_DIRECTORY, 'generated'), 'w') as tmp_gen:
            tmp_gen.write(
                fjs.compile_to_code(database_schema, handlers=handlers))
        traceback.print_exc()
        print(f'path={jsce.path}')
        print()

    except Exception as e:
        print(
            f'Validating database {database_name} with schema {database_schema_name}'
        )
        traceback.print_exc()
        print()
def main():
    with open('geotaxi/jsonschema.py', 'w') as f:
        f.write(WARNING_MESSAGE)
        f.write(fastjsonschema.compile_to_code(API_SCHEMA))
Esempio n. 11
0
        if not filename.endswith('-schema.yaml'):
            continue
        with open(os.path.join(schema_dir, filename), 'r') as f:
            schema = yaml.safe_load(f)
        # Required for relative URI parsing.
        schema['$id'] = 'file:' + temp_dir + '/' + filename
        with open(os.path.join(temp_dir, filename), 'w') as f:
            json.dump(schema, f)

    for filename in os.listdir(temp_dir):
        # Remember, the filename is still ending -schema.yaml
        code_name = filename[:-12].replace('-', '_')
        print("Processing " + filename)
        with open(os.path.join(temp_dir, filename), 'r') as f:
            schema = json.load(f)
        code = fastjsonschema.compile_to_code(schema)
        to_replace_src_name_1 = ('file:' + temp_dir + '/').replace(
            ':', '_').replace('/', '_')
        to_replace_src_name_2 = ('file://' + temp_dir + '/').replace(
            ':', '_').replace('/', '_')
        code = (code.replace(to_replace_src_name_1, '').replace(
            to_replace_src_name_2,
            '').replace('from fastjsonschema ',
                        'from ..fastjsonschema_replacement ').replace(
                            '(data):',
                            '(data: Dict[str, Any]) -> Dict[str, Any]:'))
        with open(os.path.join(code_output_dir, code_name + '.py'), 'w') as f:
            f.write(
                "# DO NOT MODIFY\n# AUTO-GENERATED CODE.\n\n# pylint: ignore\n\nfrom typing import Dict, Any\n\n"
            )
            f.write(code)