Python jsonschema.Draft7Validator() Examples

The following are 11 code examples of jsonschema.Draft7Validator(). 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: __init__.py    From poetry with MIT License 8 votes vote down vote up
def validate_object(obj, schema_name):  # type: (dict, str) -> List[str]
    schema = os.path.join(SCHEMA_DIR, "{}.json".format(schema_name))

    if not os.path.exists(schema):
        raise ValueError("Schema {} does not exist.".format(schema_name))

    with open(schema, encoding="utf-8") as f:
        schema = json.loads(f.read())

    validator = jsonschema.Draft7Validator(schema)
    validation_errors = sorted(validator.iter_errors(obj), key=lambda e: e.path)

    errors = []

    for error in validation_errors:
        message = error.message
        if error.path:
            message = "[{}] {}".format(
                ".".join(str(x) for x in error.absolute_path), message
            )

        errors.append(message)

    return errors 
Example #2
Source File: daijin_configurator.py    From mikado with GNU Lesser General Public License v3.0 6 votes vote down vote up
def create_daijin_validator(simple=True):

    cname = resource_filename("Mikado.configuration", "configuration_blueprint.json")

    # We have to repeate twice the ending configuration (bug in jsonref?)
    baseuri = "file://" +  os.path.join(os.path.dirname(cname), os.path.basename(os.path.dirname(cname)))
    with io.TextIOWrapper(resource_stream("Mikado.configuration",
                                          "daijin_schema.json")) as blue:
        blue_print = jsonref.load(blue,
                                  jsonschema=True,
                                  base_uri=baseuri)

    # _substitute_conf(blue_print)
    validator = extend_with_default(jsonschema.Draft7Validator,
                                    simple=simple)
    validator = validator(blue_print)

    return validator 
Example #3
Source File: json_schema.py    From airflow with Apache License 2.0 5 votes vote down vote up
def load_dag_schema() -> Validator:
    """
    Load & Validate Json Schema for DAG
    """
    schema = load_dag_schema_dict()
    jsonschema.Draft7Validator.check_schema(schema)
    return jsonschema.Draft7Validator(schema) 
Example #4
Source File: loaders.py    From elastalert with Apache License 2.0 5 votes vote down vote up
def __init__(self, conf):
        # schema for rule yaml
        self.rule_schema = jsonschema.Draft7Validator(
            yaml.load(open(os.path.join(os.path.dirname(__file__), 'schema.yaml')), Loader=yaml.FullLoader))

        self.base_config = copy.deepcopy(conf) 
Example #5
Source File: validate.py    From micropy-cli with MIT License 5 votes vote down vote up
def __init__(self, schema_path):
        schema = self._load_json(schema_path)
        self.schema = Draft7Validator(schema) 
Example #6
Source File: test_item.py    From scrapy-jsonschema with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_get_validator(self):
        schema = {
            "$schema": JSON_SCHEMA_DRAFT_7,
            "title": "Item with Schema Draft",
        }

        draft7_validator = JsonSchemaItem._get_validator(schema)
        self.assertTrue(isinstance(draft7_validator, Draft7Validator))

        no_draft_chema = {"title": "Item without schema Draft"}
        default_validator = JsonSchemaItem._get_validator(no_draft_chema)
        self.assertTrue(isinstance(default_validator, Draft4Validator)) 
Example #7
Source File: base.py    From GenSON with MIT License 5 votes vote down vote up
def assertObjectValidates(self, obj):
        jsonschema.Draft7Validator(self.builder.to_schema()).validate(obj) 
Example #8
Source File: base.py    From GenSON with MIT License 5 votes vote down vote up
def assertObjectDoesNotValidate(self, obj):
        with self.assertRaises(jsonschema.exceptions.ValidationError):
            jsonschema.Draft7Validator(self.builder.to_schema()).validate(obj) 
Example #9
Source File: base.py    From GenSON with MIT License 5 votes vote down vote up
def _assertSchemaIsValid(self):
        jsonschema.Draft7Validator.check_schema(self.builder.to_schema()) 
Example #10
Source File: base.py    From GenSON with MIT License 5 votes vote down vote up
def _assertComponentObjectsValidate(self):
        compiled_schema = self.builder.to_schema()
        for obj in self._objects:
            jsonschema.Draft7Validator(compiled_schema).validate(obj) 
Example #11
Source File: configurator.py    From mikado with GNU Lesser General Public License v3.0 5 votes vote down vote up
def create_validator(simple=False):

    """Method to create a validator class (see extend_with_default).
    The simple keyword (boolean) is used to determine whether to keep
    only SimpleComment or full Comments from the schema.

    :type simple: bool

    :return validator
    :rtype: jsonschema.Draft7Validator
    """

    validator = extend_with_default(jsonschema.Draft7Validator,
                                    simple=simple)

    resolver = jsonschema.RefResolver("file:///{}".format(os.path.abspath(
        os.path.dirname(pkg_resources.resource_filename("Mikado.configuration", os.path.basename(__file__)))
    )), None)

    with io.TextIOWrapper(resource_stream("Mikado.configuration",
                                          "configuration_blueprint.json")) as blue:
        blue_print = json.loads(blue.read())

    validator = validator(blue_print, resolver=resolver)

    return validator