-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding basic support for flexible cards #39
base: main
Are you sure you want to change the base?
Conversation
59df586
to
446f776
Compare
446f776
to
ced30a3
Compare
if image_location == "" { | ||
return fmt.Errorf("cannot have image") | ||
} | ||
image_stat, err := os.Stat(image_location) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("file does not exist: %s", image_location) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
if image_stat.IsDir() { | ||
return fmt.Errorf("given path is a directory: %s", image_location) | ||
} | ||
|
||
// Load the schema | ||
// TODO : probably pass the schema data instead | ||
if json_data == "" { | ||
return fmt.Errorf("cannot have empty data") | ||
} | ||
|
||
if schema_name == "" { | ||
return fmt.Errorf("cannot have an empty schema name") | ||
} | ||
|
||
_, err = validateJson(json_data, schema_name) | ||
if err != nil { | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All this validation should happen in the service. The database should only persist the valid data to the database. Separating business and infrastructure (i.e. database operations) logic is key to having a clean architecture.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to continue working on this. I did some poc work during a stream and forgot to finish it off!
<<<<<<< HEAD | ||
GetPosts(int, int) ([]common.Post, error) | ||
======= | ||
|
||
// Post related stuff | ||
GetPosts() ([]common.Post, error) | ||
>>>>>>> 446f776 (Adding basic support for flexible cards) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be fixed.
} | ||
|
||
// Validate the json | ||
validateJson(card.JsonData, card.SchemaName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why you need to validate the data after loading it from the database? If the data was validated when storing it to the database then validating it again should be obsolete, unless the configured schema has changed. And by this point the data in the database is broken and should be deleted anyways.
func validateJson(json_data string, schema_name string) (bool, error) { | ||
schema_data, err := os.ReadFile(filepath.Join("schemas", fmt.Sprintf("%s.json", schema_name))) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
schemaLoader := gojsonschema.NewBytesLoader(schema_data) | ||
documentLoader := gojsonschema.NewStringLoader(json_data) | ||
result, err := gojsonschema.Validate(schemaLoader, documentLoader) | ||
if err != nil { | ||
return false, fmt.Errorf("could not read json_data: %v", err) | ||
} | ||
if !result.Valid() { | ||
json_err := fmt.Errorf("invalid card json: ") | ||
for _, e := range result.Errors() { | ||
json_err = fmt.Errorf("%v %s", json_err, e) | ||
} | ||
return false, json_err | ||
} | ||
|
||
return true, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this should be in a separate package for handling cards and the related business logic.
TODO desc