forked from cloudquery/cq-provider-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added resource set validation cloudquery#7 Added TableValidation to enforce column/table name longer than 63 char cloudquery#9
- Loading branch information
1 parent
7ca9ac8
commit f8f2df7
Showing
8 changed files
with
121 additions
and
99 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 was deleted.
Oops, something went wrong.
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
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
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,54 @@ | ||
package schema | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
const maxTableName = 63 | ||
|
||
const maxColumnName = 63 | ||
|
||
var defaultValidators = []TableValidator{ | ||
LengthTableValidator{}, | ||
} | ||
|
||
func ValidateTable(t *Table) error { | ||
for _, validator := range defaultValidators { | ||
return validator.Validate(t) | ||
} | ||
return nil | ||
} | ||
|
||
type TableValidator interface { | ||
Validate(t *Table) error | ||
} | ||
|
||
type LengthTableValidator struct{} | ||
|
||
func validateTableAttributesNameLength(t *Table) error { | ||
// validate table name | ||
if len(t.Name) > maxTableName { | ||
return errors.New("table name has exceeded max length") | ||
} | ||
|
||
// validate table columns | ||
for _, col := range t.ColumnNames() { | ||
if len(col) > maxColumnName { | ||
return errors.New(fmt.Sprintf("column name %s has exceeded max length", col)) | ||
} | ||
} | ||
|
||
// validate table relations | ||
for _, rel := range t.Relations { | ||
err := validateTableAttributesNameLength(rel) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (tv LengthTableValidator) Validate(t *Table) error { | ||
return validateTableAttributesNameLength(t) | ||
} |
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,42 @@ | ||
package schema | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
var testTableValidators = Table{ | ||
Name: "test_table_validator", | ||
Columns: []Column{ | ||
{ | ||
Name: "zero_bool", | ||
Type: TypeBool, | ||
}, | ||
{ | ||
Name: "zero_int", | ||
Type: TypeBigInt, | ||
}, | ||
{ | ||
Name: "not_zero_bool", | ||
Type: TypeBool, | ||
}, | ||
}, | ||
} | ||
|
||
func TestTableValidators(t *testing.T) { | ||
// table has passed all validators | ||
err := ValidateTable(&testTableValidators) | ||
assert.Nil(t, err) | ||
|
||
// table name is too long | ||
tableWithLongName := testTableValidators | ||
tableWithLongName.Name = "WithLongNametableWithLongNametableWithLongNametableWithLongNamet" | ||
err = ValidateTable(&tableWithLongName) | ||
assert.Error(t, err) | ||
|
||
// column name is too long | ||
tableWithLongColumnName := testTableValidators | ||
tableWithLongName.Columns[0].Name = "tableWithLongColumnNametableWithLongColumnNametableWithLongColumnName" | ||
err = ValidateTable(&tableWithLongColumnName) | ||
assert.Error(t, err) | ||
} |