Skip to content

Conversation

@creativeprojects
Copy link
Owner

Refactor: Remove Node.js dependency from JSON schema validation tests

This PR removes the Node.js dependency previously required for JSON schema validation in tests and replaces it with a pure Go solution using the github.com/santhosh-tekuri/jsonschema/v6 library.

Changes:

  • Removed Node.js setup from GitHub Actions workflow (build.yml)
  • Replaced npm/ajv-based validation with Go-native JSON schema validation
  • Added Go dependency github.com/santhosh-tekuri/jsonschema/v6 for schema compilation and validation
  • Refactored test functions to use Go-based schema validation instead of external npm commands
  • Added test files with invalid configurations to verify schema validation works correctly
  • Improved test parallelization and structure

Benefits:

  • Eliminates external Node.js dependency for CI/CD
  • Faster test execution with native Go validation
  • Simplified development environment setup
  • Better test reliability without external tool dependencies

The functionality remains the same - all JSON schema validation tests continue to work as expected, but now run entirely within the Go ecosystem.

@coderabbitai
Copy link

coderabbitai bot commented Sep 27, 2025

Walkthrough

Replaces npm/Node.js-based JSON schema validation with a Go-native approach using jsonschema/v6 in tests. Removes Node.js setup from the CI workflow. Adds sample versioned config files under config/jsonschema/. Updates go.mod to include the jsonschema dependency.

Changes

Cohort / File(s) Summary of changes
CI workflow update
.github/workflows/build.yml
Removed Node.js setup (actions/setup-node@v4, node-version "22") used for JSON schema tests; remaining Go build, lint, test, coverage, and Sonar steps unchanged.
Go-based JSON schema test refactor
config/jsonschema/schema_test.go
Rewrote tests to compile and validate schemas using github.com/santhosh-tekuri/jsonschema/v6; removed npm/AJV invocation; added helper compileSchema; renamed tests; expanded parallel sub-tests; adjusted validation/expectations.
Sample versioned config files
config/jsonschema/v1.conf, config/jsonschema/v1.yaml, config/jsonschema/v2.conf, config/jsonschema/v2.yaml
Added simple files for versions "1" and "2" with an extra unknown_property field for testing/validation inputs.
Dependency addition
go.mod
Added dependency: github.com/santhosh-tekuri/jsonschema/v6 v6.0.2.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant T as Go Test Runner
  participant CS as compileSchema
  participant JS as jsonschema/v6
  participant FS as File System

  T->>FS: Read schema and example/invalid config files
  T->>CS: compileSchema(version)
  CS->>JS: Unmarshal + Compile schema
  JS-->>CS: Compiled schema
  CS-->>T: Return compiled schema
  T->>JS: Validate content against compiled schema
  JS-->>T: Validation result (pass/fail)
  Note over T,JS: Node.js/AJV step removed from flow
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title “refactor: update JSON schema tests to use go library” is a concise, single-sentence summary that directly reflects the core change of replacing the Node.js-based schema validation with a Go-native solution, making it clear to reviewers what the primary update entails.
Description Check ✅ Passed The pull request description clearly outlines the removal of the Node.js dependency, the integration of the Go jsonschema library, the specific workflow and test refactorings, and the intended benefits, all of which directly correspond to the changes in the summary and the code.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch run-jsonschema-tests-without-node

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Sep 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.69%. Comparing base (ca2ff90) to head (659fb52).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #563   +/-   ##
=======================================
  Coverage   79.69%   79.69%           
=======================================
  Files         137      137           
  Lines       13467    13467           
=======================================
  Hits        10732    10732           
  Misses       2310     2310           
  Partials      425      425           
Flag Coverage Δ
unittests 79.69% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud
Copy link

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
config/jsonschema/schema_test.go (1)

411-417: Fix: replace slices.Collect(...) with maps.Keys(...) (compile-time error).

slices.Collect expects an iterator/Seq, not a slice. maps.Keys(props) already returns a slice, so wrapping it in slices.Collect(...) will not compile. Also drop the now-unused slices import.

Apply this diff:

@@
-   "slices"
@@
-           m.EXPECT().Properties().Return(slices.Collect(maps.Keys(props)))
+           m.EXPECT().Properties().Return(maps.Keys(props))
🧹 Nitpick comments (7)
config/jsonschema/schema_test.go (7)

80-81: Tighten version detection to avoid false positives (e.g. "21").

Current regex matches "version": "21" as v2. Anchor to the closing quote or parse JSON for robustness.

Apply this diff:

- version2Matcher := regexp.MustCompile(`"version":\s*"2`)
+ version2Matcher := regexp.MustCompile(`"version"\s*:\s*"2"\b`)

Or parse a tiny struct for accuracy:

- schema := schema1
- if version2Matcher.Find(content) != nil {
-   schema = schema2
- }
+ schema := schema1
+ var meta struct{ Version string `json:"version"` }
+ _ = json.Unmarshal(content, &meta)
+ if meta.Version == "2" {
+   schema = schema2
+ }

97-105: Capture loop vars for parallel subtests and use filepath.Base.

Protect against accidental capture by copying filename before t.Parallel(). Also prefer filepath.Base for OS‑specific paths.

Apply this diff:

-        t.Run(path.Base(filename), func(t *testing.T) {
-          if !strings.HasSuffix(filename, ".json") {
-            filename = rewriteToJson(t, filename)
+        fn := filename
+        t.Run(filepath.Base(fn), func(t *testing.T) {
+          if !strings.HasSuffix(fn, ".json") {
+            fn = rewriteToJson(t, fn)
           }
-          filename, _ = filepath.Abs(filename)
+          fn, _ = filepath.Abs(fn)
@@
-          content, e = os.ReadFile(filename)
+          content, e = os.ReadFile(fn)
@@
-          t.Parallel()
+          t.Parallel()

Also applies to: 110-121


114-120: Avoid logging full config payloads on failure.

Dumping entire files can leak secrets in real configs. Log the filename and a trimmed snippet instead.

Apply this diff:

-          if err != nil {
-            t.Log(string(content))
-          }
+          if err != nil {
+            const max = 512
+            preview := content
+            if len(preview) > max {
+              preview = preview[:max]
+            }
+            t.Logf("validation failed for %s; first %d bytes:\n%s", filepath.Base(fn), len(preview), string(preview))
+          }

88-125: Optional: use WalkDir and early‑continue for clarity.

filepath.WalkDir avoids extra os.Stat calls and simplifies the callback; using early returns makes the intent crisper.

Happy to provide a diff if you want to switch.


126-127: Make the lower bound deterministic.

assert.Greater(t, testCount, 8) is brittle. Assert against the discovered fixture count or a named constant that reflects the curated set.


137-161: Mirror the var‑capture and logging tweaks in the “invalid” suite.

Apply the same fn := filename copy and log the filename when the unexpected success occurs.

Apply this diff:

-        t.Run(path.Base(filename), func(t *testing.T) {
-          if !strings.HasSuffix(filename, ".json") {
-            filename = rewriteToJson(t, filename)
+        fn := filename
+        t.Run(filepath.Base(fn), func(t *testing.T) {
+          if !strings.HasSuffix(fn, ".json") {
+            fn = rewriteToJson(t, fn)
           }
-          filename, _ = filepath.Abs(filename)
+          fn, _ = filepath.Abs(fn)
@@
-          content, e = os.ReadFile(filename)
+          content, e = os.ReadFile(fn)
@@
-          if err == nil {
-            t.Log(string(content))
-          }
+          if err == nil {
+            t.Logf("unexpectedly valid: %s", filepath.Base(fn))
+          }

5-30: Go toolchain ≥1.23 confirmed; stdlib maps/slices (iterator APIs) available
The stdlib maps and slices packages (with iterator-support functions like slices.Collect and maps.Keys) were added in Go 1.23 (go.daehub.com). Both go.mod (go 1.24.7) and CI matrix (go-version: 1.24) ensure the toolchain is ≥1.23.
The slices import in config/jsonschema/schema_test.go remains necessary (used at line 412); drop it only after refactoring or replacing all slices.Collect calls in that file.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca2ff90 and 659fb52.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (7)
  • .github/workflows/build.yml (0 hunks)
  • config/jsonschema/schema_test.go (24 hunks)
  • config/jsonschema/v1.conf (1 hunks)
  • config/jsonschema/v1.yaml (1 hunks)
  • config/jsonschema/v2.conf (1 hunks)
  • config/jsonschema/v2.yaml (1 hunks)
  • go.mod (1 hunks)
💤 Files with no reviewable changes (1)
  • .github/workflows/build.yml
🧰 Additional context used
🧬 Code graph analysis (1)
config/jsonschema/schema_test.go (4)
config/version.go (3)
  • Version (5-5)
  • Version01 (9-9)
  • Version02 (10-10)
config/jsonschema/schema.go (1)
  • WriteJsonSchema (20-29)
restic/commands.go (1)
  • Option (28-33)
config/info.go (1)
  • NamedPropertySet (66-69)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build and test (1.24, ubuntu-latest)
  • GitHub Check: Build and test (1.24, windows-latest)
  • GitHub Check: Build and test (1.24, macos-latest)
🔇 Additional comments (2)
config/jsonschema/schema_test.go (2)

48-56: Parallelisation looks good.

Nice use of t.Parallel() to speed up the suite; structure preserves deterministic setup and per‑case isolation.

Also applies to: 58-63, 170-171, 279-279, 286-286, 293-293, 300-300, 307-307, 339-340, 464-465, 554-555, 573-574, 595-595, 610-611, 631-632, 654-654, 664-664, 686-686, 696-696, 706-706, 716-716, 726-727, 730-730, 739-740


32-46: Simplify schema compilation using AddResource & update validation API

  • Replace the double-parse (UnmarshalJSON) and re-registration with a single call to Compiler.AddResource("mem://schema.json", bytes.NewReader(schemaBuffer.Bytes())), then Compile("mem://schema.json").
  • Change any ValidateInterface calls to (*jsonschema.Schema).Validate(v any) error, per santhosh-tekuri/jsonschema v6.
 func compileSchema(t *testing.T, version config.Version) *jsonschema.Schema {
   t.Helper()

-  schemaBuffer := &bytes.Buffer{}
-  err := WriteJsonSchema(version, "", schemaBuffer)
+  schemaBuffer := &bytes.Buffer{}
+  err := WriteJsonSchema(version, "", schemaBuffer)
   require.NoError(t, err)
-  schemaJSON, err := jsonschema.UnmarshalJSON(schemaBuffer)
-  require.NoError(t, err)
-  compiler := jsonschema.NewCompiler()
-  err = compiler.AddResource("schema.json", schemaJSON)
-  require.NoError(t, err)
-  schema, err := compiler.Compile("schema.json")
+  compiler := jsonschema.NewCompiler()
+  err = compiler.AddResource("mem://schema.json", bytes.NewReader(schemaBuffer.Bytes()))
+  require.NoError(t, err)
+  schema, err := compiler.Compile("mem://schema.json")
   require.NoError(t, err)
   return schema
 }

@creativeprojects creativeprojects merged commit 00f3f2d into master Sep 27, 2025
11 checks passed
@creativeprojects creativeprojects deleted the run-jsonschema-tests-without-node branch September 27, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants