feat(validation): Add check for necessary keys in field json
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
|||||||
"git.secnex.io/secnex/pgson/utils"
|
"git.secnex.io/secnex/pgson/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InsertManySQL(s *schema.Table, data []map[string]any) (string, error) {
|
func InsertManySQL(s *schema.Table, data []map[string]any, returning bool) (string, error) {
|
||||||
// Keep unquoted column names for data access
|
// Keep unquoted column names for data access
|
||||||
columnNames := make([]string, 0, len(data[0]))
|
columnNames := make([]string, 0, len(data[0]))
|
||||||
for column := range data[0] {
|
for column := range data[0] {
|
||||||
@@ -48,9 +48,13 @@ func InsertManySQL(s *schema.Table, data []map[string]any) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", utils.SQLQuoteIdent(s.Name), strings.Join(columns, ", "), strings.Join(values, ", "))
|
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", utils.SQLQuoteIdent(s.Name), strings.Join(columns, ", "), strings.Join(values, ", "))
|
||||||
|
if returning {
|
||||||
|
// RETURNING the primary key
|
||||||
|
query += " RETURNING " + utils.SQLQuoteIdent(s.PrimaryKey)
|
||||||
|
}
|
||||||
return query, nil
|
return query, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func InsertSQL(s *schema.Table, data map[string]any) (string, error) {
|
func InsertSQL(s *schema.Table, data map[string]any, returning bool) (string, error) {
|
||||||
return InsertManySQL(s, []map[string]any{data})
|
return InsertManySQL(s, []map[string]any{data}, returning)
|
||||||
}
|
}
|
||||||
|
|||||||
8
query.go
8
query.go
@@ -16,12 +16,12 @@ func Drop(s *schema.Table) (string, error) {
|
|||||||
return build.DropSQL(s)
|
return build.DropSQL(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InsertMany(s *schema.Table, data []map[string]any) (string, error) {
|
func InsertMany(s *schema.Table, data []map[string]any, returning bool) (string, error) {
|
||||||
return build.InsertManySQL(s, data)
|
return build.InsertManySQL(s, data, returning)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Insert(s *schema.Table, data map[string]any) (string, error) {
|
func Insert(s *schema.Table, data map[string]any, returning bool) (string, error) {
|
||||||
return build.InsertSQL(s, data)
|
return build.InsertSQL(s, data, returning)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Update(s *schema.Table, data map[string]any, where string) (string, error) {
|
func Update(s *schema.Table, data map[string]any, where string) (string, error) {
|
||||||
|
|||||||
@@ -14,7 +14,23 @@ func ValidateField(field *Field) error {
|
|||||||
if err := ValidateHashAlgorithm(field); err != nil {
|
if err := ValidateHashAlgorithm(field); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return ValidateFieldType(field)
|
if err := ValidateAttributes(field); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ValidateFieldType(field); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValidateAttributes(field *Field) error {
|
||||||
|
// Check if the field has keys that are necessary for the field
|
||||||
|
for _, attribute := range requiredAttributes {
|
||||||
|
if _, ok := map[string]any{field.Name: field.Type}[attribute]; !ok {
|
||||||
|
return fmt.Errorf("attribute %s is required", attribute)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateFieldType(field *Field) error {
|
func ValidateFieldType(field *Field) error {
|
||||||
@@ -42,8 +58,7 @@ func ValidateHashAlgorithm(field *Field) error {
|
|||||||
|
|
||||||
func (t *Table) Validate() error {
|
func (t *Table) Validate() error {
|
||||||
for _, field := range t.Schema {
|
for _, field := range t.Schema {
|
||||||
err := ValidateField(&field)
|
if err := ValidateField(&field); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user