Repository: cerbos/cerbos (go)

Issue:
Provide more verbose error message if `_schemas` directory is not at storage root
As per #1536

Pull request description:
enhancement: Handle schemas folder being in wrong place with more verbose error
resolves #1539

Patch:
diff --git a/internal/storage/index/builder.go b/internal/storage/index/builder.go
index 614f7852..190d5018 100644
--- a/internal/storage/index/builder.go
+++ b/internal/storage/index/builder.go
@@ -23,6 +23,9 @@ import (
 	"github.com/cerbos/cerbos/internal/util"
 )
 
+// errSchemasInWrongDir signals that schemas folder is in the wrong place.
+var errSchemasInWrongDir = fmt.Errorf("%s directory must be under the root of the storage directory", util.SchemasDirectory)
+
 const maxLoggableBuildErrors = 5
 
 // BuildError is an error type that contains details about the failures encountered during the index build.
@@ -94,6 +97,9 @@ func build(ctx context.Context, fsys fs.FS, opts buildOptions) (Index, error) {
 				d.Name() == util.TestDataDirectory ||
 				util.IsHidden(d.Name()) {
 				return fs.SkipDir
+			} else if d.Name() == schema.Directory {
+				ib.addLoadFailure(filePath, errSchemasInWrongDir)
+				return fs.SkipDir
 			}
 
 			return nil


Test patch:
diff --git a/internal/test/testdata/index/schemas_in_valid_dir.yaml b/internal/test/testdata/index/schemas_in_valid_dir.yaml
new file mode 100644
index 00000000..fe0ed5d1
--- /dev/null
+++ b/internal/test/testdata/index/schemas_in_valid_dir.yaml
@@ -0,0 +1,18 @@
+files:
+  "_schemas/principal.json": |-
+    {
+      "$schema": "https://json-schema.org/draft/2020-12/schema",
+      "type": "object",
+      "properties": {
+        "department": {
+          "type": "string",
+          "enum": [
+            "marketing",
+            "engineering"
+          ]
+        }
+      },
+      "required": [
+        "department"
+      ]
+    }
diff --git a/internal/test/testdata/index/schemas_in_wrong_dir.yaml b/internal/test/testdata/index/schemas_in_wrong_dir.yaml
new file mode 100644
index 00000000..da7fb207
--- /dev/null
+++ b/internal/test/testdata/index/schemas_in_wrong_dir.yaml
@@ -0,0 +1,23 @@
+wantErrList:
+  loadFailures:
+    - error: |-
+        _schemas directory must be under the root of the storage directory
+      file: policies/_schemas
+files:
+  "policies/_schemas/principal.json": |-
+    {
+      "$schema": "https://json-schema.org/draft/2020-12/schema",
+      "type": "object",
+      "properties": {
+        "department": {
+          "type": "string",
+          "enum": [
+            "marketing",
+            "engineering"
+          ]
+        }
+      },
+      "required": [
+        "department"
+      ]
+    }
diff --git a/internal/test/testdata/index/schemas_prepended_dir.yaml b/internal/test/testdata/index/schemas_prepended_dir.yaml
new file mode 100644
index 00000000..a71d976f
--- /dev/null
+++ b/internal/test/testdata/index/schemas_prepended_dir.yaml
@@ -0,0 +1,12 @@
+files:
+  "bar_schemas/resource.yaml": |-
+    ---
+    apiVersion: api.cerbos.dev/v1
+    resourcePolicy:
+      resource: leave_request
+      rules:
+      - actions: ['*']
+        effect: EFFECT_ALLOW
+        roles:
+        - admin
+      version: "20210210"