예제 #1
0
def get_validators(url: str) -> MetadataValidatorSet:
    '''
    Given a url pointing to a config file, initialize any metadata validators present
    in the configuration.

    :param url: The URL for a config file for the metadata validators.
    :returns: A set of metadata validators.
    '''
    # TODO VALIDATOR make validator CLI
    try:
        with _urllib.request.urlopen(url) as res:
            cfg = _yaml.safe_load(res)
    except _URLError as e:
        raise ValueError(
            f'Failed to open validator configuration file at {url}: {str(e.reason)}'
        ) from e
    except _ParserError as e:
        raise ValueError(
            f'Failed to open validator configuration file at {url}: {str(e)}'
        ) from e
    _validate(instance=cfg, schema=_META_VAL_JSONSCHEMA)

    mvals = _get_validators(
        cfg.get('validators', {}), 'Metadata',
        lambda k, v, m: _MetadataValidator(k, v, metadata=m))
    mvals.extend(
        _get_validators(
            cfg.get('prefix_validators', {}), 'Prefix metadata', lambda k, v,
            m: _MetadataValidator(k, prefix_validators=v, metadata=m)))
    return MetadataValidatorSet(mvals)
예제 #2
0
def model_validate(service=None, role=None):
    """
    Validates pillar metadata by schema for given service and role. If
    no service and role is specified, method will validate all defined
    services.

    CLI Example:
    .. code-block:: bash
        salt-run modelschema.model_validate keystone server

    """
    schema = schema_get(service, role)['{}-{}'.format(service, role)]
    model = __salt__['pillar.get']('{}:{}'.format(service, role))
    try:
        _validate(model, schema)
        data = 'Model is valid'
    except SchemaError as exc:
        LOG.error("SchemaError:{}".format(exc))
        raise Exception("SchemaError")
    except ValidationError as exc:
        LOG.error("ValidationError:{}\nInstance:{}\n"
                  "Schema title:{}\n"
                  "SchemaPath:{}".format(
                      exc.message, exc.instance,
                      exc.schema.get("title", "Schema title not set!"),
                      exc.schema_path))
        raise Exception("ValidationError")
    return {'{}-{}'.format(service, role): data}
 def decorated(*args, **kwargs):
     jschema = current_app.extensions.get('jsonschema', None)
     if jschema is None:
         raise RuntimeError('Flask-JsonSchema was not properly initialized for the '
                            'current application: %s' % current_app)
     _validate(request.json, jschema.get_schema(path))
     return fn(*args, **kwargs)
예제 #4
0
    def validate(self, schema=None, exclude_case=False):
        """
        Validate the object against a schema

        :param dict schema: a schema object to use to validate, it overrides the one
            that has been provided at object construction stage
        :param bool exclude_case: whether to exclude validated objects
            from the error. Useful when used with large configs
        """
        try:
            _validate(self.to_dict(expand=True), schema
                      or getattr(self[IK], SCHEMA_KEY))
        except ValidationError as e:
            _LOGGER.error(
                f"{self.__class__.__name__} object did not pass schema validation"
            )
            if getattr(self[IK], FILEPATH_KEY, None) is not None:
                # need to unlock locked files in case of validation error so that no
                # locks are left in place
                self.make_readonly()
            if not exclude_case:
                raise
            raise ValidationError(
                f"{self.__class__.__name__} object did not pass schema validation: "
                f"{e.message}")
        _LOGGER.debug("Validated successfully")
예제 #5
0
def validate(instance, schema, cls=None, *args, **kwargs):
    """
    Calls jsonschema.validate() with the arguments.
    """
    format_checker = FormatChecker()
    _validate(instance,
              schema,
              cls,
              *args,
              format_checker=format_checker,
              **kwargs)
예제 #6
0
 def decorated(*args, **kwargs):
     jschema = current_app.extensions.get('jsonschema', None)
     if jschema is None:
         raise RuntimeError('Flask-JsonSchema was not properly initialized for the '
                            'current application: %s' % current_app)
     if request.method == 'GET':
         data = request.args.to_dict()
     else:
         data = request.get_json(force=True)
     _validate(data, jschema.get_schema(path))
     return fn(*args, **kwargs)
        def decorated(*args, **kwargs):
            methods = current_app.config.get('JSONSCHEMER_METHODS', _methods)
            jsonschemer = current_app.extensions.get('jsonschemer', None)
            if request.method not in methods:
                return fn(*args, **kwargs)
            elif jsonschemer is None:
                raise RuntimeError('Flask-JsonSchemer was not properly '
                                   'initialized for the current '
                                   'application: %s' % current_app)

            data = request.get_json(silent=True)
            schema = jsonschemer.get_schema(path)
            _validate(data, schema, format_checker=jsonschemer.format_checker)
            return fn(*args, **kwargs)
예제 #8
0
        def decorated(*args, **kwargs):
            methods = current_app.config.get('JSONSCHEMER_METHODS', _methods)
            jsonschemer = current_app.extensions.get('jsonschemer', None)
            if request.method not in methods:
                return fn(*args, **kwargs)
            elif jsonschemer is None:
                raise RuntimeError('Flask-JsonSchemer was not properly '
                                   'initialized for the current '
                                   'application: %s' % current_app)

            data = request.get_json(silent=True)
            schema = jsonschemer.get_schema(path)
            _validate(data, schema, format_checker=jsonschemer.format_checker)
            return fn(*args, **kwargs)
예제 #9
0
def validate(instance):
    """Validate that the config is valid

    """
    schema = {
        "$id": "https://example.com/.schema.json",
        "$schema": "http://json-schema.org/draft-07/schema#",
        "title": "Projectionist",
        "description": "A configuration for projections.",
        "type": "object",
        "patternProperties": {
            "(\\\\?([^\\/]*[\\/])*)([^\\/]+)$": {
                "type": "object",
                "properties": {
                    "alternate": {
                        "type": "string"
                    },
                    "type": {
                        "type": "string"
                    },
                    "tasks": {
                        "oneOf": [
                            {
                                "type": "object",
                                "properties": {
                                    "parallel": {
                                        "type": "array"
                                    }
                                },
                                "additionalProperties": False,
                            },
                            {
                                "type": "object",
                                "properties": {
                                    "serial": {
                                        "type": "array"
                                    }
                                },
                                "additionalProperties": False,
                            },
                        ]
                    },
                },
                "required": ["type"],
            }
        },
        "additionalProperties": False,
    }
    _validate(instance=instance, schema=schema)
예제 #10
0
def _generate_schema_from_example_and_description(input, description):
    '''
    With an example input, a schema is automatically generated that conforms
    to the example in json-schema.org. The description given by the users
    is then added to the schema.
    '''
    s = _genson.Schema()
    s.add_object(input)
    input_schema = s.to_dict()

    if description is not None:
        if 'properties' in input_schema:
            # Case for input = {'x':1}, input_description='not a dict'
            if not isinstance(description, dict):
                msg = f'{input} and {description} do not match'
                logger.error(msg)
                raise Exception(msg)

            for key in description:
                # Case for input = {'x':1},
                # input_description={'x':'x value', 'y':'y value'}
                if key not in input_schema['properties']:
                    msg = f'{key} not found in {input}'
                    logger.error(msg)
                    raise Exception(msg)
                else:
                    input_schema['properties'][key][
                        'description'] = description[key]
        else:
            if isinstance(description, dict):
                raise Exception(f'{input} and {description} do not match')
            else:
                input_schema['description'] = description

    try:
        # This should not fail unless there are bugs with either genson or
        # jsonschema.
        _validate(input, input_schema)
    except Exception as e:
        logger.error(f'Internal error validating schema: {str(e)}')
        raise

    return input_schema
def data_validate(model, schema):
    """
    Validates model by given schema.

    CLI Example:
    .. code-block:: bash
        salt-run modelschema.data_validate {'a': 'b'} {'a': 'b'}
    """
    try:
        _validate(model, schema)
        data = 'Model is valid'
    except SchemaError as exc:
        LOG.error("SchemaError:{}".format(exc))
        data = str(exc)
    except ValidationError as exc:
        LOG.error("ValidationError:{}\nInstance:{}\n"
                  "SchemaPath:{}".format(exc.message, exc.instance,
                                         exc.schema_path))
        raise Exception("ValidationError")
    return data
예제 #12
0
파일: schema.py 프로젝트: YukinaXSer/TabPy
def _generate_schema_from_example_and_description(input, description):
    ''' With an example input, a schema is automatically generated that conforms
        to the example in json-schema.org. The description given by the users
        is then added to the schema.
    '''
    s = _genson.Schema()
    s.add_object(input)
    input_schema = s.to_dict()

    if description is not None:
        if 'properties' in input_schema:
            # Case for input = {'x':1}, input_description='not a dict'
            if not isinstance(description, dict):
                raise Exception('%s and %s do not match', input, description)
                _logger.error('%s and %s do not match', input, description)

            for key in description:
                # Case for input = {'x':1}, input_description={'x':'x value', 'y':'y value'}
                if key not in input_schema['properties']:
                    raise Exception('%s not found in %s', key, input)
                    _logger.error('%s not found in %s', key, input)
                else:
                    input_schema['properties'][key]['description'] = description[key]
        else:
            if isinstance(description, dict):
                raise Exception('%s and %s do not match', input, description)
            else:
                input_schema['description'] = description

    try:
        # This should not fail unless there are bugs with either genson or jsonschema.
        _validate(input, input_schema)
    except Exception as e:
        _logger.error('Internal error validating schema.')
        raise

    return input_schema
예제 #13
0
def validate(requirements):
    _validate(requirements, SCHEMA)
예제 #14
0
def validate(json):
    """Wrap jsonchema.validate for convenience."""
    return _validate(json,
                     json['$schema'],
                     resolver=json_resolver(json['$schema']),
                     types={'array': (list, tuple)})
예제 #15
0
 def validate(self, obj):
     _validate(obj, self.schema, format_checker=fc)
예제 #16
0
def validate_input(input, schema):
    try:
        _validate(input, schema)
    except _JSONValidationError as err:
        raise ValidationError(description=str(err.__cause__)) from err