def _validateLayerMediaTypes(self): """validate the Layer Media Types """ # These are valid mediaTypes for layers layerMediaTypes = [ MediaTypeImageLayer, MediaTypeImageLayerGzip, MediaTypeImageLayerZstd, MediaTypeImageLayerNonDistributable, MediaTypeImageLayerNonDistributableGzip, MediaTypeImageLayerNonDistributableZstd, ] # No layers, not valid layers = self.attrs.get("Layers").value if not layers: return False # Check against valid mediaType Layers for layer in layers: mediaType = layer.attrs.get("MediaType").value if mediaType not in layerMediaTypes: bot.error("layer mediaType %s is invalid" % mediaType) return False return True
def validate(self): """validate goes through each attribute, and ensure that it is of the correct type, and if required it is defined. This is already done to some extent when load is called, but this function serves as a final validation (after an initial config is loaded). """ for name, att in self.attrs.items(): # Not required, undefined if not att.required and not att.value: continue # A required attribute cannot be None or empty if att.required and not att.value: bot.error("%s is required." % name) return False # The attribute must match its type if not att.validate_type(att.value): bot.error("%s should be type %s" % (name, att.attType)) return False # Some structs need to further validate string content if hasattr(self, "_validate"): if not self._validate(): return False return True
def validate_regexp(self, value): """validate a string or nested string values against a regular expression. Return True if valid or not applicable, False otherwise """ if not self.regexp: return True # Only need to look at immediate children if not isinstance(value, list): value = [value] for entry in value: if isinstance(entry, str): if not re.search(self.regexp, entry): bot.error("%s failed regex validation %s " % (entry, self.regexp)) return False return True
def _validateConfigMediaType(self): """validate the config media type.""" # The media type of the config must be for the config manifestConfig = self.attrs.get("Config").value # Missing config is not valid if not manifestConfig: return False mediaType = manifestConfig.attrs.get("MediaType").value if not mediaType: return False if mediaType != MediaTypeImageConfig: bot.error("config mediaType %s is invalid, should be %s" % (mediaType, MediaTypeImageConfig)) return False return True
def validate(self): '''validate goes through each attribute, and ensure that it is of the correct type, and if required it is defined. This is already done to some extent when add is called. ''' for name, att in self.attrs.items(): # Not required, undefined if not att.required and not att.value: continue # A required attribute cannot be None or empty if att.required and not att.value: bot.error('%s is required.' % name) return False # The attribute must match its type if not isinstance(att.value, att.attType): bot.error("%s should be type %s" % (name, att.attType)) return False return True
def _validate(self): """custom validation function to ensure that Manifests mediaTypes are valid. """ valid_types = [MediaTypeImageManifest, MediaTypeImageIndex] manifests = self.attrs.get("Manifests").value if manifests: for manifest in manifests: mediaType = manifest.attrs.get("MediaType") if mediaType.value not in valid_types: # Case 1: it's a custom media type (allowed) but give warning if mediaType.validate_regexp(mediaType.value): bot.warning( "%s is valid, but not registered." % mediaType.value ) # Case 2: not valid and doesn't match regular expression else: bot.error("%s is not valid for index manifest." % mediaType) return False return True