Elffuss BraidHub

kikocis/EDC public

main · 4.6 KiBRenderedRawPermalink
1# Custom validation framework
2
3The validation framework hooks into the normal Jetty/Jersey request dispatch mechanism and is designed to allow users to
4intercept the request chain to perform additional validation tasks. In its current form it is intended for intercepting
5REST requests. Users can elect any validation framework they desire, such as `jakarta.validation` or
6the [Apache Commons Validator](https://commons.apache.org/proper/commons-validator/), or they can implement one
7themselves.
8
9## When to use it
10
11This feature is intended for use cases where the standard DTO validation, that ships with EDC's APIs is not sufficient.
12Please check out the [OpenAPI spec](../../resources/openapi/yaml) to find out more about the object schema.
13
14EDC features various data types that do not have a strict schema but are *extensible*, for example `Asset`/`AssetDto`,
15or a `DataRequest`/`DataRequestDto`. This was done by design, to allow for maximum flexibility and openness. However,
16users may still want to put a more rigid schema on top of those data types, for example a use case may require an
17`Asset` to always have a `owner` property or may require a `contentType` to be present. The standard EDC validation
18scheme has no way of enforcing that, so this is where _custom validation_ enters the playing field.
19
20## Building blocks
21
22There are two important components necessary for custom validation:
23
24- the `InterceptorFunction`: a function that accepts the intercepted method's parameters as argument (as `Object[]`),
25 and returns a `Result<Void>` to indicate the validation success. It **must not** throw an exception, or dispatch to
26 the target resource is not guaranteed.
27- the `ValidationFunctionRegistry`: all `InterceptorFunctions` must be registered there, using one of three registration
28 methods (see below).
29
30Custom validation works by supplying an `InterceptorFunction` to the `ValidationFunctionRegistry` in one of the
31following ways:
32
331. bound to a resource-method: here, we register the `InterceptorFunction` to any of a controller's methods. That means,
34 we need compile-time access to the controller class, because we use reflection to obtain the `Method`:
35 ```java
36 var method = YourController.class.getDeclaredMethods("theMethod", /*parameter types*/)
37 var yourFunction = objects -> Result.success(); // you validation logic goes here
38 registry.addFunction(method, yourFunction);
39 ```
40 Consequently `yourFunction` will get invoked before `YourController#theMethod` is invoked by the request dispatcher.
41 Note that there is currently no way to bind an `InterceptorFunction` directly to an HTTP endpoint.
42
432. bound to an argument type: the interceptor function gets bound to all resource methods that have a particular type in
44 their signature:
45 ```java
46 var yourFunction = objects -> Result.success(); // your validation logic goes here
47 registry.addFunction(YourObjectDto.class, yourFunction);
48 ```
49 The above function would therefore get invoked in all controllers on the classpath, that have a `YourObjectDto`
50 in their signature, e.g. `public void createObject(YourObjectDto dto)` and `public boolean deleteObject
51 (YourObjectDto dto)` would both get intercepted, even if they are defined in different controller classes.
52 *This is the recommended way in the situation described above - adding additional schema restrictions on extensible
53 types*
54
553. globally, for all resource methods: this is intended for interceptor functions that should get invoked on *all*
56 resource methods. *This is generally not recommended and should only be used in very specific situations such as
57 logging*
58
59Please check
60out [this test](../../extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/validation/integrationtest/ValidationIntegrationTest.java)
61for a comprehensive example how validation can be enabled. All functions are registered during the extension's
62initialization phase.
63
64## Limitations and caveats
65
66- `InterceptorFunction` objects **must not** throw exceptions
67- all function registration must happen during the `initialize` phase of the extension lifecycle.
68- interceptor functions **should not** perform time-consuming tasks, such as invoking other backend systems, so as not
69 to cause timeouts in the request chain
70- for method-based interception compile-time access to the resource is required. This might not be suitable for a lot of
71 situations.
72- returning a `Result.failure(...)` will result in an `HTTP 400 BAD REQUEST` status code. This is the only supported
73 status code at this time. Note that the failure message will be part of the HTTP response body.
74- binding methods directly to paths ("endpoints") is not supported.