Ejemplo n.º 1
0
def load_validator(name):
    """ Load the JSON Schema Draft 4 validator with the given name from the
    local schema directory. """
    with open(os.path.join(SCHEMA_PATH, name)) as fh:
        schema = json.load(fh)
    Draft4Validator.check_schema(schema)
    return Draft4Validator(schema, format_checker=checker)
Ejemplo n.º 2
0
 def test_select_all(self, testapi):
     """
     Select all link relations and check them for valid JSON schema.
     """
     for rel_id in testapi.get(url_for('v1.LinkRelationsView:index')).json.keys():
         resp = testapi.get(url_for('v1.LinkRelationsView:get',id=rel_id))
         Draft4Validator.check_schema(resp.json).should.be(None)
Ejemplo n.º 3
0
    def serialize(self):
        """Serialize the schema to a pure Python data structure.

        After serializing the schema once, it's not possible to mutate
        self._schema any more, since these changes would not be reflected in
        the serialized output.
        """
        # Order keys before serializing.
        # This is to get a stable sort order when dumping schemas, and a
        # convenient API at the same time (no need to pass in OrderedDicts
        # all the time). This keeps calling code more readable.
        self._schema = order_dict(self._schema, SCHEMA_KEYS_ORDER)
        if 'properties' in self._schema:
            for prop_name, prop_def in self._schema['properties'].items():
                self._schema['properties'][prop_name] = order_dict(
                    prop_def, PROPERTY_KEYS_ORDER)

        schema = deepcopy(self._schema)
        Draft4Validator.check_schema(schema)

        # Prevent access to self._schema after serialization in order to avoid
        # gotchas where mutations to self._schema don't take effect any more
        del self._schema

        return schema
Ejemplo n.º 4
0
def validate(data, schema=None):
    if schema is None:
        schema = generate()
        Validator.check_schema(schema)
    validator = Validator(schema)

    errors = list(validator.iter_errors(data))
    if not errors:
        counter = Counter([p['name'] for p in data.get('policies')])
        dupes = []
        for k, v in counter.items():
            if v > 1:
                dupes.append(k)
        if dupes:
            return [ValueError(
                "Only one policy with a given name allowed, duplicates: %s" % (
                    ", ".join(dupes)))]
        return []
    try:
        resp = specific_error(errors[0])
        name = isinstance(errors[0].instance, dict) and errors[0].instance.get('name', 'unknown') or 'unknown'
        return [resp, name]
    except Exception:
        logging.exception(
            "specific_error failed, traceback, followed by fallback")

    return filter(None, [
        errors[0],
        best_match(validator.iter_errors(data)),
    ])
Ejemplo n.º 5
0
    def __init__(self, json_data, strict=False, live_schema=None):
        self.live_schema = live_schema
        if not hasattr(json_data, '__getitem__'):
            raise TypeError('json_data must be a dict.')
        if (not self.schema) and (live_schema is None):
            raise NotImplementedError('schema not implemented!')
        if live_schema is not None:
            if not self.schema:
                self.schema = live_schema
            else:
                self.schema['properties'].update(live_schema['properties'])
                if "required" in self.schema and "required" in live_schema:
                    self.schema['required'] = list(
                        set(self.schema['required']) |
                        set(live_schema["required"])
                    )

        Draft4Validator.check_schema(self.schema)

        self.data = {}
        if not strict:
            self._filter_data(json_data, self.schema['properties'], self.data)
        else:
            self.data = json_data
        self.validator = Draft4Validator(self.schema)
        self.errors = None
Ejemplo n.º 6
0
 def validate(self):
     self.log.info("Checking schemas for validity")
     for application in self.applications.values():
         self.log.info("+ " + application.slug)
         for collection in application.collections:
             self.log.info('--- ' + collection.slug)
             Draft4Validator.check_schema(collection.schema)
Ejemplo n.º 7
0
    def __init__(self, schema=DEFAULT_LTM_SCHEMA):
        """Choose schema and initialize extended Draft4Validator.

        Raises:
            F5CcclSchemaError: Failed to read or validate the CCCL
            API schema file.
        """

        try:
            self.schema = read_yaml_or_json(schema)
        except json.JSONDecodeError as error:
            LOGGER.error("%s", error)
            raise cccl_exc.F5CcclSchemaError(
                'CCCL API schema could not be decoded.')
        except IOError as error:
            LOGGER.error("%s", error)
            raise cccl_exc.F5CcclSchemaError(
                'CCCL API schema could not be read.')

        try:
            Draft4Validator.check_schema(self.schema)
            self.validate_properties = Draft4Validator.VALIDATORS["properties"]
            validator_with_defaults = validators.extend(
                Draft4Validator,
                {"properties": self.__set_defaults})
            self.validator = validator_with_defaults(self.schema)
        except jsonschema.SchemaError as error:
            LOGGER.error("%s", error)
            raise cccl_exc.F5CcclSchemaError("Invalid API schema")
Ejemplo n.º 8
0
def validate(data, schema=None):
    if schema is None:
        schema = generate()
        Validator.check_schema(schema)

    validator = Validator(schema)
    errors = list(validator.iter_errors(data))
    if not errors:
        return check_unique(data) or []
    try:
        resp = policy_error_scope(specific_error(errors[0]), data)
        name = isinstance(
            errors[0].instance,
            dict) and errors[0].instance.get(
            'name',
            'unknown') or 'unknown'
        return [resp, name]
    except Exception:
        logging.exception(
            "specific_error failed, traceback, followed by fallback")

    return list(filter(None, [
        errors[0],
        best_match(validator.iter_errors(data)),
    ]))
def test_schemas_are_valid():
    root_dir = os.path.join(
        'inspirehep', 'modules', 'records', 'jsonschemas', 'records')
    for schemas_dir, _, schemas in os.walk(root_dir):
        schemas_path = os.path.sep.join(schemas_dir.split(os.path.sep)[1:])
        for schema in schemas:
            schema_path = os.path.join(schemas_path, schema)
            Draft4Validator.check_schema(fetch_schema(schema_path))
Ejemplo n.º 10
0
 def _validate(self):
     # Draft4Validator accepts empty JSON, but we don't want to accept it.
     if not self.json:
         raise ValueError('Schema is invalid.')
     try:
         Draft4Validator.check_schema(self.json)
     except (SchemaError, ValidationError):
         raise ValueError('Schema is invalid.')
Ejemplo n.º 11
0
    def test_put_schema(self):
        """The schema defined for PUT on base end point is valid"""
        try:
            Draft4Validator.check_schema(snapshot.SnapshotView.PUT_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 12
0
def test_schemas_are_valid():
    root_dir = os.path.join('inspirehep', 'modules', 'records', 'jsonschemas',
                            'records')
    for schemas_dir, _, schemas in os.walk(root_dir):
        schemas_path = os.path.sep.join(schemas_dir.split(os.path.sep)[1:])
        for schema in schemas:
            if schema.endswith('.json'):
                schema_path = os.path.join(schemas_path, schema)
                Draft4Validator.check_schema(fetch_schema(schema_path))
Ejemplo n.º 13
0
    def test_get_schema(self):
        """The schema defined for GET is valid"""
        try:
            Draft4Validator.check_schema(insightiq.InsightIQView.GET_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 14
0
    def test_post_schema(self):
        """The schema defined for POST on is valid"""
        try:
            Draft4Validator.check_schema(claritynow.ClarityNowView.POST_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 15
0
    def test_get_schema(self):
        """The schema defined for GET on is valid"""
        try:
            Draft4Validator.check_schema(portmap.PortMapView.GET_ARGS_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 16
0
    def test_get_args(self):
        """The schema defined for the query params on GET is valid"""
        try:
            Draft4Validator.check_schema(addr.AddrView.GET_ARGS_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
 def validate_draft4(self, schema):
     self.assertEqual(schema['$schema'],
                      'http://json-schema.org/draft-04/schema#')
     try:
         Draft4Validator.check_schema(schema)
     except SchemaError as ex:
         self.fail(
             "Json-schema for root is not valid. Failed for {0:s}".format(
                 ex))
Ejemplo n.º 18
0
    def test_v1_response_schema(self):
        """The default shape we return for every JSON response has a valid schema"""
        try:
            Draft4Validator.check_schema(flask_common.v1_RESPONSE)
            response_schema_ok = True
        except ValidationError:
            response_schema_ok = False

        self.assertTrue(response_schema_ok)
Ejemplo n.º 19
0
    def test_get_schema(self):
        """The schema defined for GET on is valid"""
        try:
            Draft4Validator.check_schema(jumpbox.JumpboxView.GET_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 20
0
    def test_token_schema(self):
        """The schema defined for DELETE on /api/1/inf/vlan/token is valid"""
        try:
            Draft4Validator.check_schema(vlan.VlanView.TASK_ARGS)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 21
0
    def setUpClass(cls):
        #warnings.filterwarnings("ignore", category=ResourceWarning, message="unclosed.*<ssl.SSLSocket.*>")

        network = httplib2.Http()
        response, content = network.request('https://developer.mpds.io/mpds.schema.json')
        assert response.status == 200

        cls.schema = json.loads(content)
        Draft4Validator.check_schema(cls.schema)
Ejemplo n.º 22
0
    def test_post_schema(self):
        """The schema defined for POST on /api/1/link is valid"""
        try:
            Draft4Validator.check_schema(link.LinkView.POST_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 23
0
    def test_config_schema(self):
        """The schema defined for POST on /config is valid"""
        try:
            Draft4Validator.check_schema(onefs.OneFSView.CONFIG_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 24
0
def api(require_login=True, schema=None):
    """
    Decorator for API requests.
    Handles auth and adds the username as the first argument.
    """
    if schema is not None:
        Draft4Validator.check_schema(schema)
        validator = Draft4Validator(schema)
    else:
        validator = None

    def innerdec(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            g.user = PUBLIC
            user_agent_str = request.headers.get('user-agent', '')
            g.user_agent = httpagentparser.detect(user_agent_str,
                                                  fill_none=True)

            if validator is not None:
                try:
                    validator.validate(request.get_json(cache=True))
                except ValidationError as ex:
                    raise ApiException(requests.codes.bad_request, ex.message)

            auth = request.headers.get(AUTHORIZATION_HEADER)
            g.auth = auth
            if auth is None:
                if require_login:
                    raise ApiException(requests.codes.unauthorized,
                                       "Not logged in")
            else:
                headers = {AUTHORIZATION_HEADER: auth}
                try:
                    resp = requests.get(OAUTH_USER_API, headers=headers)
                    resp.raise_for_status()

                    data = resp.json()
                    # TODO(dima): Generalize this.
                    g.user = data.get('current_user', data.get('login'))
                    assert g.user
                    g.email = data['email']
                except requests.HTTPError as ex:
                    if resp.status_code == requests.codes.unauthorized:
                        raise ApiException(requests.codes.unauthorized,
                                           "Invalid credentials")
                    else:
                        raise ApiException(requests.codes.server_error,
                                           "Server error")
                except (ConnectionError, requests.RequestException) as ex:
                    raise ApiException(requests.codes.server_error,
                                       "Server error")
            return f(g.user, *args, **kwargs)

        return wrapper

    return innerdec
Ejemplo n.º 25
0
    def test_delete_schema(self):
        """The schema defined for DELETE on /api/1/inf/vlan is valid"""
        try:
            Draft4Validator.check_schema(vlan.VlanView.DELETE_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 26
0
    def test_delete_schema(self):
        """The schema defined for DELETE on /api/1/inf/inventory is a valid schema"""
        try:
            Draft4Validator.check_schema(inventory.InventoryView.DELETE_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 27
0
def validate(schema_filename, data):
    with open(schema_filename) as f:
        schema = json.load(f)  # cteme JSON Schema primo ze souboru
    Validator.check_schema(schema)  # zkontroluje schema nebo vyhodi vyjimku

    base_uri = 'file://' + os.path.dirname(schema_filename) + '/'
    resolver = RefResolver(base_uri, schema)
    validator = Validator(schema, resolver=resolver)
    return validator.iter_errors(data)  # vraci chyby jednu po druhe
Ejemplo n.º 28
0
def get_error(schema_text):
    try:
        schema_object = json.loads(schema_text)
        Draft4Validator.check_schema(schema_object)
    except SchemaError as schema_error:
        return schema_error
    except ValueError as value_error:
        return value_error
    return None
Ejemplo n.º 29
0
    def test_post_schema(self):
        """The schema defined for POST on is valid"""
        try:
            Draft4Validator.check_schema(winserver.WinServerView.POST_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
    def __testValidateOpts(self, updateId, schemaLevel="full"):
        schemaNames = ["repository_holdings"]
        collectionNames = {
            "repository_holdings": [
                "repository_holdings_update_entry",
                "repository_holdings_current_entry",
                "repository_holdings_unreleased_entry",
                "repository_holdings_removed_entry",
                "repository_holdings_combined_entry",
            ],
            "entity_sequence_clusters":
            ["cluster_members", "cluster_provenance", "entity_members"],
        }
        #
        eCount = 0
        for schemaName in schemaNames:
            for collectionName in collectionNames[schemaName]:
                _ = self.__schP.makeSchemaDef(schemaName,
                                              dataTyping="ANY",
                                              saveSchema=True)
                cD = self.__schP.makeSchema(schemaName,
                                            collectionName,
                                            encodingType="JSON",
                                            level=schemaLevel,
                                            saveSchema=True)
                dL = self.__getRepositoryHoldingsDocuments(
                    schemaName, collectionName, updateId)
                if self.__export:
                    savePath = os.path.join(HERE, "test-output",
                                            collectionName + ".json")
                    self.__mU.doExport(savePath, dL, fmt="json", indent=3)
                # Raises exceptions for schema compliance.
                Draft4Validator.check_schema(cD)
                #
                valInfo = Draft4Validator(cD, format_checker=FormatChecker())
                for ii, dD in enumerate(dL):
                    logger.debug("Schema %s collection %s document %d",
                                 schemaName, collectionName, ii)
                    try:
                        cCount = 0
                        for error in sorted(valInfo.iter_errors(dD), key=str):
                            logger.info(
                                "schema %s collection %s path %s error: %s",
                                schemaName, collectionName, error.path,
                                error.message)
                            logger.info(">>>")
                            logger.info(">>> failing object is %r", dD)
                            logger.info(">>>")
                            eCount += 1
                            cCount += 1
                        #
                        logger.debug("schema %s collection %s count %d",
                                     schemaName, collectionName, cCount)
                    except Exception as e:
                        logger.exception("Validation error %s", str(e))

        return eCount
Ejemplo n.º 31
0
    def test_post_schema(self):
        """The schema we've defined for POST on /api/2/auth/token is valid"""
        try:
            Draft4Validator.check_schema(token.TokenView2.POST_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 32
0
def test_schema_validity():
    for name in ("schema_base.json",
                 "schema_data.json",
                 "schema_node.json",
                 "schema_prov_exe.json",
                 "schema_workflow.json"):
        with open(os.path.join(sch_pth, name), 'r') as f:
            schema = json.load(f)
            Draft4Validator.check_schema(schema)
Ejemplo n.º 33
0
def json_schema_validator(value):
    """
    raises ValidationError if value is not a valid json schema
    """
    try:
        Draft4Validator.check_schema(value)
    except SchemaError as e:
        raise ValidationError(_('Schema is invalid: %(msg)s'),
                              params={"msg": str(e.message)})
Ejemplo n.º 34
0
def assertContainsRequiredFields(schema_filename, response):
  schema_dir = os.path.abspath(os.path.join(os.path.dirname(
      inspect.getfile(inspect.currentframe())), '..', '..', 'schema'))
  schema_filename = os.path.join(schema_dir, schema_filename)
  schema = json.load(open(schema_filename))
  Draft4Validator.check_schema(schema)
  resolver = RefResolver(referrer=schema, base_uri='file://' + schema_dir + '/')
  # Raises ValidationError when incorrect response
  validate(response, schema, resolver=resolver)
Ejemplo n.º 35
0
    def test_iamges_schema(self):
        """The schema defined for GET on /images is valid"""
        try:
            Draft4Validator.check_schema(centos.CentOSView.IMAGES_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 36
0
def check_schema(schema):
    try:
        Draft4Validator.check_schema(schema)
    except SchemaError as e:
        pytest.fail(
            'invalid schema in test, should only happen while developing'
            ' tests! %s' % e)
    else:
        return schema
Ejemplo n.º 37
0
    def test_delete_schema(self):
        """The schema defined for DELETE on is valid"""
        try:
            Draft4Validator.check_schema(centos.CentOSView.DELETE_SCHEMA)
            schema_valid = True
        except RuntimeError:
            schema_valid = False

        self.assertTrue(schema_valid)
Ejemplo n.º 38
0
 def test_schema(self):
     """Check schema for validity."""
     # check it loads okay
     with open(
         resource_filename("gramps_webapi", "data/apispec.yaml")
     ) as file_handle:
         api_schema = yaml.safe_load(file_handle)
     # check structure
     Draft4Validator.check_schema(api_schema)
Ejemplo n.º 39
0
    def get_validator(self, ref):
        from jsonschema import Draft4Validator

        validator = self._validators.get(ref)
        if validator is not None:
            return validator
        schema = {"$ref": ref, **self.doc}
        Draft4Validator.check_schema(schema)
        self._validators[ref] = validator = Draft4Validator(schema)
        return validator
Ejemplo n.º 40
0
def test():
    """Tests all included schemata against the Draft4Validator"""

    from jsonschema import Draft4Validator

    for schemaname, schemadata in schemastore.items():
        hfoslog("[SCHEMATA] Validating schema ", schemaname)
        Draft4Validator.check_schema(schemadata['schema'])
        if 'uuid' not in schemadata['schema']:
            hfoslog("[SCHEMATA] Schema without uuid encountered: ", schemaname, lvl=debug)
Ejemplo n.º 41
0
 def test_schema_handler_with_default_uri_normalization(self):
     response = self.fetch('/person/Gender/_schema')
     self.assertEqual(response.code, 200)
     schema = json.loads(response.body)
     self.assertEqual(schema['id'], u'http://semantica.globo.com/person/Gender')
     self.assertEqual(schema['$schema'], 'http://json-schema.org/draft-04/schema#')
     try:
         Draft4Validator.check_schema(schema)
     except SchemaError as ex:
         self.fail("Json-schema for class {0} is not valid. Failed for {1:s}".format('person:Gender', ex))
Ejemplo n.º 42
0
def validate_obj_schema(obj, schema):
    """ Validate an object with an schema, if false, returns error message """
    try:
        Draft4Validator.check_schema(schema)
        Draft4Validator(schema).validate(obj)
    except jsonschema.ValidationError as val_err:
        return (False, {'msg': str(val_err)})
    except jsonschema.SchemaError as sch_err:
        return (False, {'msg': str(sch_err)})
    return (True, obj)
Ejemplo n.º 43
0
def test_skill_configuration_schema_is_valid_wrt_draft_04():
    """Test that the JSON schema for the skill configuration file is compliant with the specification Draft 04."""
    skill_config_schema = json.load(
        open(
            os.path.join(
                ROOT_DIR, "aea", "configurations", "schemas", "skill-config_schema.json"
            )
        )
    )
    Draft4Validator.check_schema(skill_config_schema)
    def test_valid_schema(self):

        schema_path = "../schema/phenopacket-schema.json"

        schema_fh = open(os.path.join(os.path.dirname(__file__), schema_path), "r")
        schema = json.load(schema_fh)
        schema_fh.close()

        ## call validator
        Draft4Validator.check_schema(schema)
Ejemplo n.º 45
0
def validateSchemasInFolder(folder):
    path = os.path.abspath(folder)
    files = [f for f in listdir(path) if isfile(join(path, f))]

    for schemaFile in files:
        if (schemaFile.endswith('.json')):
            print("Validating schema ", schemaFile, "...")
            schema = json.load(open(join(path, schemaFile)))
            Draft4Validator.check_schema(schema)
            print("done.")
Ejemplo n.º 46
0
def load_schema(schema):
    """Validates the given schema and returns an associated schema validator
    object that can check other objects' conformance to the schema.

    :param schema: The JSON schema object.
    :returns: The object loaded in from the JSON schema file.

    """
    Draft4Validator.check_schema(schema)
    return Draft4Validator(schema, format_checker=FormatChecker())
Ejemplo n.º 47
0
def validateSchemasInFolder(folder):
    path = os.path.abspath(folder)
    files = [ f for f in listdir(path) if isfile(join(path,f)) ]

    for schemaFile in files:
        if (schemaFile.endswith('.json')):
            print("Validating schema ", schemaFile, "...")
            schema = json.load(open(join(path,schemaFile)))
            Draft4Validator.check_schema(schema)
            print("done.")
Ejemplo n.º 48
0
 def schema(self, schema):
     """sets the stream's schema. An empty schema is "{}". The schemas allow you to set a specific data type. 
     Both python dicts and strings are accepted."""
     if isinstance(schema, basestring):
         strschema = schema
         schema = json.loads(schema)
     else:
         strschema = json.dumps(schema)
     Draft4Validator.check_schema(schema)
     self.set({"schema": strschema})
    def __parse_schema(self):
        if '$schema' not in self.schema or self.schema['$schema'].find('draft-03') == -1:
            Draft4Validator.check_schema(self.schema)
        else:
            raise ValueError("Draft-03 schema is not supported currently.")

        self.object_defines['root'] = self.schema
        if 'id' in self.schema:
            self.base_uri = self.schema['id']

        self.__parse_object('root',self.schema)
Ejemplo n.º 50
0
    def test_1_generate_json_schema(self):

        meta_json = SQLJSON()
        print()
        f = open(os.path.join(Test_Resource_Dir, "../../../schema/", "sql.json"), "w")
        _schema = meta_json.generate_schema()

        json.dump(obj=_schema, fp=f, sort_keys=True, indent=4)
        f.close()

        Draft4Validator.check_schema(_schema)
Ejemplo n.º 51
0
 def create(self, schema="{}", **kwargs):
     """Creates a stream given an optional JSON schema encoded as a python dict. You can also add other properties
     of the stream, such as the icon, datatype or description. Create accepts both a string schema and
     a dict-encoded schema."""
     if isinstance(schema, basestring):
         strschema = schema
         schema = json.loads(schema)
     else:
         strschema = json.dumps(schema)
     Draft4Validator.check_schema(schema)
     kwargs["schema"] = strschema
     self.metadata = self.db.create(self.path, kwargs).json()
Ejemplo n.º 52
0
def derive_invocation_schema(manifest):
	"""
	Creates an invocation schema from a gear manifest.
	This can be used to validate the files and configuration offered to run a gear.
	"""

	validate_manifest(manifest)

	result = {
		'title': 'Invocation manifest for ' + manifest['label'],
		'$schema': 'http://json-schema.org/draft-04/schema#',
		'type': 'object',
		'properties': {
			'config': {
				'type': 'object',
				'properties': {},
				'required': []
			},
			'inputs': {
				'type': 'object',
				'properties': {},
				'required': []
			}
		},
		'required': [ 'config', 'inputs' ]
	}

	# Copy over constraints from manifest
	for kind in ['config', 'inputs']:
		for key in manifest[kind]:
			# Copy constraints, removing 'base' keyword which is not a constraint
			val = copy.deepcopy(manifest[kind][key])
			val.pop('base', None)

			# The config map holds scalars, while the inputs map holds objects.
			if kind == 'config':
				result['properties'][kind]['properties'][key] = val
			else:
				result['properties'][kind]['properties'][key] = {}
				result['properties'][kind]['properties'][key]['properties'] = val
				result['properties'][kind]['properties'][key]['type'] = 'object'

			# Require the key be preset.
			result['properties'][kind]['required'].append(key)

		# After handling each key, remove required array if none are present.
		# Required by jsonschema (minItems 1).
		if len(result['properties'][kind]['required']) == 0:
			result['properties'][kind].pop('required', None)

	# Important: check our work - the result must be a valid schema.
	Draft4Validator.check_schema(result)
	return result
Ejemplo n.º 53
0
    def generate_models(self, files):

        loader = JmgLoader()

        for fname in (f for fileGlob in files for f in glob.glob(fileGlob)):

            if self.root_name:
                scope = [self.root_name]
            else:
                base_name = os.path.basename(fname)
                base_uri = os.path.splitext(base_name)[0]
                base_uri = base_uri.replace('.schema', '')
                scope = [base_uri]

            with open(fname) as jsonFile:

                print("%s" % fname)

                # root_schema = json.load(jsonFile)
                # base_uri = 'file://' + os.path.split(os.path.realpath(f))[0]
                base_uri = 'file://' + os.path.realpath(fname)
                root_schema = jsonref.load(jsonFile,
                                           base_uri=base_uri,
                                           jsonschema=False, # resolve references relative to local tree, not against "id" URIs
                                           loader=loader,
                                           object_pairs_hook=collections.OrderedDict
                                           )

                # import json
                # print(json.dumps(root_schema, indent=4, separators=(',', ': ')))

                if self.validate:
                    # TODO: Add exception handling
                    try:
                        Draft4Validator.check_schema(root_schema)
                    except SchemaError as e:
                        print(e)
                        sys.exit(-1)

                assert isinstance(root_schema, dict)

                if JsonSchema2Model.SCHEMA_URI not in root_schema:
                    root_schema[JsonSchema2Model.SCHEMA_URI] = fname

                self.create_model(root_schema, scope)

        self.render_models()

        self.copy_static_files()

        if self.include_dependencies:
            self.copy_dependencies()
def validate_schema(path, schemaFile):
    try:
        logger.info("Validating schema %s", schemaFile)
        schema_file = open(join(path, schemaFile))
        schema = json.load(schema_file)
        try:
            Draft4Validator.check_schema(schema)
            return True
        except Exception as e:
            logger.error(e)
            return False
        logger.info("done.")
    finally:
        schema_file.close()
 def _jsonschema_errors(self):
     from django.conf import settings
     errors = []
     schemas = settings.SIMPLE_JSONSCHEMA
     for url, schema in schemas.items():
         try:
             Draft4Validator.check_schema(schema)
         except SchemaError as e:
             errors.append({
                 'url': url,
                 'error': e,
                 'schema': json.dumps(schema, indent=4, sort_keys=True)
             })
     return errors
Ejemplo n.º 56
0
    def setUp(self):
        self.test_request = {
            "elements": "K-Ag",
            "classes": "iodide",
            "props": "heat capacity",
            "lattices": "cubic"
        }

        network = httplib2.Http()

        response, content = network.request('http://developer.mpds.io/mpds.schema.json')
        assert response.status == 200

        self.schema = json.loads(content)
        Draft4Validator.check_schema(self.schema)
Ejemplo n.º 57
0
    def validate(self, obj, check_schema=False):
        '''Validate obj against the schema and check reference constraints.

        Returns a dictionary where each key is a tuple to the path of the
        error and each value is a list of errors which occurred at that
        path. On successful validation, None is returned.
        '''
        # Validate object against schema
        schema = self.schema
        add_instance_constraints(schema, obj)
        if check_schema:
            Draft4Validator.check_schema(schema)
        validator = Draft4Validator(schema)
        try:
            validator.validate(obj)
        except exceptions.ValidationError as e:
            return {tuple(e.path): [e.message]}
Ejemplo n.º 58
0
def isolate_file_invocation(invocation, input_name):
	"""
	Given an invocation schema, isolate just a specific file.

	Useful to validate a single input.
	"""

	inv = copy.deepcopy(invocation)
	fis = inv['properties']['inputs']['properties'][input_name]

	fis['title']   = 'Input invocation manifest for ' + input_name
	fis['$schema'] = 'http://json-schema.org/draft-04/schema#'
	fis['type']    = 'object'

	# Important: check our work - the result must be a valid schema.
	Draft4Validator.check_schema(fis)
	return fis
Ejemplo n.º 59
0
def isolate_config_invocation(invocation):
	"""
	Given an invocation schema, isolate just the config portion.

	Useful to validate configuration options separately from files.
	"""

	inv = copy.deepcopy(invocation)
	fis = inv['properties']['config']

	fis['title']   = 'Config invocation manifest'
	fis['$schema'] = 'http://json-schema.org/draft-04/schema#'
	fis['type']    = 'object'

	# Important: check our work - the result must be a valid schema.
	Draft4Validator.check_schema(fis)
	return fis