You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/providing-services.md
+25-32Lines changed: 25 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -772,55 +772,48 @@ Here's an overview table:
772
772
- Pessimistic locking is not supported by SQLite. H2 supports exclusive locks only.
773
773
:::
774
774
775
-
## Input Validation
776
775
777
-
CAP runtimes provide generic input validation for incoming requests out of the box, based on the data types and constraints defined in CDS models. You can add custom input validation by...
776
+
## Custom Logic
778
777
779
-
-[Declarative Constraints](./services/constraints) with the following annotations:
-[Programmatic Validations](#custom-logic) in custom event handlers
778
+
As most standard tasks and use cases are covered by [generic service providers](#generic-providers), the need to add service implementation code is greatly reduced and minified, and hence the quantity of individual boilerplate coding.
787
779
788
-
> [!tip]
789
-
> Prefer declarative constraints over programmatic validations wherever possible, as they require no implementation coding and are automatically served by CAP runtimes in optimized ways.
780
+
The remaining cases that need custom handlers, reduce to real custom logic, specific to your domain and application, such as:
- Triggering follow-up actions, for example calling other services or emitting outbound events in response to inbound events
786
+
- And more... In general, all the things not (yet) covered by generic handlers
792
787
793
-
Declarative constraints allow you to express conditions using CXL expressions that are validated automatically whenever data is written, greatly reducing the need for extensive custom code for input validation.
794
788
795
-
::: tip Read the guide
796
-
Find additional information about constraints in this guide:
Status-transition flows ensure transitions are explicitly modeled, validated, and executed in a controlled and reliable way, thereby eliminating the need for extensive custom coding.
792
+
CAP supports various declarative techniques to express custom logic without coding, in particular for input validation and status-transition flows.
803
793
804
-
::: tip Read the guide
805
-
Find additional information about modeling status-transition flows in this guide:<br>
-[Status-Transition Flows](./services/status-flows.md) ensure transitions are explicitly modeled, validated, and executed in a controlled and reliable way, thereby eliminating the need for extensive custom coding.
810
797
811
798
799
+
#### Input Validation
812
800
813
-
As most standard tasks and use cases are covered by [generic service providers](#generic-providers), the need to add service implementation code is greatly reduced and minified, and hence the quantity of individual boilerplate coding.
801
+
-[Declarative Constraints](./services/constraints) allow to annotate your models and have the respective checks still be executed and enforced by generic runtimes, with the following annotations:
814
802
815
-
The remaining cases that need custom handlers, reduce to real custom logic, specific to your domain and application, such as:
> Prefer declarative constraints over programmatic validations wherever possible, as they require no implementation coding and are automatically served by CAP runtimes in optimized ways.
- Triggering follow-up actions, for example calling other services or emitting outbound events in response to inbound events
821
-
- And more... In general, all the things not (yet) covered by generic handlers
822
814
823
815
816
+
### Custom Service Providers
824
817
825
818
**In Node.js**, the easiest way to add custom implementations for services is through equally named _.js_ files placed next to a service definition's _.cds_ file:
Copy file name to clipboardExpand all lines: guides/services/constraints.md
+17-28Lines changed: 17 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -243,7 +243,7 @@ annotate TravelService.Travels with {
243
243
244
244
245
245
246
-
### `@assert.format`
246
+
### `@assert.format`
247
247
248
248
Allows you to specify a regular expression string (in ECMA 262 format in CAP Node.js and java.util.regex.Pattern format in CAP Java) that all string input must match.
249
249
@@ -254,7 +254,7 @@ entity Foo {
254
254
```
255
255
256
256
257
-
### `@assert.range`
257
+
### `@assert.range`
258
258
259
259
Allows you to specify `[ min, max ]` ranges for elements with ordinal types — that is, numeric or date/time types. For `enum` elements, `true` can be specified to restrict all input to the defined enum values.
260
260
@@ -287,32 +287,9 @@ Support for open intervals and infinity is available for CAP Node.js since `@sap
287
287
288
288
289
289
290
-
### `@assert.target`
290
+
### `@assert.target`
291
291
292
-
Annotate a [managed to-one association](../../cds/cdl#managed-associations) of a CDS model entity definition with the
293
-
`@assert.target` annotation to check whether the target entity referenced by the association (the reference's target)
294
-
exists. In other words, use this annotation to check whether a non-null foreign key input in a table has a corresponding
295
-
primary key in the associated/referenced target table.
296
-
297
-
You can check whether multiple targets exist in the same transaction. For example, in the `Books` entity, you could
298
-
annotate one or more managed to-one associations with the `@assert.target` annotation. However, it is assumed that
299
-
dependent values were inserted before the current transaction. For example, in a deep create scenario, when creating a
300
-
book, checking whether an associated author exists that was created as part of the same deep create transaction isn't
301
-
supported, in this case, you will get an error.
302
-
303
-
The `@assert.target` check constraint is meant to **validate user input** and not to ensure referential integrity.
304
-
Therefore only `CREATE`, and `UPDATE` events are supported (`DELETE` events are not supported). To ensure that every
305
-
non-null foreign key in a table has a corresponding primary key in the associated/referenced target table
306
-
(ensure referential integrity), the [`@assert.integrity`](../databases#database-constraints) constraint must be used instead.
307
-
308
-
If the reference's target doesn't exist, an HTTP response
309
-
(error message) is provided to HTTP client applications and logged to stdout in debug mode. The HTTP response body's
310
-
content adheres to the standard OData specification for an error
Add `@assert.target` annotation to the service definition as previously mentioned:
292
+
Annotate a [managed to-one association](../../cds/cdl#managed-associations) with `@assert.target` to check whether the target entity referenced by the association (the reference's target) exists for a given input.
316
293
317
294
```cds
318
295
entity Books {
@@ -328,7 +305,19 @@ entity Authors {
328
305
}
329
306
```
330
307
331
-
**HTTP Request** — *assume that an author with the ID `"796e274a-c3de-4584-9de2-3ffd7d42d646"` doesn't exist in the database*
308
+
You can check whether multiple targets exist in the same transaction. For example, in the `Books` entity, you could
309
+
annotate one or more managed to-one associations with the `@assert.target` annotation. However, it is assumed that
310
+
dependent values were inserted before the current transaction. For example, in a deep create scenario, when creating a book, checking whether an associated author exists that was created as part of the same deep create transaction isn't supported, in this case, you will get an error.
311
+
312
+
The `@assert.target` check constraint is meant to **validate user input** and not to ensure referential integrity.
313
+
Therefore only `CREATE`, and `UPDATE` events are supported (`DELETE` events are not supported). To ensure that every
314
+
non-null foreign key in a table has a corresponding primary key in the associated/referenced target table
315
+
(ensure referential integrity), the [`@assert.integrity`](../databases#database-constraints) constraint must be used instead.
316
+
317
+
If the reference's target doesn't exist, an HTTP response
318
+
(error message) is provided to HTTP client applications and logged to stdout in debug mode. The HTTP response body's
319
+
content adheres to the standard OData specification for an error
0 commit comments