This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #7 * Added TableValidation to enforce column/table name longer than 63 char #9
- Loading branch information
1 parent
a143680
commit 7d8a9a3
Showing
10 changed files
with
156 additions
and
13 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
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
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 fmt.Errorf("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,43 @@ | ||
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) | ||
} |