Python jsonschema.FormatChecker() Examples

The following are code examples for showing how to use jsonschema.FormatChecker(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: singer-tools   Author: singer-io   File: check_tap.py    Apache License 2.0 6 votes vote down vote up
def add(self, message):
        if isinstance(message, singer.RecordMessage):
            stream = self.ensure_stream(message.stream)
            if stream.latest_schema:
                validator_fn = extend_with_default(Draft4Validator)
                validator = validator_fn(
                    stream.latest_schema, format_checker=FormatChecker())
                validator.validate(copy.deepcopy(message.record))
            else:
                print('I saw a record for stream {} before the schema'.format(
                    message.stream))
                exit(1)
            stream.num_records += 1

        elif isinstance(message, singer.SchemaMessage):
            stream = self.ensure_stream(message.stream)
            stream.num_schemas += 1
            stream.latest_schema = message.schema

        elif isinstance(message, singer.StateMessage):
            self.latest_state = message.value
            self.num_states += 1 
Example 2
Project: nova   Author: ZhanHan   File: util.py    Apache License 2.0 6 votes vote down vote up
def extract_json(body, schema):
    """Extract JSON from a body and validate with the provided schema."""
    try:
        data = jsonutils.loads(body)
    except ValueError as exc:
        raise webob.exc.HTTPBadRequest(
            _('Malformed JSON: %(error)s') % {'error': exc},
            json_formatter=json_error_formatter)
    try:
        jsonschema.validate(data, schema,
                            format_checker=jsonschema.FormatChecker())
    except jsonschema.ValidationError as exc:
        raise webob.exc.HTTPBadRequest(
            _('JSON does not validate: %(error)s') % {'error': exc},
            json_formatter=json_error_formatter)
    return data 
Example 3
Project: nova   Author: ZhanHan   File: allocation.py    Apache License 2.0 6 votes vote down vote up
def _extract_allocations(body, schema):
    """Extract allocation data from a JSON body."""
    try:
        data = jsonutils.loads(body)
    except ValueError as exc:
        raise webob.exc.HTTPBadRequest(
            _('Malformed JSON: %(error)s') % {'error': exc},
            json_formatter=util.json_error_formatter)
    try:
        jsonschema.validate(data, schema,
                            format_checker=jsonschema.FormatChecker())
    except jsonschema.ValidationError as exc:
        raise webob.exc.HTTPBadRequest(
            _('JSON does not validate: %(error)s') % {'error': exc},
            json_formatter=util.json_error_formatter)
    return data 
Example 4
Project: OmicronServer   Author: OmicronProject   File: json_schema_parser.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, path):
        """
        Initialize the schema
        :param path: the path from which the file should be pulled
        """
        if not os.path.isfile(path):
            raise FileNotFoundError('Unable to find file with path <%s>' %\
                                    path)
        else:
            self.json_dict = self._read_schema_from_file(path)
        format_checker = jsonschema.FormatChecker()
        is_draft_3 = format_checker.conforms(self.json_dict, 'draft3')
        is_draft_4 = format_checker.conforms(self.json_dict, 'draft4')

        if not (is_draft_3 or is_draft_4):
            raise BadJsonSchemaError(
                'schema dict %s does not correspond to a draft 3 or draft '
                '4 JSON Schema', self.json_dict)

        self.path = path 
Example 5
Project: flask_template   Author: ricardochaves   File: test_contract_routes.py    MIT License 6 votes vote down vote up
def test_contract_githubuser_route(client):
    """Contract: Test githubuser route."""

    schema = {
        "type": "object",
        "properties": {
            "urser_name": {"type": "string"},
            "id": {"type": "integer", "minimum": 1},
            "created_at": {"type": "string", "format": "date-time"},
        },
        "required": ["urser_name", "id", "created_at"],
    }

    response = client.get("/githubuser/ricardochaves")

    validate(response.json, schema, format_checker=FormatChecker()) 
Example 6
Project: acceptable   Author: canonical-ols   File: test_djangoutils.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_errors(self, form, data):
        schema = djangoutil.get_form_schema(form)
        validator = Draft4Validator(schema, format_checker=FormatChecker())

        # manipulate jsonschema errors to look like django form error dict
        schema_errors = {}
        for error in validator.iter_errors(data):
            if error.path:
                # strip out 'foo.n' naming for arrays to just 'foo'
                key = '.'.join(
                    i for i in error.path if isinstance(i, (str, bytes))
                )
            else:
                # missing required fields are not keyed by path
                key = error.validator_value[0]
            schema_errors[key] = error.message
        return form(data=data).errors, schema_errors 
Example 7
Project: cadasta-platform   Author: Cadasta   File: validators.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def validate_json(value, schema):
    v = Draft4Validator(schema, format_checker=FormatChecker())
    errors = sorted(v.iter_errors(value), key=lambda e: e.path)

    message_dict = {}
    for e in errors:
        if e.validator == 'anyOf':
            fields = [
                f.message.split(' ')[0].replace('\'', '') for f in e.context
            ]

            for f in fields:
                message_dict[f] = _("Please provide either {}").format(
                    _(" or ").join(fields))

        elif e.validator == 'required':
            field = e.message.split(' ')[0].replace('\'', '')
            message_dict[field] = _("This field is required.")
        else:
            field = '.'.join([str(el) for el in e.path])
            message_dict[field] = e.message

    if message_dict:
        raise JsonValidationError(message_dict) 
Example 8
Project: placement   Author: openstack   File: util.py    Apache License 2.0 6 votes vote down vote up
def extract_json(body, schema):
    """Extract JSON from a body and validate with the provided schema."""
    try:
        data = jsonutils.loads(body)
    except ValueError as exc:
        raise webob.exc.HTTPBadRequest(
            'Malformed JSON: %(error)s' % {'error': exc},
            json_formatter=json_error_formatter)
    try:
        jsonschema.validate(data, schema,
                            format_checker=jsonschema.FormatChecker())
    except jsonschema.ValidationError as exc:
        raise webob.exc.HTTPBadRequest(
            'JSON does not validate: %(error)s' % {'error': exc},
            json_formatter=json_error_formatter)
    return data 
Example 9
Project: rdss-message-api-specification   Author: JiscSD   File: validator.py    Apache License 2.0 6 votes vote down vote up
def __init__(self):
        self._schemas = self._load_schemas()
        self.resolver = jsonschema.RefResolver(
            '',
            {},
            store={
                schema['$id']: schema
                for schema in self._schemas
            }
        )
        self.format_checker = jsonschema.FormatChecker()

        self.schema_lookup = {
            full_id: definition_schema
            for schema in self._schemas
            for full_id, definition_schema in self._generate_definition_schemas(schema)
        } 
Example 10
Project: flasgger   Author: flasgger   File: base.py    MIT License 6 votes vote down vote up
def init_app(self, app, decorators=None):
        """
        Initialize the app with Swagger plugin
        """
        self.decorators = decorators or self.decorators
        self.app = app

        self.load_config(app)
        # self.load_apispec(app)
        if self.template_file is not None:
            self.template = self.load_swagger_file(self.template_file)
        self.register_views(app)
        self.add_headers(app)

        if self.parse:
            if RequestParser is None:
                raise RuntimeError('Please install flask_restful')
            self.parsers = {}
            self.schemas = {}
            self.format_checker = jsonschema.FormatChecker()
            self.parse_request(app)

        self._configured = True
        app.swag = self 
Example 11
Project: arche   Author: scrapinghub   File: schema.py    MIT License 6 votes vote down vote up
def full_validate(
    schema: RawSchema, raw_items: RawItems, keys: pd.Index
) -> Dict[str, set]:
    """This function uses jsonschema validator which returns all found error per item.
    See `fast_validate()` for arguments descriptions.
    """
    errors: DefaultDict = defaultdict(set)

    validator = validators.validator_for(schema)(schema)
    validator.format_checker = FormatChecker()
    for i, raw_item in enumerate(tqdm(raw_items, desc="JSON Schema Validation")):
        raw_item.pop("_type", None)
        raw_item.pop("_key", None)
        for e in validator.iter_errors(raw_item):
            error = format_validation_message(
                e.message, e.path, e.schema_path, e.validator
            )
            errors[error].add(keys[i])
    return dict(errors) 
Example 12
Project: coriolis   Author: cloudbase   File: replica_schedules.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def _validate_create_body(self, body):
        schedule = body.get("schedule")
        if schedule is None:
            raise exception.InvalidInput(
                "schedule is required")
        schedule = self._validate_schedule(schedule)
        schemas.validate_value(
            body, schemas.SCHEDULE_API_BODY_SCHEMA,
            format_checker=jsonschema.FormatChecker())

        enabled = body.get("enabled", True)
        exp = body.get("expiration_date", None)
        if exp is not None:
            exp = self._validate_expiration_date(exp)
        shutdown = body.get("shutdown_instance", False)
        return (schedule, enabled, exp, shutdown) 
Example 13
Project: coriolis   Author: cloudbase   File: replica_schedules.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def _validate_update_body(self, update_body):
        body = {}
        schedule = update_body.get("schedule")
        if schedule is not None:
            schedule = self._validate_schedule(schedule)
            body["schedule"] = schedule
        enabled = update_body.get("enabled")
        if enabled is not None:
            body["enabled"] = enabled
        shutdown = update_body.get("shutdown_instance")
        if shutdown is not None:
            body["shutdown_instance"] = shutdown
        schemas.validate_value(
            body, schemas.SCHEDULE_API_BODY_SCHEMA,
            format_checker=jsonschema.FormatChecker())

        exp = None
        if "expiration_date" in update_body:
            exp = self._validate_expiration_date(
                update_body.get("expiration_date"))
            body["expiration_date"] = exp
        return body 
Example 14
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema, relax_additional_properties=False):
        validators = {
            'minimum': self._validate_minimum,
            'maximum': self._validate_maximum,
        }
        if relax_additional_properties:
            validators[
                'additionalProperties'] = _soft_validate_additional_properties

        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators)
        format_checker = FormatChecker()
        self.validator = validator_cls(schema, format_checker=format_checker) 
Example 15
Project: pydatajson   Author: datosgobar   File: validation.py    MIT License 5 votes vote down vote up
def init_jsonschema_validator(self, schema_dir, schema_filename):
        schema_path = os.path.join(schema_dir, schema_filename)
        schema = readers.read_json(schema_path)
        # SegĂșn https://github.com/Julian/jsonschema/issues/98
        # Permite resolver referencias locales a otros esquemas.
        if platform.system() == 'Windows':
            base_uri = "file:///" + schema_path.replace("\\", "/")
        else:
            base_uri = "file://" + schema_path
        resolver = jsonschema.RefResolver(base_uri=base_uri, referrer=schema)
        format_checker = jsonschema.FormatChecker()
        return jsonschema.Draft4Validator(
            schema=schema, resolver=resolver, format_checker=format_checker) 
Example 16
Project: ocds-merge   Author: open-contracting   File: test_fixtures.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_valid(filename, schema):
    errors = 0

    with open(filename) as f:
        data = json.load(f)
    if filename.endswith('-versioned.json') or filename.endswith('-compiled.json'):
        data = [data]

    for datum in data:
        for error in validator(schema, format_checker=FormatChecker()).iter_errors(datum):
            errors += 1
            warnings.warn(json.dumps(error.instance, indent=2, separators=(',', ': ')))
            warnings.warn('{} ({})\n'.format(error.message, '/'.join(error.absolute_schema_path)))

    assert errors == 0, '{} is invalid. See warnings below.'.format(filename) 
Example 17
Project: zun   Author: openstack   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema, is_body=True):
        self.is_body = is_body
        validators = {
            'minimum': self._validate_minimum,
            'maximum': self._validate_maximum
        }
        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators)
        fc = jsonschema.FormatChecker()
        self.validator = validator_cls(schema, format_checker=fc) 
Example 18
Project: nova   Author: ZhanHan   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema, relax_additional_properties=False):
        validators = {
            'minimum': self._validate_minimum,
            'maximum': self._validate_maximum,
        }
        if relax_additional_properties:
            validators[
                'additionalProperties'] = _soft_validate_additional_properties

        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators)
        format_checker = FormatChecker()
        self.validator = validator_cls(schema, format_checker=format_checker) 
Example 19
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_user(data):
    """
    Validate request json parameters
    :param data:
    :return:
    """
    try:
        validate(data, user_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 20
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_signin(data):
    """
    Validate request json parameters
    :param data:
    :return:
    """
    try:
        validate(data, user_signin_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 21
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_user(data):
    try:
        validate(data, user_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 22
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_signin(data):
    try:
        validate(data, user_signin_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 23
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_user(data):
    try:
        validate(data, user_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 24
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_user(data):
    try:
        validate(data, user_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 25
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_signin(data):
    try:
        validate(data, user_signin_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 26
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_user(data):
    try:
        validate(data, user_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 27
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: __init__.py    MIT License 5 votes vote down vote up
def validate_signin(data):
    try:
        validate(data, user_signin_schema, format_checker=FormatChecker())
        return {'ok': True, 'data': data}
    except ValidationError as e:
        raise e
    except SchemaError as e:
        raise e
    except Exception as e:
        raise e 
Example 28
Project: pyrs-schema   Author: palankai   File: base.py    MIT License 5 votes vote down vote up
def _make_validator(schema):
    format_checker = jsonschema.FormatChecker(formats.draft4_format_checkers)
    validator_funcs = jsonschema.Draft4Validator.VALIDATORS
    validator_funcs[u'type'] = _validate_type_draft4
    meta_schema = jsonschema.Draft4Validator.META_SCHEMA
    validator_cls = jsonschema.validators.create(
        meta_schema=meta_schema,
        validators=validator_funcs,
        version="draft4",
    )
    validator_cls.check_schema(schema)
    return validator_cls(schema, format_checker=format_checker) 
Example 29
Project: netjsonconfig   Author: openwisp   File: backend.py    GNU General Public License v3.0 5 votes vote down vote up
def validate(self):
        try:
            validate(self.config, self.schema, format_checker=FormatChecker())
        except JsonSchemaError as e:
            raise ValidationError(e) 
Example 30
Project: pantalaimon   Author: matrix-org   File: client.py    Apache License 2.0 5 votes vote down vote up
def validate_json(instance, schema):
    """Validate a dictionary using the provided json schema."""
    Validator(schema, format_checker=FormatChecker()).validate(instance) 
Example 31
Project: pxm-manifest-specification   Author: mapbox   File: manifest.py    MIT License 5 votes vote down vote up
def test_json_schema(runner):
    """Tests that the json schema is in sync with this code."""
    schema_dir = os.path.dirname(os.path.realpath(__file__))
    fname = os.path.join(schema_dir, f'schemas/pxm-manifest-{version}.json')
    with open(fname) as f:
        schema = json.load(f)

        result = runner.invoke(create_manifest, manifest_args)
        doc = json.loads(result.output)

        assert result.exit_code == 0
        # if an exception is raised by validate then the test fails
        validate(doc, schema, format_checker=FormatChecker()) 
Example 32
Project: stx-nova   Author: starlingx-staging   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema, relax_additional_properties=False,
                 is_body=True):
        self.is_body = is_body
        validators = {
            'minimum': self._validate_minimum,
            'maximum': self._validate_maximum,
        }
        if relax_additional_properties:
            validators[
                'additionalProperties'] = _soft_validate_additional_properties

        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators)
        format_checker = FormatChecker()
        self.validator = validator_cls(schema, format_checker=format_checker) 
Example 33
Project: target-stitch   Author: singer-io   File: __init__.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def handle_batch(self, messages, contains_activate_version, schema, key_names, bookmark_names=None, state_writer=None, state=None): # pylint: disable=no-self-use,unused-argument
        '''Handles messages by validating them against schema.'''
        LOGGER.info("ValidatingHandler handle_batch")
        validator = Draft4Validator(schema, format_checker=FormatChecker())
        for i, message in enumerate(messages):
            if isinstance(message, singer.RecordMessage):
                try:
                    validator.validate(message.record)
                    if key_names:
                        for k in key_names:
                            if k not in message.record:
                                raise TargetStitchException(
                                    'Message {} is missing key property {}'.format(
                                        i, k))
                except Exception as e:
                    raise TargetStitchException(
                        'Record does not pass schema validation: {}'.format(e))

        # pylint: disable=undefined-loop-variable
        # NB: This seems incorrect as there's a chance message is not defined
        LOGGER.info('%s (%s): Batch is valid',
                    messages[0].stream,
                    len(messages))
        if state:
            line = simplejson.dumps(state)
            state_writer.write("{}\n".format(line))
            state_writer.flush() 
Example 34
Project: notifications-api   Author: alphagov   File: __init__.py    MIT License 5 votes vote down vote up
def validate_v0(json_to_validate, schema_filename):
    schema_dir = os.path.join(os.path.dirname(__file__), 'schemas/v0')
    resolver = jsonschema.RefResolver('file://' + schema_dir + '/', None)
    with open(os.path.join(schema_dir, schema_filename)) as schema:
        jsonschema.validate(
            json_to_validate,
            json.load(schema),
            format_checker=jsonschema.FormatChecker(),
            resolver=resolver
        ) 
Example 35
Project: falcon-confiante   Author: ecarrara   File: middleware.py    MIT License 5 votes vote down vote up
def __init__(
        self,
        openapi_spec: dict,
        strict_mode: bool = True,
        validate_response: bool = False,
    ):
        self.openapi_spec = openapi_spec
        self.format_checker = FormatChecker()
        self.validators = {}
        self.strict_mode = strict_mode
        self.validate_response = validate_response 
Example 36
Project: acceptable   Author: canonical-ols   File: _validation.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def validate(payload, schema):
    """Validate `payload` against `schema`, returning an error list.

    jsonschema provides lots of information in it's errors, but it can be a bit
    of work to extract all the information.
    """
    v = jsonschema.Draft4Validator(
        schema, format_checker=jsonschema.FormatChecker())
    error_list = []
    for error in v.iter_errors(payload):
        message = error.message
        location = '/' + '/'.join([str(c) for c in error.absolute_path])
        error_list.append(message + ' at ' + location)
    return error_list 
Example 37
Project: cjio   Author: cityjson   File: validation.py    MIT License 5 votes vote down vote up
def validate_against_schema(j, js):
    isValid = True
    es = []
    #-- lazy validation to catch as many as possible
    myvalidator = jsonschema.Draft4Validator(js, format_checker=jsonschema.FormatChecker())
    for err in sorted(myvalidator.iter_errors(j), key=str):
        isValid = False
        es.append(err.message)
    return (isValid, es)

    # try:
    #     jsonschema.Draft4Validator(js, format_checker=jsonschema.FormatChecker()).validate(j)
    # except jsonschema.ValidationError as e:
    #     raise Exception(e.message)
    #     return False
    # except jsonschema.SchemaError as e:
    #     raise Exception(e.message)
    #     return False
    
    # try:
    #     jsonschema.validate(j, js, format_checker=jsonschema.FormatChecker())
    # except jsonschema.ValidationError as e:
    #     raise Exception(e.message)
    #     return False
    # except jsonschema.SchemaError as e:
    #     raise Exception(e.message)
    #     return False 
Example 38
Project: paasta   Author: Yelp   File: validate.py    Apache License 2.0 5 votes vote down vote up
def validate_schema(file_path, file_type):
    """Check if the specified config file has a valid schema

    :param file_path: path to file to validate
    :param file_type: what schema type should we validate against
    """
    try:
        schema = get_schema(file_type)
    except Exception as e:
        paasta_print(f"{SCHEMA_ERROR}: {file_type}, error: {e!r}")
        return

    if schema is None:
        paasta_print(f"{SCHEMA_NOT_FOUND}: {file_path}")
        return
    validator = Draft4Validator(schema, format_checker=FormatChecker())
    basename = os.path.basename(file_path)
    config_file_object = get_config_file_dict(file_path)
    try:
        validator.validate(config_file_object)
        if file_type == "kubernetes" and not validate_instance_names(
            config_file_object, file_path
        ):
            return
    except ValidationError:
        paasta_print(f"{SCHEMA_INVALID}: {file_path}")

        errors = validator.iter_errors(config_file_object)
        paasta_print("  Validation Message: %s" % exceptions.best_match(errors).message)
    except Exception as e:
        paasta_print(f"{SCHEMA_ERROR}: {file_type}, error: {e!r}")
        return
    else:
        paasta_print(f"{SCHEMA_VALID}: {basename}")
        return True 
Example 39
Project: MDStudio   Author: MD-Studio   File: schema.py    Apache License 2.0 5 votes vote down vote up
def validate_json_schema(schema_def, instance):
    DefaultValidatingDraft4Validator(schema_def, format_checker=FormatChecker()).validate(instance) 
Example 40
Project: tozti   Author: tozti   File: utils.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def validate(inst, schema):
    """Validate data against a JsonSchema."""

    return jsonschema.validate(inst, schema, cls=jsonschema.Draft4Validator,
                               format_checker=jsonschema.FormatChecker()) 
Example 41
Project: dci-control-server   Author: redhat-cip   File: schemas.py    Apache License 2.0 5 votes vote down vote up
def check_json_is_valid(schema, json):
    v = DCIValidator(schema, format_checker=FormatChecker())
    errors = []
    for error in sorted(v.iter_errors(json), key=str):
        errors.append(_get_error_message(error))
    if len(errors):
        raise DCIException("Request malformed", {"errors": errors, "error": errors[0]}) 
Example 42
Project: hws   Author: nash-x   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema):
        validators = {
            'minimum': self._validate_minimum,
            'maximum': self._validate_maximum,
        }
        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators)
        format_checker = jsonschema.FormatChecker()
        self.validator = validator_cls(schema, format_checker=format_checker) 
Example 43
Project: placement   Author: openstack   File: util.py    Apache License 2.0 5 votes vote down vote up
def validate_query_params(req, schema):
    try:
        # NOTE(Kevin_Zheng): The webob package throws UnicodeError when
        # param cannot be decoded. Catch this and raise HTTP 400.
        jsonschema.validate(dict(req.GET), schema,
                            format_checker=jsonschema.FormatChecker())
    except (jsonschema.ValidationError, UnicodeDecodeError) as exc:
        raise webob.exc.HTTPBadRequest(
            'Invalid query string parameters: %(exc)s' %
            {'exc': exc}) 
Example 44
Project: target-postgres   Author: datamill-co   File: singer_stream.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def update_schema(self, schema, key_properties):
        # In order to determine whether a value _is in_ properties _or not_ we need to flatten `$ref`s etc.
        self.schema = json_schema.simplify(schema)
        self.key_properties = deepcopy(key_properties)
        self.validator = Draft4Validator(self.schema, format_checker=FormatChecker())

        properties = self.schema['properties']

        if SINGER_RECEIVED_AT not in properties:
            properties[SINGER_RECEIVED_AT] = {
                'type': ['null', 'string'],
                'format': 'date-time'
            }

        if SINGER_SEQUENCE not in properties:
            properties[SINGER_SEQUENCE] = {
                'type': ['null', 'integer']
            }

        if SINGER_TABLE_VERSION not in properties:
            properties[SINGER_TABLE_VERSION] = {
                'type': ['null', 'integer']
            }

        if SINGER_BATCHED_AT not in properties:
            properties[SINGER_BATCHED_AT] = {
                'type': ['null', 'string'],
                'format': 'date-time'
            }

        if len(self.key_properties) == 0:
            self.use_uuid_pk = True
            self.key_properties = [SINGER_PK]
            properties[SINGER_PK] = {
                'type': ['string']
            }
        else:
            self.use_uuid_pk = False 
Example 45
Project: ocdskit   Author: open-contracting   File: validate.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def handle(self):
        components = urlparse(self.args.schema)
        if components.scheme == 'file':
            with open(self.args.schema[7:]) as f:
                schema = json.load(f)
        else:
            schema = requests.get(self.args.schema).json()

        format_checker = FormatChecker()
        if self.args.check_urls:
            def check_url(instance):
                # See https://github.com/Julian/jsonschema/blob/master/jsonschema/_format.py
                if not isinstance(instance, str_types):
                    return True
                rfc3987.parse(instance, rule='URI')  # raises ValueError
                try:
                    response = requests.get(instance, timeout=self.args.timeout)
                    result = response.status_code in (200,)
                    if not result:
                        print('HTTP {} on GET {}'.format(response.status_code, instance))
                    return result
                except requests.exceptions.Timeout:
                    print('Timedout on GET {}'.format(instance))
                    return False

            format_checker.checks('uri', raises=(ValueError))(check_url)

        for i, data in enumerate(self.items()):
            errors = False
            for error in validator(schema, format_checker=format_checker).iter_errors(data):
                print('item {}: {} ({})'.format(i, error.message, '/'.join(error.absolute_schema_path)))
                errors = True
            if not errors and self.args.verbose:
                print('item {}: no errors'.format(i)) 
Example 46
Project: shadowChain   Author: zh-explorer   File: config_decode.py    Apache License 2.0 5 votes vote down vote up
def make_validator():
    global top_validator, client_validator
    top_schema, client_schema = schema_data["top_schema"], schema_data["client_schema"]
    default_validating = extend_with_default(jsonschema.Draft4Validator)
    top_validator = default_validating(top_schema, format_checker=jsonschema.FormatChecker())
    client_validator = default_validating(client_schema, format_checker=jsonschema.FormatChecker()) 
Example 47
Project: power-up   Author: IBM   File: validate_config_schema.py    Apache License 2.0 5 votes vote down vote up
def validate_config_schema(self):
        """Config schema validation

        Exception:
            If schema validation fails
        """

        schema = SchemaDefinition.get_schema(ordered=True)
        try:
            validate(
                self.config, schema, format_checker=jsonschema.FormatChecker())
        except jsonschema.exceptions.ValidationError as error:
            if error.cause is None:
                path = None
                for index, element in enumerate(error.path):
                    if isinstance(element, int):
                        path += '[{}]'.format(element)
                    else:
                        if index == 0:
                            path = '{}'.format(element)
                        else:
                            path += '.{}'.format(element)
                exc = 'Schema validation failed - {} - {}'.format(
                    path, str(error))
            else:
                exc = 'Schema validation failed - {} - {}'.format(
                    error.cause, str(error))
            if 'Additional properties are not allowed' in str(error):
                raise UserException(exc)
            else:
                raise UserCriticalException(exc) 
Example 48
Project: masakari   Author: openstack   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema, relax_additional_properties=False):
        validators = {
            'minimum': self._validate_minimum,
            'maximum': self._validate_maximum,
        }
        if relax_additional_properties:
            validators[
                'additionalProperties'] = _soft_validate_additional_properties

        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators)
        format_checker = FormatChecker()
        self.validator = validator_cls(schema, format_checker=format_checker) 
Example 49
Project: EuropePMC-Annotation-Validator   Author: EuropePMC   File: AnnotationValidator.py    Apache License 2.0 5 votes vote down vote up
def getSchema(inputtype):
        errorLog = list()
        validSchema = None
        if inputtype == 'NAMED_ENTITY':
            try:
                request_schema = requests.get('http://europepmc.org/docs/ne_annotation_schema.json') 
                ne_schemaObj = request_schema.json()
                validSchema =  Draft4Validator(ne_schemaObj, format_checker=FormatChecker())
                    
            except Exception, nescherr:
                errorLog.append(repr(nescherr)) 
Example 50
Project: invenio-records   Author: inveniosoftware   File: test_api.py    MIT License 5 votes vote down vote up
def test_validate_with_format(app, db):
    """Test that validation can accept custom format rules."""
    with app.app_context():
        checker = FormatChecker()
        checker.checks('foo')(lambda el: el.startswith('foo'))
        data = {
            'bar': 'foo',
            '$schema': {
                'properties': {
                    'bar': {'format': 'foo'}
                }
            }
        }

        # test record creation with valid data
        assert data == Record.create(data)
        record = Record.create(data, format_checker=checker)
        # test direct call to validate with valid data
        assert record.validate(format_checker=checker) is None
        # test commit with valid data
        record.commit(format_checker=checker)

        record['bar'] = 'bar'
        # test direct call to validate with invalid data
        with pytest.raises(ValidationError) as excinfo:
            record.validate(format_checker=checker)
        assert "'bar' is not a 'foo'" in str(excinfo.value)
        # test commit with invalid data
        with pytest.raises(ValidationError) as excinfo:
            record.commit(format_checker=checker)
        assert "'bar' is not a 'foo'" in str(excinfo.value)

        data['bar'] = 'bar'
        # test record creation with invalid data
        with pytest.raises(ValidationError) as excinfo:
            record = Record.create(data, format_checker=checker)
        assert "'bar' is not a 'foo'" in str(excinfo.value) 
Example 51
Project: sahara-tests   Author: openstack   File: validation.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema):
        format_checker = jsonschema.FormatChecker()
        super(Validator, self).__init__(
            schema, format_checker=format_checker) 
Example 52
Project: arche   Author: scrapinghub   File: test_integration.py    MIT License 5 votes vote down vote up
def test_format(instance, format_value):
    with pytest.raises(ValidationError) as excinfo:
        validate(instance, {"format": format_value}, format_checker=FormatChecker())
    assert f"'{instance}' is not a '{format_value}'" in str(excinfo.value) 
Example 53
Project: zun   Author: kevin-zhaoshuai   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema, is_body=True):
        self.is_body = is_body
        validators = {
            'minimum': self._validate_minimum,
            'maximum': self._validate_maximum
        }
        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators)
        fc = jsonschema.FormatChecker()
        self.validator = validator_cls(schema, format_checker=fc) 
Example 54
Project: hyputils   Author: tgbugs   File: base.py    MIT License 5 votes vote down vote up
def __init__(self):
        format_checker = jsonschema.FormatChecker()
        self.validator = jsonschema.Draft4Validator(
            self.schema, format_checker=format_checker
        ) 
Example 55
Project: bbtornado   Author: bakkenbaeck   File: validate.py    MIT License 5 votes vote down vote up
def validate_json(json_data,
                  json_schema=None,
                  json_example=None,
                  validator_cls=None,
                  format_checker=jsonschema.FormatChecker(),
                  on_empty_404=False):
    # if output is empty, auto return the error 404.
    if not json_data and on_empty_404:
        raise JsonError(404, "Not found.")

    if json_schema is not None:
        json_data = _to_json(json_data)
        # We wrap output in an object before validating in case
        # output is a string (and ergo not a validatable JSON
        # object)
        jsonschema.validate(
            {
                "result": json_data
            },
            {
                "type": "object",
                "properties": {
                    "result": json_schema
                },
                "required": ["result"]
            },
            cls=validator_cls,
            format_checker=format_checker
        )

    return json_data 
Example 56
Project: mopidy-packages   Author: mopidy   File: models.py    Apache License 2.0 5 votes vote down vote up
def _get_data(self, path):
        if not path.exists():
            return None

        with path.open() as fh:
            data = json.load(fh)

        try:
            jsonschema.validate(
                data, self.get_schema(),
                format_checker=jsonschema.FormatChecker())
        except jsonschema.ValidationError as exc:
            raise ModelException('Invalid JSON structure: %s' % exc) from exc
        else:
            return data 
Example 57
Project: keystone_docker   Author: tobegit3hub   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema):
        # NOTE(lbragstad): If at some point in the future we want to extend
        # our validators to include something specific we need to check for,
        # we can do it here. Nova's V3 API validators extend the validator to
        # include `self._validate_minimum` and `self._validate_maximum`. This
        # would be handy if we needed to check for something the jsonschema
        # didn't by default. See the Nova V3 validator for details on how this
        # is done.
        validators = {}
        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators)
        fc = jsonschema.FormatChecker()
        self.validator = validator_cls(schema, format_checker=fc) 
Example 58
Project: rptk   Author: wolcomm   File: conftest.py    Apache License 2.0 4 votes vote down vote up
def format_checker():
    """Get a custom format_checker instance."""
    format_checker = jsonschema.FormatChecker()

    def coerce_to_unicode(value):
        try:
            value = unicode(value)
        except (ValueError, NameError):
            pass
        return value

    @format_checker.checks("ipv4-prefix", raises=ValueError)
    def is_ipv4_prefix(instance):
        instance = coerce_to_unicode(instance)
        try:
            ipaddress.IPv4Network(instance, strict=True)
            return True
        except Exception:
            return False

    @format_checker.checks("ipv6-prefix", raises=ValueError)
    def is_ipv6_prefix(instance):
        instance = coerce_to_unicode(instance)
        try:
            ipaddress.IPv6Network(instance, strict=True)
            return True
        except Exception:
            return False

    @format_checker.checks("ipv4-address-prefix", raises=ValueError)
    def is_ipv4_address_prefix(instance):
        instance = coerce_to_unicode(instance)
        try:
            ipaddress.IPv4Network(instance, strict=False)
            return True
        except Exception:
            return False

    @format_checker.checks("ipv6-address-prefix", raises=ValueError)
    def is_ipv6_address_prefix(instance):
        instance = coerce_to_unicode(instance)
        try:
            ipaddress.IPv6Network(instance, strict=False)
            return True
        except Exception:
            return False

    return format_checker 
Example 59
Project: easyucs   Author: vesposito   File: manager.py    GNU General Public License v2.0 4 votes vote down vote up
def _validate_config_from_json(self, config_json=None):
        """
        Validates a config using the JSON schema definition
        :param config_json: JSON content containing config to be validated
        :return: True if config is valid, False otherwise
        """

        # Open JSON master schema for a UCS System config
        json_file = open("schema/ucs/ucsm/master.json")
        json_string = json_file.read()
        json_file.close()
        json_schema = json.loads(json_string)

        schema_path = 'file:///{0}/'.format(
            os.path.dirname(os.path.abspath("schema/ucs/ucsm/master.json")).replace("\\", "/"))
        resolver = jsonschema.RefResolver(schema_path, json_schema)
        format_checker = jsonschema.FormatChecker()

        try:
            jsonschema.validate(config_json, json_schema, resolver=resolver, format_checker=format_checker)
        except jsonschema.ValidationError as err:
            absolute_path = []
            for path in err.absolute_path:
                absolute_path.append(path)

            # TODO: Improve error logging by providing a simple explanation when JSON file is not valid
            self.logger(level="error", message="Invalid config JSON file in " + str(absolute_path))
            self.logger(level="error", message="Failed to validate config JSON file using schema: " + str(err.message))
            return False
        except jsonschema.SchemaError as err:
            self.logger(level="error", message="Failed to validate config JSON file due to schema error: " + str(err))
            return False

        # We now validate that the easyucs_version of the file is not greater than the running version of EasyUCS
        easyucs_version_from_file = config_json["easyucs"]["metadata"][0]["easyucs_version"]
        if packaging_version.parse(easyucs_version_from_file) > packaging_version.parse(__version__):
            self.logger(level="error",
                        message="Failed to validate config JSON file because it has been created using a more " +
                                "recent version of EasyUCS (" + easyucs_version_from_file + " > " + __version__ + ")")
            return False

        return True 
Example 60
Project: easyucs   Author: vesposito   File: manager.py    GNU General Public License v2.0 4 votes vote down vote up
def _validate_config_from_json(self, config_json=None):
        """
        Validates a config using the JSON schema definition
        :param config_json: JSON content containing config to be validated
        :return: True if config is valid, False otherwise
        """

        # Open JSON master schema for a UCS CIMC config
        json_file = open("schema/ucs/cimc/master.json")
        json_string = json_file.read()
        json_file.close()
        json_schema = json.loads(json_string)

        schema_path = 'file:///{0}/'.format(
            os.path.dirname(os.path.abspath("schema/ucs/cimc/master.json")).replace("\\", "/"))
        resolver = jsonschema.RefResolver(schema_path, json_schema)
        format_checker = jsonschema.FormatChecker()

        try:
            jsonschema.validate(config_json, json_schema, resolver=resolver, format_checker=format_checker)
        except jsonschema.ValidationError as err:
            absolute_path = []
            while True:
                try:
                    value = err.absolute_path.popleft()
                    if value != 0:
                        absolute_path.append(value)
                except IndexError:
                    break
            # TODO: Improve error logging by providing a simple explanation when JSON file is not valid
            self.logger(level="error", message="Invalid config JSON file in " + str(absolute_path))
            self.logger(level="error", message="Failed to validate config JSON file using schema: " + str(err.message))
            return False
        except jsonschema.SchemaError as err:
            self.logger(level="error", message="Failed to validate config JSON file due to schema error: " + str(err))
            return False

        # We now validate that the easyucs_version of the file is not greater than the running version of EasyUCS
        easyucs_version_from_file = config_json["easyucs"]["metadata"][0]["easyucs_version"]
        if packaging_version.parse(easyucs_version_from_file) > packaging_version.parse(__version__):
            self.logger(level="error",
                        message="Failed to validate config JSON file because it has been created using a more " +
                                "recent version of EasyUCS (" + easyucs_version_from_file + " > " + __version__ + ")")
            return False

        return True 
Example 61
Project: easyucs   Author: vesposito   File: manager.py    GNU General Public License v2.0 4 votes vote down vote up
def _validate_config_from_json(self, config_json=None):
        """
        Validates a config using the JSON schema definition
        :param config_json: JSON content containing config to be validated
        :return: True if config is valid, False otherwise
        """

        # Open JSON master schema for a UCS Central config
        json_file = open("schema/ucs/ucsc/master.json")
        json_string = json_file.read()
        json_file.close()
        json_schema = json.loads(json_string)

        schema_path = 'file:///{0}/'.format(
            os.path.dirname(os.path.abspath("schema/ucs/ucsc/master.json")).replace("\\", "/"))
        resolver = jsonschema.RefResolver(schema_path, json_schema)
        format_checker = jsonschema.FormatChecker()

        try:
            jsonschema.validate(config_json, json_schema, resolver=resolver, format_checker=format_checker)
        except jsonschema.ValidationError as err:
            absolute_path = []
            while True:
                try:
                    value = err.absolute_path.popleft()
                    if value != 0:
                        absolute_path.append(value)
                except IndexError:
                    break
            # TODO: Improve error logging by providing a simple explanation when JSON file is not valid
            self.logger(level="error", message="Invalid config JSON file in " + str(absolute_path))
            self.logger(level="error", message="Failed to validate config JSON file using schema: " + str(err.message))
            return False
        except jsonschema.SchemaError as err:
            self.logger(level="error", message="Failed to validate config JSON file due to schema error: " + str(err))
            return False

        # We now validate that the easyucs_version of the file is not greater than the running version of EasyUCS
        easyucs_version_from_file = config_json["easyucs"]["metadata"][0]["easyucs_version"]
        if packaging_version.parse(easyucs_version_from_file) > packaging_version.parse(__version__):
            self.logger(level="error",
                        message="Failed to validate config JSON file because it has been created using a more " +
                                "recent version of EasyUCS (" + easyucs_version_from_file + " > " + __version__ + ")")
            return False

        return True