-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Early validation base methods (#293)
* Validate body per schema skeleton * Introduce and run validation functions * Add validate test skeleton * Add copyright headers
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package decoder | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func (d *PathDecoder) Validate(ctx context.Context) (hcl.Diagnostics, error) { | ||
if d.pathCtx.Schema == nil { | ||
return hcl.Diagnostics{}, &NoSchemaError{} | ||
} | ||
|
||
diags := hcl.Diagnostics{} | ||
// Validate module files per schema | ||
for _, f := range d.pathCtx.Files { | ||
body, ok := f.Body.(*hclsyntax.Body) | ||
if !ok { | ||
// TODO! error | ||
continue | ||
} | ||
|
||
diags = diags.Extend(d.validateBody(ctx, body, d.pathCtx.Schema)) | ||
} | ||
|
||
// Run validation functions | ||
for _, vFunc := range d.decoderCtx.Validations { | ||
diags = diags.Extend(vFunc(ctx)) | ||
} | ||
|
||
return diags, nil | ||
} | ||
|
||
func (d *PathDecoder) validateBody(ctx context.Context, body *hclsyntax.Body, bodySchema *schema.BodySchema) hcl.Diagnostics { | ||
diags := hcl.Diagnostics{} | ||
|
||
for name, _ := range body.Attributes { | ||
_, ok := bodySchema.Attributes[name] | ||
if !ok { | ||
// TODO! unknown attribute validation | ||
} | ||
// TODO! validate against schema | ||
} | ||
|
||
for _, block := range body.Blocks { | ||
blockSchema, ok := bodySchema.Blocks[block.Type] | ||
if !ok { | ||
// TODO! unknown block validation | ||
} | ||
// TODO! validate against schema | ||
|
||
if block.Body != nil { | ||
mergedSchema, err := mergeBlockBodySchemas(block.AsHCLBlock(), blockSchema) | ||
if err != nil { | ||
// TODO! err | ||
} | ||
|
||
diags = diags.Extend(d.validateBody(ctx, block.Body, mergedSchema)) | ||
} | ||
} | ||
|
||
return diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package decoder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func TestValidate_schema(t *testing.T) { | ||
testCases := []struct { | ||
testName string | ||
bodySchema *schema.BodySchema | ||
cfg string | ||
expectedDiagnostics hcl.Diagnostics | ||
}{ | ||
{ | ||
"empty schema", | ||
schema.NewBodySchema(), | ||
``, | ||
hcl.Diagnostics{}, | ||
}, | ||
{ | ||
"valid schema", | ||
&schema.BodySchema{ | ||
Attributes: map[string]*schema.AttributeSchema{ | ||
"test": { | ||
Constraint: schema.LiteralType{Type: cty.Number}, | ||
IsRequired: true, | ||
}, | ||
}, | ||
}, | ||
`test = 1`, | ||
hcl.Diagnostics{}, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(fmt.Sprintf("%2d-%s", i, tc.testName), func(t *testing.T) { | ||
f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos) | ||
d := testPathDecoder(t, &PathContext{ | ||
Schema: tc.bodySchema, | ||
Files: map[string]*hcl.File{ | ||
"test.tf": f, | ||
}, | ||
}) | ||
|
||
ctx := context.Background() | ||
diags, err := d.Validate(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := cmp.Diff(tc.expectedDiagnostics, diags); diff != "" { | ||
t.Fatalf("unexpected diagnostics: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package lang | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
) | ||
|
||
type ValidationFunc func(ctx context.Context) hcl.Diagnostics |