Skip to content

Conversation

@StefanHenke
Copy link
Contributor

Describes the basic feature set of the new @assert annotation. The full blown CXN documentation is pending and will be provided by Patrice later. Once available, we will link it here. That´s why the expressions in the examples are kept simple by intention.

Copy link
Contributor

@agoerler agoerler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please introduce in smaller steps

  • one case
  • multiple cases
  • error function

cosider using ? :

Please add some examples for moderately more complex expressions.

Like, e.g.

  • "if an order has items the order's total amount must be larger then 0"
  • "a book's author's name must not be null"

Co-authored-by: Adrian Görler <adrian.goerler@sap.com>
@StefanHenke
Copy link
Contributor Author

Please introduce in smaller steps

  • one case
  • multiple cases
  • error function

cosider using ? :

Please add some examples for moderately more complex expressions.

Like, e.g.

  • "if an order has items the order's total amount must be larger then 0"
  • "a book's author's name must not be null"

What exactly do you mean with consider using ? :`?

We agreed with Johannes and Patrice yesterday that we will keep the expressions simple here and wait for the CXL docs. But I can check for a "moderately more complex expression".

@rjayasinghe
Copy link
Contributor

Please introduce in smaller steps

  • one case
  • multiple cases
  • error function

cosider using ? :
Please add some examples for moderately more complex expressions.
Like, e.g.

  • "if an order has items the order's total amount must be larger then 0"
  • "a book's author's name must not be null"

What exactly do you mean with consider using ? :`?

We agreed with Johannes and Patrice yesterday that we will keep the expressions simple here and wait for the CXL docs. But I can check for a "moderately more complex expression".

I think @agoerler meant that we split up this single block into 3 examples. After each example we explain the nature of the used expression.

@StefanHenke
Copy link
Contributor Author

Please introduce in smaller steps

  • one case
  • multiple cases
  • error function

cosider using ? :
Please add some examples for moderately more complex expressions.
Like, e.g.

  • "if an order has items the order's total amount must be larger then 0"
  • "a book's author's name must not be null"

What exactly do you mean with consider using ? :`?
We agreed with Johannes and Patrice yesterday that we will keep the expressions simple here and wait for the CXL docs. But I can check for a "moderately more complex expression".

I think @agoerler meant that we split up this single block into 3 examples. After each example we explain the nature of the used expression.

Yes, I have split up the examples already. But was not sure if ?: is something in addition

Copy link
Contributor

@patricebender patricebender left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 As Adrian stated, it would be nice to have a little more (complex) real life scenarios. I would have probably taken the well known Bookshop for showcasing the different scenarios. But that may be my personal taste.

@patricebender
Copy link
Contributor

What exactly do you mean with consider using ? :`?

You can use the ternary operator as syntactic sugar for the clumsy case … when … then … statements:

> cds.parse.xpr`1 > 2 ? true : false`
[
  'case',         'when',
  { val: 1 },     '>',
  { val: 2 },     'then',
  { val: true },  'else',
  { val: false }, 'end'
]

@agoerler
Copy link
Contributor

agoerler commented Nov 21, 2025

What exactly do you mean with consider using ? :`?

You can use the ternary operator as syntactic sugar for the clumsy case … when … then … statements:

> cds.parse.xpr`1 > 2 ? true : false`
[
  'case',         'when',
  { val: 1 },     '>',
  { val: 2 },     'then',
  { val: true },  'else',
  { val: false }, 'end'
]

@patricebender - could you also omit the :-clause and it would default to else null ? E.g.

size > 100 ? 'size must be below 100'

@StefanHenke
Copy link
Contributor Author

What exactly do you mean with consider using ? :`?

You can use the ternary operator as syntactic sugar for the clumsy case … when … then … statements:

> cds.parse.xpr`1 > 2 ? true : false`
[
  'case',         'when',
  { val: 1 },     '>',
  { val: 2 },     'then',
  { val: true },  'else',
  { val: false }, 'end'
]

@patricebender - could you also omit the :-clause and it would default to else null ? E.g.

size > 100 ? 'size must be below 100'

That makes sense. When I tested it, compiler still required the default else null a few weeks back.

StefanHenke and others added 2 commits November 21, 2025 08:19
Co-authored-by: Patrice Bender <patrice.bender@sap.com>
@StefanHenke
Copy link
Contributor Author

👍 As Adrian stated, it would be nice to have a little more (complex) real life scenarios. I would have probably taken the well known Bookshop for showcasing the different scenarios. But that may be my personal taste.

I have improved the examples to use Bookshop related entities. Regarding the ternary operator, we still have to put the : else as the compiler complains otherwise.

@agoerler
Copy link
Contributor

I would like to suggest that you introduce a few sub-sections for

  • condition
  • error messages
  • message targets

@StefanHenke
Copy link
Contributor Author

After incorporating the feedback, I´m asking for another (hopefully final) round of review to the changes approved.

Copy link
Contributor

@agoerler agoerler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should mention that @assert should be used on service level.

entity Authors : cuid, managed {
@assert: (case
when dateOfBirth > dateOfDeath then error('error.author.date', (dateOfBirth, dateOfDeath), (dateOfBirth, dateOfDeath))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error function will not be release 2025/12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.