ERPClimb logoERPClimb

Code examples · SAP BTP · Beginner

SAP BTP CAP: validate input with annotations, no code

The problem

A product service must refuse a product without a name, a price outside 0 to 10,000, a product code that does not look like ABC-1234, and any attempt to set the stock level from outside.

How we solve it

  1. 1Data modelplain entity
  2. 2Rulesannotations
  3. 3Try itvalid and invalid

The result

Valid products are created.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A product service must refuse a product without a name, a price outside 0 to 10,000, a product code that does not look like ABC-1234, and any attempt to set the stock level from outside.

2. How we design it

Most simple input rules are declared, not programmed. CAP has annotations for required fields, number ranges, text formats and read-only fields. We put them on the service entity and CAP checks every request against these rules automatically. Handlers stay for the rules that need logic.

Why we decided it this way

  1. 1Annotations on the service, not on the database entityDifferent services can apply different rules to the same data.
  2. 2@mandatory, @assert.range, @assert.formatThree common rules with no code and a clear 400 answer.
  3. 3@readonly for the stock levelClients cannot write the field; only your own code can.

Before you start

  • Node.js 20+ and @sap/cds-dk installed.
  • An empty project: cds init products-demo.

3. The code, step by step

Step 1 of 3: Define the entity

A product with the four fields we want to protect.

CDS · db/schema.cds
// Product entity
using { cuid } from '@sap/cds/common';
namespace demo.products;

entity Products : cuid {
  code  : String(8);
  name  : String(80);
  price : Decimal(9, 2);
  stock : Integer;
}
Part by part (plain text)
  1. Product entity. The entity has a generated key and four fields. No rule is written here; the rules belong to the service.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

Valid products are created. Invalid ones are refused with status 400 and a message that names the problem.

What you should see

Valid product: 201. Missing name, price 20000 or a code like 'abc': 400 with a message about the field. The exact wording depends on your @sap/cds version. Stock sent by the client: not written.

Try it and check the result

  • Run cds watch and send the four requests. The first returns 201, the other three return 400.
  • Send a valid product with "stock": 500. Read it back with GET: the stock is not 500, because the field is read-only for clients.
  • Change the range to [0, 100] in the annotation, save (cds watch restarts) and repeat the price 99.5 and 200 requests to see the new limit.

Common mistakes

  • Annotation rules protect only requests that go through this service. Direct database access skips them.
  • @assert.format uses a regular expression. Anchor it with ^ and $, or a partial match passes.
  • Declared rules cover simple checks. Rules across several fields or against other data need a handler.
  • Read the real error answer once in your @sap/cds version. The wording of the messages can change between versions.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: SAP Cloud Application Programming Model with Node.js, local development. Check on your @sap/cds version.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.