def ValidateSubfields(self, field):
        """Validates that the subfields that compose the field are valid.

    Adds any findings to the field.

    Args:
      field: reference to the Field object for subfield validation

    Returns:
      True if the field uses any subfields from the local namespace.
    """
        pns = self.parent_namespace
        if not self.SubfieldsAreDefined() or (pns is not None and
                                              not pns.SubfieldsAreDefined()):
            # If subfields are undefined on any relevant namespace, proper subfield
            # validation is impossible. Note that an empty subfield list is still a
            # defined subfield list.
            return False

        missing_fields = False
        uses_local_subfields = False
        for subfield in field.subfields:
            if subfield in self.subfields:
                uses_local_subfields = True
            elif pns is None or subfield not in pns.subfields:
                missing_fields = True
                field.AddFinding(
                    findings_lib.MissingSubfieldError(subfield, field))

        if not missing_fields and not self._IsValidConstruction(field):
            field.AddFinding(findings_lib.InvalidFieldConstructionError(field))

        return uses_local_subfields
Example #2
0
    def testFieldsUniverseGetFindings(self):
        context = findings_lib.FileContext(_GOOD_PATH + '/file.yaml')
        folder = field_lib.FieldFolder(_GOOD_PATH)
        folder.AddFinding(
            findings_lib.InconsistentFileLocationError('', context))
        namespace = folder.local_namespace
        namespace.AddFinding(
            findings_lib.MissingSubfieldError(['any'],
                                              field_lib.Field(
                                                  'two', context=context)))
        field = field_lib.Field('one', context=context)
        # Currently there are no warnings for fields, so using a subfield warning
        field.AddFinding(
            findings_lib.MissingSubfieldDescriptionWarning('one', context))
        namespace.InsertField(field)

        fields_universe = field_lib.FieldUniverse([folder])

        findings = fields_universe.GetFindings()
        self.assertLen(findings, 3)
        self.assertTrue(
            fields_universe.HasFindingTypes([
                findings_lib.InconsistentFileLocationError,
                findings_lib.MissingSubfieldError,
                findings_lib.MissingSubfieldDescriptionWarning
            ]))
        self.assertFalse(fields_universe.IsValid())