SAP

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Learn how to define constraints declaratively in your CDS model

With a concise collection of annotations, we can add business and techncial constraints to our domain model cleanly and declaratively.

Overview

You will learn

  • How to model constraints
  • What annotations exist for this
  • How to use them
DJ Adams D DJ Adams August 25, 2026
Created on August 25, 2026
Contributors

More from DJ Adams

See all 19 tutorials by DJ Adams →

Prerequisites

Prerequisites

You will need a development environment for CAP Node.js. See the tutorial Set up a self-contained development environment for CAP Node.js The assumptions in this tutorial are based on option 1 or option 2 in that tutorial, in that you have a VS Code (or GitHub Codespace) environment based on the foundation repository used in the setup described there, which also means that your starting directory will be /workspaces/cap-nodejs-dev-env. If you have your own CAP Node.js development environment setup, then please make the appropriate adjustments where necessary.

Steps

Intro

CDS is a great example of a declarative or “intent” based approach to defining a solution - you declare what you want, and let the system (CAP) take care of how that’s implemented. With a combination of CDL and CXL, plus annotations, we have a “solid state” language with which to define what the business and the solution requires.

In this tutorial we’ll gradually add constraints to a CDS model, to understand what they can do and how they are applied.


Step 1 Start with a simple CAP project

To keep things simple, let’s use an existing “base project” from an SAP CodeJam on CAP. It’s an extremely cut down version of the classic Northwind service, even more reduced than Northbreeze in the OData Deep Dive Server repo.

It’s called Northwhisper, with just a very small amount of product, category and supplier data.

👉 Clone the repository:

Shell
git clone https://github.com/SAP-samples/cap-tour-hands-on

👉 Now open the baseproj/ directory within the cloned repository in a new VS Code / Codespace window:

Shell
code cap-tour-hands-on/baseproj/

This should present just the base project in the Explorer, at the root.

👉 Open up a terminal window and start a CAP server running with:

Shell
cds watch
Step 2 List the existing categories
+
Step 3 Add a new category with an inappropriate name
+
Step 4 Make the category description mandatory
+
Step 5 Get to know the general @assert annotation
+
Step 6 Constrain the category name format
+
Step 7 Add a custom constraint message
+
Step 8 Make a second category creation request
+
Step 9 Wrap-up and further info
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 9
1. Start with a simple CAP project 2. List the existing categories 3. Add a new category with an inappropriate name 4. Make the category description mandatory 5. Get to know the general @assert annotation 6. Constrain the category name format 7. Add a custom constraint message 8. Make a second category creation request 9. Wrap-up and further info