func ValidateExternalFieldsWithDevFolder()

in code/go/internal/validator/semantic/validate_external_fields_with_dev_folder.go [16:65]


func ValidateExternalFieldsWithDevFolder(fsys fspath.FS) specerrors.ValidationErrors {

	const buildPath = "_dev/build/build.yml"
	buildFilePathDefined := true
	f, err := pkgpath.Files(fsys, buildPath)
	if err != nil {
		return specerrors.ValidationErrors{
			specerrors.NewStructuredErrorf("not able to read _dev/build/build.yml: %w", err),
		}
	}

	if len(f) != 1 {
		buildFilePathDefined = false
	}

	mapDependencies := make(map[string]struct{})
	if buildFilePathDefined {
		dependencies, err := readDevBuildDependenciesKeys(f[0])
		if err != nil {
			return specerrors.ValidationErrors{specerrors.NewStructuredError(err, specerrors.UnassignedCode)}
		}
		for _, dep := range dependencies {
			mapDependencies[dep] = struct{}{}
		}
	}

	validateFunc := func(metadata fieldFileMetadata, f field) specerrors.ValidationErrors {
		if f.External == "" {
			return nil
		}

		if !buildFilePathDefined {
			return specerrors.ValidationErrors{
				specerrors.NewStructuredErrorf(
					"file \"%s\" is invalid: field %s with external key defined (%q) but no _dev/build/build.yml found",
					metadata.fullFilePath, f.Name, f.External),
			}
		}

		if _, ok := mapDependencies[f.External]; !ok {
			return specerrors.ValidationErrors{
				specerrors.NewStructuredErrorf(
					"file \"%s\" is invalid: field %s with external key defined (%q) but no definition found for it (_dev/build/build.yml)",
					metadata.fullFilePath, f.Name, f.External),
			}
		}
		return nil
	}
	return validateFields(fsys, validateFunc)
}