Python jsonschema.FormatChecker() Examples

The following are 30 code examples of jsonschema.FormatChecker(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jsonschema , or try the search function .
Example #1
Source File: validators.py    From ceph-lcm with Apache License 2.0 6 votes vote down vote up
def require_schema(schema):
    """This decorator verifies that request JSON matches given JSONSchema.

    http://json-schema.org
    """

    validator = jsonschema.Draft4Validator(
        schema,
        format_checker=jsonschema.FormatChecker()
    )

    def outer_decorator(func):
        @functools.wraps(func)
        def inner_decorator(self, *args, **kwargs):
            errors = validator.iter_errors(self.request_json)
            errors = [err.message for err in errors]

            if errors:
                LOG.warning("Cannot validate request: %s", errors)
                raise exceptions.InvalidJSONError(errors)

            return func(self, *args, **kwargs)

        return inner_decorator
    return outer_decorator 
Example #2
Source File: json_faker.py    From contrail-server-manager with Apache License 2.0 6 votes vote down vote up
def validate_JSON(data,combined_schema):
    for keys in combined_schema['properties']:
        if "$ref" in (combined_schema['properties'])[keys]:
            refUrl= ((combined_schema['properties'])[keys])['$ref']
            references_file, section = refUrl.split('#')

    if not os.path.isfile(references_file):
        print "References file does not exists"
        sys.exit()

    schema_dir = os.path.dirname(os.path.realpath(references_file))
    resolver = RefResolver("file://{}/".format(schema_dir), None)

    try:
        validate(data,combined_schema,format_checker=FormatChecker(), resolver=resolver)
        print "JSON is valid with the schema"
        return True
    except exceptions.ValidationError as error:
        print(error.message)
        return False

#Merging schemas and putting key translations 
Example #3
Source File: check_tap.py    From singer-tools with 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 #4
Source File: validator.py    From conp-dataset with MIT License 6 votes vote down vote up
def validate_json(json_obj):
    with open(SCHEMA_PATH) as s:
        json_schema = json.load(s)
    # first validate schema file
    v = jsonschema.Draft4Validator(json_schema)
    # now validate json file
    try:
        jsonschema.validate(json_obj, json_schema, format_checker=jsonschema.FormatChecker())
        logger.info('The file is valid. Validation passed.')
        return True
    except jsonschema.exceptions.ValidationError:
        errors = [e for e in v.iter_errors((json_obj))]
        logger.info(f"The file is not valid. Total errors: {len(errors)}")
        for i, error in enumerate(errors, 1):
            logger.error(f"{i} Validation error in {'.'.join(str(v) for v in error.path)}: {error.message}")
        logger.info('Validation failed.')
        return False 
Example #5
Source File: validators.py    From cadasta-platform with 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 #6
Source File: schema.py    From arche with 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 #7
Source File: replica_schedules.py    From coriolis with 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 #8
Source File: Validator.py    From EDDN with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def validate(self, json_object):
        results = ValidationResults()

        if "$schemaRef" not in json_object:
            results.add(ValidationSeverity.FATAL, JsonValidationException("No $schemaRef found, unable to validate."))
            return results

        schemaRef = json_object["$schemaRef"]
        if schemaRef not in self.schemas.keys():
            #  We don't want to go out to the Internet and retrieve unknown schemas.
            results.add(ValidationSeverity.FATAL, JsonValidationException("Schema " + schemaRef + " is unknown, unable to validate."))
            return results

        schema = self.schemas[schemaRef]
        try:
            jsValidate(json_object, schema, format_checker=FormatChecker())
        except ValidationError as e:
            results.add(ValidationSeverity.ERROR, e)

        return results 
Example #9
Source File: replica_schedules.py    From coriolis with 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 #10
Source File: cluster.py    From flocker with Apache License 2.0 6 votes vote down vote up
def validate_host_mapping(host_mapping):
    """
    Validate a provided host mapping.

    :param dict host_mapping: The parsed JSON host mapping from the
    environment variable ``FLOCKER_ACCEPTANCE_HOSTNAME_TO_PUBLIC_ADDRESS``.
    :raises: jsonschema.ValidationError if the configuration is invalid.
    """
    schema = {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "object",
        "additionalProperties": "true",
    }

    v = Draft4Validator(schema, format_checker=FormatChecker())
    v.validate(host_mapping) 
Example #11
Source File: cmd.py    From contrail-docker with Apache License 2.0 6 votes vote down vote up
def validate(self, data=None):
        if not data:
            data = self.config_dict
        schema_dir = "/usr/share/contrailctl/schema/"
        schema_path="{}/{}.json".format(schema_dir, self.component)
        resolver = RefResolver("file://{}/".format(schema_dir), None)
        try:
            schema=open(schema_path,'r').read()
        except IOError as error:
            print("Schema file is missing - {}".format(schema_path))
            return True
        try:
            validate(data, json.loads(schema), format_checker=FormatChecker(), resolver=resolver)
            return True
        except exceptions.ValidationError as error:
            print(error.message)
            return False 
Example #12
Source File: json_schema.py    From pan-fca with Apache License 2.0 5 votes vote down vote up
def validate_schema(schema, data):
    try:
        validate(data, schema, format_checker=FormatChecker())
    except ValidationError as e:
        return (False, "ValidationError: {0}".format(e.message))
    except SchemaError as e:
        return (False, "SchemaError: {0}".format(e.message))
    except Exception as e:
        return (False, "UnknownError: {0}".format(e.message))
    return (True, '') 
Example #13
Source File: validators.py    From karbor with 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 #14
Source File: validators.py    From python-jsonschema-objects with MIT License 5 votes vote down vote up
def format(param, value, _):
        if not FormatChecker().conforms(value, param):
            raise ValidationError(
                "'{0}' is not formatted as a {1}".format(value, param)
            ) 
Example #15
Source File: test_integration.py    From arche with 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 #16
Source File: validators.py    From core with MIT License 5 votes vote down vote up
def _validate_json(json_data, schema, resolver):
    jsonschema.validate(json_data, schema, resolver=resolver, format_checker=jsonschema.FormatChecker()) 
Example #17
Source File: validators.py    From tacker with Apache License 2.0 5 votes vote down vote up
def __init__(self, schema):
        validator_cls = jsonschema.validators.extend(self.validator_org,
                                                     validators={})
        format_checker = FormatChecker()
        self.validator = validator_cls(schema, format_checker=format_checker) 
Example #18
Source File: GenericTest.py    From nmos-testing with Apache License 2.0 5 votes vote down vote up
def validate_schema(self, payload, schema):
        """
        Validate the payload under the given schema.
        Raises an exception if the payload (or schema itself) is invalid
        """
        checker = jsonschema.FormatChecker(["ipv4", "ipv6", "uri"])
        jsonschema.validate(payload, schema, format_checker=checker) 
Example #19
Source File: base.py    From flasgger with MIT License 5 votes vote down vote up
def __init__(
            self, app=None, config=None, sanitizer=None, template=None,
            template_file=None, decorators=None, validation_function=None,
            validation_error_handler=None, parse=False, format_checker=None,
            merge=False
    ):
        self._configured = False
        self.endpoints = []
        self.definition_models = []  # not in app, so track here
        self.sanitizer = sanitizer or BR_SANITIZER

        self._init_config(config, merge)

        self.template = template
        self.template_file = template_file
        self.decorators = decorators
        self.format_checker = format_checker or jsonschema.FormatChecker()

        def default_validation_function(data, schema):
            return jsonschema.validate(
                data, schema, format_checker=self.format_checker,
            )

        def default_error_handler(e, _, __):
            return abort(400, e.message)

        self.validation_function = validation_function\
            or default_validation_function

        self.validation_error_handler = validation_error_handler\
            or default_error_handler
        self.apispecs = {}  # cached apispecs
        self.parse = parse
        if app:
            self.init_app(app) 
Example #20
Source File: singer_stream.py    From target-postgres with 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)

        # The validator can handle _many_ more things than our simplified schema, and is, in general handled by third party code
        self.validator = Draft4Validator(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 #21
Source File: mapping.py    From alchemyjsonschema with MIT License 5 votes vote down vote up
def __init__(
        self,
        validator_class,
        schema_factory,
        module,
        resolver=None,
        format_checker=None,
    ):
        self.schema_factory = schema_factory
        self.validator_class = validator_class
        self.resolver = resolver
        self.format_checker = format_checker or FormatChecker()
        self.module = module 
Example #22
Source File: validators.py    From masakari with 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 #23
Source File: validate_config_schema.py    From power-up with 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 #24
Source File: schema.py    From mailur with GNU General Public License v3.0 5 votes vote down vote up
def validate(value, schema):
    """Collect all errors during validation"""
    validator = Draft4WithDefaults(schema, format_checker=FormatChecker())
    errs = sorted(validator.iter_errors(value), key=lambda e: e.path)
    errs = ['%s: %s' % (list(e.schema_path), e.message) for e in errs]
    if errs:
        raise Error(errs, schema)
    return value 
Example #25
Source File: validate.py    From paasta with 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:
        print(f"{SCHEMA_ERROR}: {file_type}, error: {e!r}")
        return

    if schema is None:
        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:
        print(f"{SCHEMA_INVALID}: {file_path}")

        errors = validator.iter_errors(config_file_object)
        print("  Validation Message: %s" % exceptions.best_match(errors).message)
    except Exception as e:
        print(f"{SCHEMA_ERROR}: {file_type}, error: {e!r}")
        return
    else:
        print(f"{SCHEMA_VALID}: {basename}")
        return True 
Example #26
Source File: validation.py    From cjio with 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 #27
Source File: __init__.py    From notifications-api with 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 #28
Source File: __init__.py    From target-stitch with 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 #29
Source File: test_fictional_example.py    From sample-data with 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)

    for error in validator(schema, format_checker=FormatChecker()).iter_errors(data):
        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 #30
Source File: client.py    From pantalaimon with 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)