feat(validation): Add check for necessary keys in field json

This commit is contained in:
Björn Benouarets
2025-11-06 01:01:15 +01:00
parent cd757c80f5
commit 1f5f07e624
3 changed files with 29 additions and 10 deletions

View File

@@ -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)
} }

View File

@@ -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) {

View File

@@ -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
} }
} }