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

feat: support context in errors formatter #554

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

Conversation

smacker
Copy link
Contributor

@smacker smacker commented Aug 21, 2024

Hello!

This is a follow up for #520 to allow localization of the error messages.

My idea is that we can't assume any particular implementation for the localization, instead we can provide an interface that would let developers to take the language from context and apply any transformations to the errors they see fit.

From the API level we expose one more hook:

// ErrorFormatterContext is a function that will be used instead of ErrorFormatter
// to format an error message when defined
var ErrorFormatterContext func(ctx Context, format string, a ...any) string = nil

To apply the translation a developer might:

  1. Add a middleware to read Accept-Language header, validate it is a supported language and put the value into Context.
  2. Define ErrorFormatterContext, e.g. (simplified)
import (
	"embed"
	"strings"

	"github.com/nicksnyder/go-i18n/v2/i18n"
	"golang.org/x/text/language"
)

var bundle *i18n.Bundle

func init() {
	bundle = i18n.NewBundle(language.English)
	_, _ = bundle.LoadMessageFileFS(localeFS, "i18n/en.json")
	_, _ = bundle.LoadMessageFileFS(localeFS, "i18n/ja.json")
}

huma.ErrorFormatterContext = func(ctx Context, format string, a ...any) string {
	localizer := i18n.NewLocalizer(bundle, middleware.GetLanguage(ctx))
	localizedMessage, _ := localizer.Localize(&i18n.LocalizeConfig{
		MessageID:    format,
		TemplateData: a,
	})
	return localizedMessage
}

To keep backward compatibility and to avoid introducing performance regressions the code is a bit boilerplaty. Please let me know what you think about such approach. I didn't fully cover it with tests yet, want to get your feedback first.

Below are the results of the benchmark on my machine (please keep in mind I had to disable few tests because they are failing on main branch):

BenchmarkValidate/bool-12      	270200919	         4.446 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int-12       	178897509	         6.574 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_float64-12         	161212857	         6.613 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_int8-12            	184066688	         6.551 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_int16-12           	190347226	         6.290 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_int32-12           	167603607	         6.566 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_int64-12           	183883692	         6.580 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_uint-12            	176594246	         6.555 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_uint8-12           	180268419	         7.428 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_uint16-12          	179324629	         6.532 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_uint32-12          	183534787	         6.515 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int_from_uint64-12          	183638134	         6.513 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/float64_from_int-12         	199660590	         6.010 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/float64_from_float32-12     	191371929	         6.072 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/int64-12                    	184026264	         6.573 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/minimum-12                  	183218635	         6.534 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/exclusive_minimum-12        	184018750	         6.538 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/maximum-12                  	183950544	         6.539 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/exclusive_maximum-12        	181728464	         6.552 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/multiple_of-12              	97737116	        12.23 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/string-12                   	196833898	         6.053 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/min_length-12               	162912882	         7.396 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/max_length-12               	162739723	         7.425 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/non_ascii_max_length-12     	100000000	        11.06 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/pattern-12                  	27916633	        41.54 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/datetime-12                 	27612126	        43.22 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/datetime_string-12          	27862994	        42.90 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/date-time-http-12           	 5110598	       228.4 ns/op	     163 B/op	       4 allocs/op
BenchmarkValidate/date-12                     	20342314	        59.22 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/time-12                     	 5154030	       233.7 ns/op	     144 B/op	       6 allocs/op
BenchmarkValidate/email-12                    	 6468878	       183.7 ns/op	      96 B/op	       5 allocs/op
BenchmarkValidate/hostname-12                 	 4481942	       270.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/idn-hostname-12             	162504363	         7.380 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/ipv4-12                     	45344332	        26.33 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/ipv6-12                     	18462342	        65.40 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/uri-12                      	 9973724	       119.5 ns/op	     144 B/op	       1 allocs/op
BenchmarkValidate/uuid-12                     	57361718	        20.81 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/uuid_success_prefix-12      	45659420	        25.67 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/uuid_success_braces-12      	57278894	        21.17 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/uritemplate-12              	 2202934	       535.3 ns/op	     177 B/op	       2 allocs/op
BenchmarkValidate/jsonpointer-12              	 8444362	       140.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/rel_jsonpointer-12          	22817845	        50.46 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/regex-12                    	 1000000	      1132 ns/op	    2424 B/op	      35 allocs/op
BenchmarkValidate/base64_byte-12              	14051227	        84.51 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/base64_string-12            	14230352	        84.12 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array-12                    	30504241	        39.35 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#01-12                 	23295598	        50.62 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#02-12                 	25144251	        48.23 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#03-12                 	24682659	        49.23 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#04-12                 	24420728	        49.62 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#05-12                 	23881963	        50.27 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#06-12                 	23479976	        50.17 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#07-12                 	23197987	        50.40 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#08-12                 	24260210	        49.28 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#09-12                 	23804269	        50.23 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/array#10-12                 	16736858	        70.46 ns/op	      12 B/op	       3 allocs/op
BenchmarkValidate/array#11-12                 	16053108	        75.94 ns/op	      24 B/op	       3 allocs/op
BenchmarkValidate/min_items-12                	65701222	        18.00 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/max_items-12                	66922114	        18.08 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/unique-12                   	 7690896	       156.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/map-12                      	18823540	        63.78 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/map_any-12                  	19147344	        62.30 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/map_any_int-12              	10234318	       116.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/map_minProps-12             	25853199	        46.37 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/map_any_minProps-12         	26218141	        45.28 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/map_maxProps-12             	26026839	        46.12 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/map_any_maxProps-12         	26390905	        45.22 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/json.RawMessage-12          	293788394	         4.080 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/object_struct-12            	122741674	         9.802 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/object_struct_any-12        	100000000	        10.07 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/object_optional-12          	50584797	        23.76 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/object_any_optional-12      	50881323	        23.41 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/readOnly_set-12             	199912593	         5.980 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/readOnly_any_set-12         	199883401	         5.986 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/readOnly_missing-12         	50041440	        23.79 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/readOnly_any_missing-12     	50294958	        23.69 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/nested-12                   	 7614474	       155.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/nested_any-12               	 5440443	       221.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/enum_string-12              	122468490	         9.801 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/enum_int-12                 	142374466	         8.447 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/enum_uint16-12              	142445456	         8.475 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/enum_array-12               	50447810	        24.57 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/optional-12                 	48244921	        24.02 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/optional_null-12            	47455761	        23.96 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/optional_any_null-12        	49453860	        23.64 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/dependentRequired_empty-12  	50979675	        25.24 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/dependentRequired_filled-12 	199451079	         6.006 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/dependentRequired_ignored-12         	50559667	        23.45 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/dependentRequired_empty_success_any-12         	50454794	        23.52 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/dependentRequired_filled_success_any-12        	200792670	         6.003 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidate/dependentRequired_ignored_success_any-12       	49787020	        24.23 ns/op	      48 B/op	       1 allocs/op
BenchmarkValidate/pointer_required_field-12                      	17407976	        66.04 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/bool-12                                 	275868912	         4.364 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int-12                                  	183983472	         6.543 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_float64-12                     	184027334	         6.623 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_int8-12                        	182789446	         6.534 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_int16-12                       	192011558	         6.289 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_int32-12                       	183929068	         6.526 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_int64-12                       	183563646	         6.527 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_uint-12                        	183101719	         6.522 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_uint8-12                       	183503845	         6.522 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_uint16-12                      	183333051	         6.527 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_uint32-12                      	183707679	         6.534 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int_from_uint64-12                      	183834288	         6.605 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/float64_from_int-12                     	200769546	         6.002 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/float64_from_float32-12                 	200920788	         5.985 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/int64-12                                	183533512	         6.539 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/minimum-12                              	183799303	         6.527 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/exclusive_minimum-12                    	184067876	         6.514 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/maximum-12                              	183705043	         6.516 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/exclusive_maximum-12                    	182966852	         6.516 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/multiple_of-12                          	98415445	        12.27 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/string-12                               	200753550	         5.982 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/min_length-12                           	162586975	         7.343 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/max_length-12                           	157070572	         7.347 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/non_ascii_max_length-12                 	100000000	        11.00 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/pattern-12                              	28508274	        41.46 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/datetime-12                             	26376886	        42.91 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/datetime_string-12                      	27723122	        43.12 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/date-time-http-12                       	 5288682	       224.7 ns/op	     163 B/op	       4 allocs/op
BenchmarkValidateContext/date-12                                 	20100193	        58.14 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/time-12                                 	 5202078	       233.0 ns/op	     144 B/op	       6 allocs/op
BenchmarkValidateContext/email-12                                	 6594186	       191.2 ns/op	      96 B/op	       5 allocs/op
BenchmarkValidateContext/hostname-12                             	 4522002	       268.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/idn-hostname-12                         	156448912	         7.901 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/ipv4-12                                 	40455290	        27.06 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/ipv6-12                                 	18171286	        64.81 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/uri-12                                  	 9853665	       121.9 ns/op	     144 B/op	       1 allocs/op
BenchmarkValidateContext/uuid-12                                 	56786814	        20.96 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/uuid_success_prefix-12                  	46549666	        25.61 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/uuid_success_braces-12                  	56421468	        21.05 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/uritemplate-12                          	 2197502	       543.0 ns/op	     177 B/op	       2 allocs/op
BenchmarkValidateContext/jsonpointer-12                          	 8543415	       142.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/rel_jsonpointer-12                      	23096521	        50.20 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/regex-12                                	 1000000	      1143 ns/op	    2424 B/op	      35 allocs/op
BenchmarkValidateContext/base64_byte-12                          	13531659	        84.70 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/base64_string-12                        	14082234	        87.99 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array-12                                	30256105	        40.55 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#01-12                             	23956658	        50.46 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#02-12                             	24133093	        47.76 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#03-12                             	24754857	        48.57 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#04-12                             	24331578	        49.79 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#05-12                             	23897241	        50.31 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#06-12                             	23345091	        50.14 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#07-12                             	23053130	        51.32 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#08-12                             	24015649	        49.16 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#09-12                             	23118732	        50.38 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/array#10-12                             	17091866	        70.83 ns/op	      12 B/op	       3 allocs/op
BenchmarkValidateContext/array#11-12                             	15752286	        73.96 ns/op	      24 B/op	       3 allocs/op
BenchmarkValidateContext/min_items-12                            	63409446	        18.56 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/max_items-12                            	65683393	        18.25 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/unique-12                               	 7648353	       156.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/map-12                                  	18714471	        63.97 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/map_any-12                              	19236163	        62.35 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/map_any_int-12                          	10162009	       118.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/map_minProps-12                         	26044821	        46.40 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/map_any_minProps-12                     	26580452	        45.12 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/map_maxProps-12                         	26371210	        46.49 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/map_any_maxProps-12                     	26802914	        45.25 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/json.RawMessage-12                      	292729602	         4.085 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/object_struct-12                        	121021155	         9.839 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/object_struct_any-12                    	100000000	        10.07 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/object_optional-12                      	21694310	        55.96 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/object_any_optional-12                  	21439066	        55.58 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/readOnly_set-12                         	200953846	         6.013 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/readOnly_any_set-12                     	194935348	         6.265 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/readOnly_missing-12                     	20954079	        55.58 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/readOnly_any_missing-12                 	21316279	        55.19 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/nested-12                               	 7760514	       157.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/nested_any-12                           	 5414528	       220.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/enum_string-12                          	121845676	         9.969 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/enum_int-12                             	142164162	         8.458 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/enum_uint16-12                          	142403752	         8.559 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/enum_array-12                           	51112314	        23.04 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/optional-12                             	21297049	        56.40 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/optional_null-12                        	21250863	        55.66 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/optional_any_null-12                    	21761830	        55.74 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/dependentRequired_empty-12              	21646290	        55.69 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/dependentRequired_filled-12             	200456008	         6.035 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/dependentRequired_ignored-12            	21333601	        55.32 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/dependentRequired_empty_success_any-12  	21720079	        55.79 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/dependentRequired_filled_success_any-12 	199827259	         6.030 ns/op	       0 B/op	       0 allocs/op
BenchmarkValidateContext/dependentRequired_ignored_success_any-12         	21527205	        55.80 ns/op	      64 B/op	       2 allocs/op
BenchmarkValidateContext/pointer_required_field-12                        	16223322	        67.34 ns/op	       0 B/op	       0 allocs/op

Summary by CodeRabbit

  • New Features

    • Introduced a context-aware error formatting option to enhance error message customization.
    • Improved validation processes with context utilization for better error handling and request management.
  • Bug Fixes

    • Ensured backward compatibility while transitioning to context-aware validation methods.
  • Tests

    • Added new tests to validate context-aware error formatting and benchmark performance within the validation framework.

Copy link

coderabbitai bot commented Aug 21, 2024

Walkthrough

The changes across the codebase enhance error handling and validation processes by integrating context awareness. A new variable, ErrorFormatterContext, allows for customizable error message formatting based on contextual information. Additionally, validation functions have been updated to accept a context.Context parameter, improving flexibility in managing request-scoped data. These modifications prioritize backward compatibility while providing developers with improved tools for context-aware operations.

Changes

Files Change Summary
error.go Introduced ErrorFormatterContext for context-aware error formatting; ErrorFormatter remains unchanged.
huma.go, validate.go Updated validation functions to utilize ValidateContext with context parameters for enhanced error handling.
validate_test.go Added new tests for context-aware error formatting, including TestValidateContextFormatter and BenchmarkValidateContext.

Poem

In fields of code, we hop with glee,
New ways to format, oh so free!
Context in hand, we validate right,
Errors transformed, a joyful sight.
With every leap, our changes bloom,
Hooray for the code, dispelling the gloom! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Aug 21, 2024

Codecov Report

Attention: Patch coverage is 60.08403% with 95 lines in your changes missing coverage. Please review.

Project coverage is 90.67%. Comparing base (77c7a16) to head (2e5d9a4).

Files Patch % Lines
validate.go 59.74% 50 Missing and 45 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #554      +/-   ##
==========================================
- Coverage   92.76%   90.67%   -2.09%     
==========================================
  Files          22       22              
  Lines        3883     4033     +150     
==========================================
+ Hits         3602     3657      +55     
- Misses        236      286      +50     
- Partials       45       90      +45     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 77c7a16 and 2e5d9a4.

Files selected for processing (4)
  • error.go (1 hunks)
  • huma.go (2 hunks)
  • validate.go (20 hunks)
  • validate_test.go (3 hunks)
Additional context used
GitHub Check: codecov/patch
validate.go

[warning] 200-200: validate.go#L200
Added line #L200 was not covered by tests


[warning] 208-208: validate.go#L208
Added line #L208 was not covered by tests


[warning] 216-216: validate.go#L216
Added line #L216 was not covered by tests


[warning] 225-225: validate.go#L225
Added line #L225 was not covered by tests


[warning] 243-243: validate.go#L243
Added line #L243 was not covered by tests


[warning] 252-252: validate.go#L252
Added line #L252 was not covered by tests


[warning] 260-260: validate.go#L260
Added line #L260 was not covered by tests


[warning] 268-268: validate.go#L268
Added line #L268 was not covered by tests


[warning] 277-277: validate.go#L277
Added line #L277 was not covered by tests


[warning] 286-286: validate.go#L286
Added line #L286 was not covered by tests


[warning] 294-294: validate.go#L294
Added line #L294 was not covered by tests


[warning] 302-302: validate.go#L302
Added line #L302 was not covered by tests


[warning] 310-310: validate.go#L310
Added line #L310 was not covered by tests


[warning] 318-318: validate.go#L318
Added line #L318 was not covered by tests


[warning] 341-341: validate.go#L341
Added line #L341 was not covered by tests


[warning] 361-361: validate.go#L361
Added line #L361 was not covered by tests


[warning] 383-383: validate.go#L383
Added line #L383 was not covered by tests


[warning] 461-461: validate.go#L461
Added line #L461 was not covered by tests


[warning] 476-476: validate.go#L476
Added line #L476 was not covered by tests


[warning] 512-512: validate.go#L512
Added line #L512 was not covered by tests


[warning] 531-531: validate.go#L531
Added line #L531 was not covered by tests


[warning] 540-540: validate.go#L540
Added line #L540 was not covered by tests


[warning] 549-549: validate.go#L549
Added line #L549 was not covered by tests


[warning] 558-558: validate.go#L558
Added line #L558 was not covered by tests


[warning] 571-571: validate.go#L571
Added line #L571 was not covered by tests


[warning] 582-582: validate.go#L582
Added line #L582 was not covered by tests


[warning] 591-591: validate.go#L591
Added line #L591 was not covered by tests


[warning] 600-603: validate.go#L600-L603
Added lines #L600 - L603 were not covered by tests


[warning] 618-618: validate.go#L618
Added line #L618 was not covered by tests


[warning] 655-655: validate.go#L655
Added line #L655 was not covered by tests

Additional comments not posted (4)
error.go (1)

369-371: Addition of ErrorFormatterContext is approved.

The introduction of ErrorFormatterContext provides a flexible mechanism for context-aware error message formatting while maintaining backward compatibility.

validate.go (1)

Line range hint 186-319: Context-aware validation logic is well-implemented.

The addition of the Context parameter and the use of ErrorFormatterContext enhance the flexibility of error message formatting.

Tools
GitHub Check: codecov/patch

[warning] 200-200: validate.go#L200
Added line #L200 was not covered by tests


[warning] 208-208: validate.go#L208
Added line #L208 was not covered by tests


[warning] 216-216: validate.go#L216
Added line #L216 was not covered by tests


[warning] 225-225: validate.go#L225
Added line #L225 was not covered by tests


[warning] 243-243: validate.go#L243
Added line #L243 was not covered by tests


[warning] 252-252: validate.go#L252
Added line #L252 was not covered by tests


[warning] 260-260: validate.go#L260
Added line #L260 was not covered by tests


[warning] 268-268: validate.go#L268
Added line #L268 was not covered by tests


[warning] 277-277: validate.go#L277
Added line #L277 was not covered by tests


[warning] 286-286: validate.go#L286
Added line #L286 was not covered by tests


[warning] 294-294: validate.go#L294
Added line #L294 was not covered by tests


[warning] 302-302: validate.go#L302
Added line #L302 was not covered by tests


[warning] 310-310: validate.go#L310
Added line #L310 was not covered by tests


[warning] 318-318: validate.go#L318
Added line #L318 was not covered by tests

validate_test.go (1)

1410-1450: New tests for context-aware error formatting are well-implemented.

The TestValidateContextFormatter and BenchmarkValidateContext functions effectively validate and benchmark the context-aware error formatting.

huma.go (1)

1146-1146: Modification to use ValidateContext is approved.

The use of ValidateContext in the Register function enhances the validation process by leveraging context for request-scoped values.

Also applies to: 1254-1254

if ErrorFormatterContext == nil {
res.Add(path, v, validation.MsgExpectedMatchExactlyOneSchema)
} else {
res.Add(path, v, ErrorFormatterContext(ctx, validation.MsgExpectedMatchExactlyOneSchema))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test coverage for context-aware error formatting.

The static analysis hints indicate that some lines involving ErrorFormatterContext are not covered by tests.

Would you like me to help generate tests to cover these lines or open a GitHub issue to track this task?

Also applies to: 361-361, 383-383, 461-461, 476-476, 512-512, 531-531, 540-540, 549-549, 558-558, 571-571, 582-582, 591-591, 600-603, 618-618, 655-655

Tools
GitHub Check: codecov/patch

[warning] 341-341: validate.go#L341
Added line #L341 was not covered by tests

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.

1 participant