Skip to content

implement schema validation for vendoring and CLI schemas - #1147

Merged
Andriy Knysh (aknysh) merged 147 commits into
mainfrom
feature/dev-3030-implement-schema-validation-for-vendoring-and-2
May 1, 2025
Merged

Andriy Knysh (aknysh) merged 147 commits into
mainfrom
feature/dev-3030-implement-schema-validation-for-vendoring-and-2

Conversation

@samtholiya

@samtholiya Sam Tholiya (samtholiya) commented Mar 14, 2025 •

Copy link
Copy Markdown
Contributor

what

  • Added a new validator command to the CLI.
  • This command verifies the YAML structure of the atmos CLI.
  • If a schema key is present in the YAML, it validates the structure against the schema.

image
image

why

  • Ensures that configuration files follow the correct YAML structure.
  • Prevents misconfigurations and errors caused by invalid or malformed YAML.
  • Adds an extra layer of validation by enforcing schema compliance when applicable.
  • Improves reliability and usability of the CLI by catching issues early.

references

Summary by CodeRabbit

  • New Features
    • Added a CLI command to validate YAML files against JSON schemas defined in project configuration.
    • Provided example schema and configuration files for schema validation demonstration.
    • Enhanced data fetching to support HTTP, local files, inline JSON, and embedded Atmos schema sources.
    • Implemented flexible file matching using glob patterns with gitignore-style support.
    • Introduced YAML schema validation with detailed error reporting and logging.
    • Added new JSON schemas for Atmos Stack Manifest and Vendor Config files.
    • Added file downloading capabilities with automatic format detection and parsing.
    • Improved URI validation with explicit error messages for common issues.
  • Bug Fixes
    • Enhanced URI validation error handling for clearer user feedback.
  • Tests
    • Added unit tests for schema validation, data fetching, file matching, and file downloading.
    • Included CLI tests covering schema validation success and failure scenarios.
    • Added mocks to support testing of data fetchers, validators, and file downloaders.

Sam Tholiya (samtholiya) and others added 30 commits March 8, 2025 23:23
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
cmd/validate_schema.go (3)

3-11: Clean up redundant imports for utils package.

You're importing the same utils package twice - once as the full name and once with an alias 'u'. This creates unnecessary confusion.

import (
	"errors"

	log "github.com/charmbracelet/log"
	"github.com/cloudposse/atmos/internal/exec"
-	"github.com/cloudposse/atmos/pkg/utils"
	u "github.com/cloudposse/atmos/pkg/utils"
	"github.com/spf13/cobra"
)

13-13: Complete the comment with a period.

The static analysis tool correctly identified that this comment should end with a period to maintain consistency with Go documentation standards.

- // ValidateSchemaCmd represents the 'atmos validate schema' command.
+ // ValidateSchemaCmd represents the 'atmos validate schema' command.

55-57: Consider handling flag parsing errors consistently.

While you've indicated the flag parsing error handling isn't required, it would be better for consistency and robustness to handle potential errors.

if cmd.Flags().Changed("schemas-atmos-manifest") {
-	schema, _ = cmd.Flags().GetString("schemas-atmos-manifest")
+	var err error
+	schema, err = cmd.Flags().GetString("schemas-atmos-manifest")
+	if err != nil {
+		log.Error("Failed to parse schema flag", "error", err)
+		u.OsExit(1)
+	}
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f85320 and 9a85217.

📒 Files selected for processing (3)
  • cmd/validate_schema.go (1 hunks)
  • website/docs/cli/commands/validate/validate-schema.mdx (1 hunks)
  • website/src/components/Screengrabs/atmos-validate-schema--help.html (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • website/docs/cli/commands/validate/validate-schema.mdx
  • website/src/components/Screengrabs/atmos-validate-schema--help.html
🧰 Additional context used
🧠 Learnings (1)
cmd/validate_schema.go (1)
Learnt from: samtholiya
PR: cloudposse/atmos#1147
File: internal/exec/validate_schema.go:42-57
Timestamp: 2025-04-11T22:06:46.999Z
Learning: The "ExecuteAtmosValidateSchemaCmd" function in internal/exec/validate_schema.go has been reviewed and confirmed to have acceptable cognitive complexity despite static analysis warnings. The function uses a clean structure with only three if statements for error handling and delegates complex operations to helper methods.
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (2)
cmd/validate_schema.go (2)

25-71: The command implementation is well structured and robust.

The command properly separates concerns and provides clear error handling paths. The retrieved learning confirms that the ExecuteAtmosValidateSchemaCmd function has acceptable complexity despite static analysis warnings, as it delegates complex operations to helper methods.


73-76: Flag definition and command registration looks good.

The flag name matches what's being checked in the command execution and has a clear description. The command is properly registered with the parent command.

Comment thread cmd/validate_schema.go Resolved
coderabbitai[bot]
coderabbitai Bot previously approved these changes Apr 22, 2025
coderabbitai[bot]
coderabbitai Bot previously approved these changes Apr 27, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
pkg/datafetcher/schema/stacks/stack-config/1.0.json (1)

380-389: ⚠️ Potential issue

Add !include support to scalar definitions.

Per the project conventions, any definition should allow an include string (!include). The "command" and "component" definitions currently only allow plain strings. Update them to a oneOf that supports both a !include reference and a normal string.

Example diff:

   "command": {
-    "title": "command",
-    "description": "Command to execute",
-    "type": "string"
+    "title": "command",
+    "description": "Command to execute",
+    "oneOf": [
+      { "type": "string", "pattern": "^!include" },
+      { "type": "string" }
+    ]
   },
   "component": {
-    "title": "component",
-    "description": "Component section",
-    "type": "string"
+    "title": "component",
+    "description": "Component section",
+    "oneOf": [
+      { "type": "string", "pattern": "^!include" },
+      { "type": "string" }
+    ]
   },
🧹 Nitpick comments (2)
pkg/datafetcher/schema/stacks/stack-config/1.0.json (2)

2-4: Include version in $id for clarity and conflict avoidance.

Using a generic $id may lead to collisions if multiple schema versions are published. Consider appending the version to the $id, e.g.:

-$id": "https://json.schemastore.org/atmos-manifest.json",
+$id": "https://json.schemastore.org/atmos-manifest-1.0.json",

38-38: Consider restricting unknown top-level properties.

Currently additionalProperties: true at the root allows any extra keys. To enforce strict schema validation, you may want to set additionalProperties: false so that only known sections are permitted. If you need to allow extensions, you could use a pattern or custom extension mechanism instead.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2f9d020 and e2743bd.

📒 Files selected for processing (2)
  • pkg/datafetcher/schema/config/global/1.0.json (1 hunks)
  • pkg/datafetcher/schema/stacks/stack-config/1.0.json (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • pkg/datafetcher/schema/config/global/1.0.json
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary

@aknysh Andriy Knysh (aknysh) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aknysh
Andriy Knysh (aknysh) merged commit 150687a into main May 1, 2025
@aknysh
Andriy Knysh (aknysh) deleted the feature/dev-3030-implement-schema-validation-for-vendoring-and-2 branch May 1, 2025 00:11
@mergify mergify Bot removed the needs-cloudposse Needs Cloud Posse assistance label May 1, 2025
@github-actions

github-actions Bot commented May 1, 2025

Copy link
Copy Markdown

These changes were released in v1.174.0.

This branch was successfully deployed

1 active deployment
preview — e2743bd2 Deployed Apr 28, 2025 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-release Do not create a new release (wait for additional code changes)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants