Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

matheusgomes28
Copy link
Owner

TODO desc

Comment on lines +191 to +218
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
}
Copy link
Contributor

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.

Copy link
Owner Author

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!

Comment on lines +17 to +23
<<<<<<< HEAD
GetPosts(int, int) ([]common.Post, error)
=======

// Post related stuff
GetPosts() ([]common.Post, error)
>>>>>>> 446f776 (Adding basic support for flexible cards)
Copy link
Contributor

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)
Copy link
Contributor

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.

Comment on lines +262 to +283
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
}
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants