-
Notifications
You must be signed in to change notification settings - Fork 5
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
Input Parsing and error handling #14
Comments
Running through some examples for input handling. In all these cases the error type is pub type Invalid(a) {
Missing(key: String)
CastFailure(key: String, help: a)
} Keep as lean as possible.In these examples all the custom // required field
try name = params.find(form, "name")
try name = params.cast(form, as_name)
// optional field
let url = params.optional(form, "url")
try url = params.cast_optional(url, url.parse) |> result.map_error(fn(_) { "not a valid url"})
// or wrapper function that handles mapping the error
try url = params.cast_optional(url, as_url)
// handling a fallback
let url = params.use_fallback(url, Uri(...)) Notes
Higher leveltry name = params.required(from: form, get: "name", cast: as_name)
try url = params.optional(from: form, get: "url", cast: as_url)
try url = params.overridable(from: form, get: "url", cast: as_url, or: "default.com") Better as_ functionsA params module could define cast functions for strings ints etc try name = params.required(form, "name", as_string(_, [MinLength(5), Pattern(), MaxLength(30)]) Comment on
|
Wrapping ErrorsInstead of wrapper functions that all return the same error type we could have a function for parse_form and then wrap the error once. fn try_parse_form(form) {
try ...
try ...
FormValues(a: ....)
}
pub fn parse_form(form) {
try_parse_form(form)
|> result.map_error()
} There will be a lot of these function pairs. because there is no way to wrap the error inside the first function. Currently I prefer writting web wrapper functions, but this could be a lot of wrapper functions and requires more up front work about choosing an error type. Application Error typehandle functions return It could a big enum type ErrorResponse {
UnprocessableEntity(input.Invalid())
}
fn parse_form(form) {
try_parse_form(form)
// big enum
|> result.map_error(UnprocessableEntity)
} There is no completeness to assure that Enum is used correctly. Or it could be a special Error type type ErrorResponse {
ErrorResponse(type: ErrorType, details: String)
} can enum all the error types, has only a single place to set the details. |
@lpil thanks for the feedback on the PR, I have merged version 1 to play with will see how it goes. I particularly think it might make sense to work on maps because although params come in order the api as designed so far doesn't allow you to pull out a value that is defined multiple times |
Perhaps we could move forward with this and a more complex validation library can be left as an exercise for someone else. Getting 90% of the way there with everything provides the most value now! Thanks |
Add an example endpoint that parses input.
This will probably make use of https://github.com/rjdellecese/gleam_decode and ideally the approach would be reusable for multiple input sources. e.g. query strings, JSON, forms
The text was updated successfully, but these errors were encountered: