-
Notifications
You must be signed in to change notification settings - Fork 114
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
ReferenceChecker don't get passed in a propper context #192
Comments
@smyrman I think backward-compatibility doesn't need to be a problem at this stage. Moreover we don't know how many users rest-layer has, and maybe such breaking change is a good way to figure out which are the users. To me, adding a good comments in the code/docs is sufficient. IMO This is very needed feature indeed, having Context in the hooks. |
+1 for breaking change and have the right interface. |
I thing the right interface if we are going to break things anyway, looks more or less like this: type Validator interface{
Validate(ctx context.Context, value, original interface{}) (interface{}, error)
} It would replace both the current There are also good cases for FieldValidators where the original (or base) value is of interest for validation, e.g. for correctly performing read-only checks for FieldValidators containing sub-schemas of some sort, such as For reference, the current // Validator is an interface used to validate schema against actual data.
type Validator interface {
GetField(name string) *Field
Prepare(ctx context.Context, payload map[string]interface{}, original *map[string]interface{}, replace bool) (changes map[string]interface{}, base map[string]interface{})
Validate(changes map[string]interface{}, base map[string]interface{}) (doc map[string]interface{}, errs map[string][]interface{})
} The
We need to see if it's practical to merge |
Will close issues that relates to a broader redesign of the schema package (such as this one), leaving only #77 open. |
Reopen as a bug; no matter if w are able to solve this easily in the current design, I guess we need to still track it. |
Code:
rest-layer/resource/index.go
Line 132 in 6d75064
The ReferenceChecker does not get passed in a request-scoped context (or any context for that matter), which means that any hooks that might run OnGet / OnGot for the resource in question don't get passed the relevant request context.
use-cases
There are many good use-cases for wanting to access the correct context in a hook.
One use-case, is permission handling, when it's reasonable to assume that the context may contain some user or permission related information.
Fix: pass in context to FieldValidator
The obvious fix may be to change the
shema.FieldValidator
interface to accept a context to be passed to the validator function. This is possile to do as a backwards-compatible change as well, as one could add a new interface,schema.CtxFieldValidator
or so, to take precedence overschema.FieldValidaotor
in type checks, but not sure the backwards-compatible path here is worth it before the v1.0 release.There may be other changes that could be done to pass along the right context that isn't yet clear to me, but having the opurtunity to pass in context to a FieldValidator, probably makes senes anyway...
The text was updated successfully, but these errors were encountered: