Skip to content

Commit ebcaa5d

Browse files
Review of @Assert docs (#2292)
Co-authored-by: Stefan Henke <stefan.henke@sap.com>
1 parent 41bdca6 commit ebcaa5d

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

guides/providing-services.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ The `@assert.target` check constraint relies on database locks to ensure accurat
941941

942942
### `@assert` <Beta/>
943943

944-
Annotate an _element_ with `@assert` to define the CXL expressions that are validated _after_ the data has been written to the database but _before_ it is committed. If the validation fails, the expression will return a `String` that indicates an error to the runtime. If the validation passes the expression return `null`
944+
Annotate an element with `@assert` to define CXL expressions that are validated _after_ the data has been written to the database but _before_ it is committed it. If validation fails, the expression returns a `String` that indicates an error to the runtime. If validation passes, the expression returns `null`.
945945

946946
```cds
947947
entity OrderItems : cuid {
@@ -953,7 +953,7 @@ entity OrderItems : cuid {
953953
}
954954
```
955955

956-
Alternatively, the same condition can be simplified by using the [ternary conditional operator](../releases/archive/2023/march23#ternary-conditional-operator):
956+
You can simplify the same condition by using the [ternary conditional operator](../releases/archive/2023/march23#ternary-conditional-operator):
957957

958958
```cds
959959
entity OrderItems : cuid {
@@ -965,23 +965,23 @@ entity OrderItems : cuid {
965965

966966
#### Error Messages and Message Targets
967967

968-
If a validation fails, the transaction is rolled back with an exception. If you use [Fiori draft state messages](../advanced/fiori#validating-drafts) the error is persisted. The error targets the annotated element, which is then highlighted on the Fiori UI.
968+
In general, if validation fails, the transaction is rolled back with an exception. But, if you use [Fiori draft state messages](../advanced/fiori#validating-drafts), the error is persisted. The error targets the annotated element, which is then highlighted on the Fiori UI.
969969

970970
::: info Error Messages
971-
The error message returned by the CXL expression inside the annotation can be either a static message or a message key to support i18n. If a message key is used, the message is looked up in the message bundle of the service.
972-
[Learn more about localized messages](./i18n){.learn-more}
971+
The CXL expression in the annotation can return either a static error message or a message key to support i18n. If you use a message key, the message is looked up in the message bundle of the service.
972+
[Learn more about localized messages.](./i18n){.learn-more}
973973
:::
974974

975975

976976
#### Complex Asserts
977977

978978
::: warning Use complex asserts on service layer
979-
Like other annotations, `@assert` is propagated to projections. If you annotate an element with `@assert` and the condition uses other elements - from the same or an associated entity - you must ensure that these elements are available in all projections to which the annotated element is propagated. Otherwise the CDS model won't compile.
979+
Like other annotations, `@assert` is propagated to projections. If you annotate an element with `@assert` and the condition uses other elements from the same or an associated entity, you must ensure that these elements are available in all projections to which the annotated element is propagated. Otherwise the CDS model won't compile.
980980

981981
It is therefore recommended to use complex asserts on the highest projection, that is on the service layer.
982982
:::
983983

984-
For the examples given in this section, consider the following CDS _domain_ and _service_ model:
984+
For the examples given in this section, consider the following _domain_ and _service_ model:
985985

986986
```cds
987987
context db {
@@ -1009,14 +1009,15 @@ service OrderService {
10091009
}
10101010
```
10111011

1012-
An `@assert` annotation may use other elements from the same entity. This annotation checks that the delivery date of an order is after the order date:
1012+
An `@assert` annotation can use other elements from the same entity. This annotation checks that the delivery date of an order is after the order date:
10131013

10141014
```cds
10151015
annotate OrderService.Orders with {
10161016
deliveryDate @assert: (deliveryDate < orderDate ? 'DELIVERY_BEFORE_ORDER' : null); // [!code highlight]
10171017
}
10181018
```
1019-
In an `@assert` condition you can also refer to elements of associated entities. The following example ensures that the `quantity` of the ordered book is validated against the actual `stock`. If the stock level is insufficient, a static error message is returned:
1019+
1020+
In an `@assert` condition, you can also refer to elements of associated entities. The following example validates the `quantity` of the ordered book against the actual `stock`. If the stock level is insufficient, a static error message is returned:
10201021

10211022
```cds
10221023
annotate OrderService.OrderItems with {
@@ -1052,11 +1053,11 @@ annotate OrderService.OrderItems with {
10521053

10531054
#### Background
10541055

1055-
Expressions are evaluated *after* the request has been applied to the underlying datastore. Affected are the entities of the request's payload. The runtime executes check-statements with the provided expressions and the primary key values for the given entities.
1056+
The system evaluates expressions after it applies the request to the underlying datastore. This affects the entities in the request's payload. The runtime executes check statements with the provided expressions and the primary key values for the given entities.
10561057

10571058
::: warning Limitations
10581059
- All primary key fields need to be contained in the CQN statement for validations to be enforced (including deep insert and deep update).
1059-
- Only elements with simple types (like, `String`, `Integer`, `Boolean`) can be annotated with `@assert`. Elements typed with structured or arrayed types are not supported.
1060+
- Only elements with simple types (like `String`, `Integer`, `Boolean`) can be annotated with `@assert`. Elements typed with structured or arrayed types are not supported.
10601061
:::
10611062

10621063
### Custom Error Messages

0 commit comments

Comments
 (0)