Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions guides/providing-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ The `@assert.target` check constraint relies on database locks to ensure accurat

### `@assert` <Beta/>

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`
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`.

```cds
entity OrderItems : cuid {
Expand All @@ -953,7 +953,7 @@ entity OrderItems : cuid {
}
```

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

```cds
entity OrderItems : cuid {
Expand All @@ -965,23 +965,23 @@ entity OrderItems : cuid {

#### Error Messages and Message Targets

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.
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.

::: info Error Messages
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.
[Learn more about localized messages](./i18n){.learn-more}
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.
[Learn more about localized messages.](./i18n){.learn-more}
:::


#### Complex Asserts

::: warning Use complex asserts on service layer
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.
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.

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

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

```cds
context db {
Expand Down Expand Up @@ -1009,14 +1009,15 @@ service OrderService {
}
```

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:
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:

```cds
annotate OrderService.Orders with {
deliveryDate @assert: (deliveryDate < orderDate ? 'DELIVERY_BEFORE_ORDER' : null); // [!code highlight]
}
```
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:

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:

```cds
annotate OrderService.OrderItems with {
Expand Down Expand Up @@ -1052,11 +1053,11 @@ annotate OrderService.OrderItems with {

#### Background

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.
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.

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

### Custom Error Messages
Expand Down
Loading