Skip to content

Commit

Permalink
added default jsonSchema standardizer
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmoySG committed May 12, 2024
1 parent 819ecc8 commit 11207d8
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 5 deletions.
4 changes: 4 additions & 0 deletions internal/collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collections
import (
"github.com/TanmoySG/wunderDB/internal/metadata"
"github.com/TanmoySG/wunderDB/model"
s "github.com/TanmoySG/wunderDB/pkg/schema"
wdbErrors "github.com/TanmoySG/wunderDB/pkg/wdb/errors"
)

Expand Down Expand Up @@ -38,6 +39,9 @@ func (c Collections) CreateCollection(collectionID model.Identifier, schema mode
return err
}

// standardize schema: add schema fields if not present
schema = s.StandardizeSchema(schema)

c[collectionID] = &model.Collection{
Data: map[model.Identifier]*model.Record{},
Schema: schema,
Expand Down
4 changes: 2 additions & 2 deletions internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ func (d Data) Query(query string, mode QueryType) (interface{}, *er.WdbError) {
case JsonPathQuery:
jpqResult, err := root.JSONPath(query)
if err != nil {
fmt.Println("ths", err)
return nil, er.JSONPathQueryError.SetMessage(err.Error())
}
queryResultNodes = jpqResult
case EvaluateQuery:
evqResult, err := ajson.Eval(root, query)
if err != nil {
fmt.Println("thsd", err)
return nil, er.QueryExecutionFailed.SetMessage(err.Error())
}

queryResultNodes = []*ajson.Node{evqResult}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (wh wdbHandlers) handleTransactions(c *fiber.Ctx, apiResponse response.ApiR
txnActor := txlogs.GetTxnActor(c.Get(AuthorizationHeader))
txnAction := apiResponse.Response.Action

txnHttpDetails := txlogs.GetTxnHttpDetails(*c)
txnHttpDetails := txlogs.GetTxnHttpDetails(c) // Pass the pointer to fiber.Ctx instead of dereferencing it
txnEntityPath := txlModel.TxlogSchemaJsonEntityPath{
Database: databaseEntity,
Collection: entities.Collections,
Expand Down
2 changes: 1 addition & 1 deletion internal/txlogs/txlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func CreateTxLog(txnAction, txnActor string, txnRequestStatus string, txnEntitie
}
}

func GetTxnHttpDetails(c fiber.Ctx) txlModel.TxlogSchemaJsonTransactionDetails {
func GetTxnHttpDetails(c *fiber.Ctx) txlModel.TxlogSchemaJsonTransactionDetails {
txnHttpUrl, txnUserAgent, txnRequestIP := c.Path(), c.Get("User-Agent"), c.IP()

txnRequestHttpMethod := txlModel.TxlogSchemaJsonTransactionDetailsMethod(c.Method())
Expand Down
31 changes: 30 additions & 1 deletion pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ type Schema struct {
loadedSchema jsonschema.JSONLoader
}

var (
// update default values for required schema fields
// TODO: make this configurable at startup
requiredSchemaFields = map[string]interface{}{
"additionalProperties": false,
}
)

func UseSchema(schema model.Schema) (*Schema, *er.WdbError) {
marshaledSchemaJSON, err := json.Marshal(schema)
if err != nil {
Expand All @@ -29,6 +37,27 @@ func (s Schema) Validate(data interface{}) (bool, *er.WdbError) {
}
loadedData := jsonschema.NewStringLoader(string(marshaledDataJSON))

validity, _ := jsonschema.Validate(s.loadedSchema, loadedData)
validity, err := jsonschema.Validate(s.loadedSchema, loadedData)
if err != nil {
return false, er.SchemaValidationFailed.SetMessage(err.Error())
}

return validity.Valid(), nil
}

// adds default schema fields if not present, eg: additionalProperties [default: false]
func StandardizeSchema(schema model.Schema) model.Schema {

// return schema if schema is empty
if len(schema) == 0 {
return schema
}

for field, fieldDefaultValue := range requiredSchemaFields {
if _, ok := schema[field]; !ok {
schema[field] = fieldDefaultValue
}
}

return schema
}
9 changes: 9 additions & 0 deletions pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ func Test_Validate(t *testing.T) {
isValid, _ := js.Validate(testData)
assert.Equal(t, true, isValid)
}

func Test_StandardizeSchema(t *testing.T) {
testSchema := model.Schema{
"type": "object",
}
standardizedSchema := StandardizeSchema(testSchema)
assert.Equal(t, "object", standardizedSchema["type"])
assert.Equal(t, false, standardizedSchema["additionalProperties"])
}
13 changes: 13 additions & 0 deletions pkg/wdb/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ var (
HttpStatusCode: 406,
}

// JSONPath and Query Erros
QueryExecutionFailed = WdbError{
ErrCode: "queryExecutionFailed",
ErrMessage: "query execution failed",
HttpStatusCode: 400,
}

JSONPathQueryError = WdbError{
ErrCode: "jsonPathQueryError",
ErrMessage: "json path query failed",
HttpStatusCode: 400,
}

// Safe Name Error
CollectionNameFormatError = WdbError{
ErrCode: "entityNameFormatError",
Expand Down
31 changes: 31 additions & 0 deletions pkg/wdb/errors/modifiers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package wdbErrors

// getters for errors
func (e *WdbError) Error() string {
return e.ErrMessage
}

func (e *WdbError) StatusCode() int {
return e.HttpStatusCode
}

func (e *WdbError) Code() string {

return e.ErrCode
}

// setters for errors
func (e *WdbError) SetCode(code string) *WdbError {
e.ErrCode = code
return e
}

func (e *WdbError) SetMessage(message string) *WdbError {
e.ErrMessage = message
return e
}

func (e *WdbError) SetStatusCode(code int) *WdbError {
e.HttpStatusCode = code
return e
}

0 comments on commit 11207d8

Please sign in to comment.