func validateRequiredFields()

in code/go/internal/validator/semantic/validate_required_fields.go [53:111]


func validateRequiredFields(fsys fspath.FS, requiredFields map[string]string) specerrors.ValidationErrors {
	// map datastream/package -> field name -> found
	foundFields := make(map[string]map[string]struct{})

	checkField := func(metadata fieldFileMetadata, f field) specerrors.ValidationErrors {
		if _, ok := foundFields[metadata.dataStream]; !ok {
			foundFields[metadata.dataStream] = make(map[string]struct{})
		}
		foundFields[metadata.dataStream][f.Name] = struct{}{}

		expectedType, found := requiredFields[f.Name]
		if !found {
			return nil
		}

		// Check if type is the expected one, but only for fields what are
		// not declared as external. External fields won't have a type in
		// the definition.
		// More info in https://github.com/elastic/elastic-package/issues/749
		if f.External == "" && f.Type != expectedType {
			return specerrors.ValidationErrors{
				specerrors.NewStructuredError(
					unexpectedTypeRequiredField{
						field:        f.Name,
						foundType:    f.Type,
						dataStream:   metadata.dataStream,
						fullPath:     metadata.fullFilePath,
						expectedType: expectedType,
					},
					specerrors.UnassignedCode,
				),
			}
		}

		return nil
	}
	errs := validateFields(fsys, checkField)

	// Using the data streams found here, since there could not be a data stream
	// without the `fields` folder or an input package without that folder
	for dataStream, dataStreamFields := range foundFields {
		for requiredName, requiredType := range requiredFields {
			if _, found := dataStreamFields[requiredName]; !found {
				errs = append(errs,
					specerrors.NewStructuredError(
						notFoundRequiredField{
							field:        requiredName,
							expectedType: requiredType,
							dataStream:   dataStream,
						},
						specerrors.UnassignedCode,
					),
				)
			}
		}
	}

	return errs
}