def validate(self, val):
        """
        Validates that the val matches the expected fields for this struct.
        val must be a dict, and must contain only fields represented by this struct and its
        ancestors.

        Returns two element tuple: (bool, string)

        - `bool` - True if valid, False if not
        - `string` - Description of validation error, or None if valid

        :Parameters:
          val
            Value to validate.  Must be a dict
        """
        if type(val) is not dict:
            return False, "%s is not a dict" % (str(val))

        for k, v in list(val.items()):
            field = self.field(k)
            if field:
                ok, msg = self.contract.validate(field, field.is_array, v)
                if not ok:
                    return False, "field '%s': %s" % (field.name, msg)
            else:
                return False, "field '%s' not found in struct %s" % (k, self.name)

        all_fields = self.get_all_fields([])
        for field in all_fields:
            if field.name not in val and not field.optional:
                return False, "field '%s' missing from: %s" % (field.name, str(val))

        return True, None
dataset/ETHPy150Open StackHut/stackhut/stackhut_toolkit/common/barrister/runtime.py/Struct.validate/original