def validate_model_metadata(data: Dict) -> Dict: """Validate metadata. Parameters ---------- data User defined model metadata (json) Returns ------- Validated model metadata (json) Raises ------ SeldonInvalidMetadataError if data cannot be properly validated Notes ----- Read data from json and create ModelMetadataValidator and MetadataTensorValidator objects. This function reads data in safe manner from json: validation and exceptions will happen in ModelMetadataValidator and MetadataTensorValidator classes. SeldonInvalidMetadataError has been chosen for exception as validation mostly depend on having a correct type for specific components. """ if MODEL_IMAGE is not None: image_name, image_version = split_image_tag(MODEL_IMAGE) else: image_name, image_version = "", "" name = data.get("name", image_name) versions = data.get("versions", [image_version]) platform = data.get("platform", "") try: inputs = [ MetadataTensorValidator(x.get("name"), x.get("datatype"), x.get("shape")) for x in data.get("inputs", []) ] outputs = [ MetadataTensorValidator(x.get("name"), x.get("datatype"), x.get("shape")) for x in data.get("outputs", []) ] except (AttributeError, TypeError): raise SeldonInvalidMetadataError( "Model metadata inputs and outputs must be sequence of dictionaries." ) meta = ModelMetadataValidator( name=name, versions=versions, platform=platform, inputs=inputs, outputs=outputs, ) return meta.to_dict()
def test_split_image_tag(): image = "seldonio/sklearn-iris:0.1" expected_name = "seldonio/sklearn-iris" expected_version = "0.1" name, version = split_image_tag(image) assert name == expected_name assert version == expected_version
def test_split_image_tag_with_colon(): image = "localhost:32000/sklearn-iris:0.1" expected_name = "localhost:32000/sklearn-iris" expected_version = "0.1" name, version = split_image_tag(image) assert name == expected_name assert version == expected_version
def validate_model_metadata(data: Dict) -> Dict: """Validate metadata. Parameters ---------- data User defined model metadata (json) Returns ------- Validated model metadata (json) Raises ------ SeldonInvalidMetadataError if data cannot be properly validated Notes ----- Read data from json and validate against v1 or v2 metadata schema. SeldonInvalidMetadataError exception will be raised if validation fails. """ if MODEL_IMAGE is not None: image_name, image_version = split_image_tag(MODEL_IMAGE) else: image_name, image_version = "", "" default_meta = { "apiVersion": "v2", "name": image_name, "versions": [image_version], "platform": "", "inputs": [], "outputs": [], } data = {**default_meta, **data} v = data.get("apiVersion", "v2") if v == "v1": schema = V1_SCHEMA elif v == "v2": schema = V2_SCHEMA else: raise SeldonInvalidMetadataError(f"Unknown metadata schema: {v}") try: validate(data, schema) except ValidationError as e: raise SeldonInvalidMetadataError(e) logger.debug( f"Successfully validated metadata:\n{json.dumps(data, indent=2)}") return data