Input validation

Rejecting bad input before it reaches the database. While this can be done manually inside a handler, there are solutions in place to derive type, range, enum and mandatory checks from the model definitions.

Overview

SAP SHIPS
Node.js →

@sap/cds Enforces @mandatory, @assert.range, @assert.format, @assert.target and @assert with a CXL case expression whenever data is written: the static checks on the input, the expressions collected into one query and run in the database, and a failure rejects the request and rolls the transaction back.

Java →

com.sap.cds:cds-services-bom Enforces declarative @assert and @mandatory constraints whenever data is written, and translates the resulting validation error messages out of the box.

Not covered: It stops at what a range, a regular expression or a CXL expression run in the database can say about an entity's elements: @assert.format takes a pattern and has no named format such as email or uri, and @assert.target does not check across services or against a target created in the same deep insert. Past that the check is a before handler of the developer's own, or a second vocabulary: JSON Schema keywords per element, evaluated by Ajv, in sap-cap-validator-plugin, and Zod schemas registered in a before handler in cds-validate. capire's own advice at that edge is to try the declarative constraints first and to write the before handler only past them; in CAP Java the Before phase is the one meant for validating the incoming parameters.

SourceDeclarative Constraints — capirechecked

Artifacts6

6 of 6 artifacts
ArtifactSignals
CDS plugins2
capvalCustom messages and handler classes for CAP input validation
Signals
none
sap-cap-validator-pluginCAP plugin validating input against @Validator JSON Schema rules
Signals
Quiet+1
Packages4
@sap/cdsCAP Node.js runtime
Signals
none
cap-toolkitUtility bundle for CAP handlers: builder, roles, validation, queries
Signals
Functional boundary
cds-validateZod schemas as CAP validation middleware
Signals
none
com.sap.cds:cds-services-bomMaven bill of materials pinning CAP Java module versions
Signals
none

Content7

Group by
Piece, what it offers
Declarative Programming in CAP: Less Code, More Value
SAP Community, BirgitS, BlogThe reasoning
CAPVAL: A project is born
SAP Community, MattBrightman, Blog
Extend SAP Cloud Application Programming Model(CAP) Validations with the CAPVAL Plugin
SAP Community, MattBrightman, BlogThe reasoningRunnable code
Show 4 more
#CAP-TechBytes Input Validation
SAP Community, Thajunniza, BlogThe reasoning
[Part - 1] Enrich CAP based Services with Behaviors using Annotations
SAP Community, Ajit Kumar Panda, Blog
Learn how to define constraints declaratively in your CDS model
SAP Developer Center, SAP, TutorialThe reasoningA recipe
Everything new: Try out the latest features in CAP
SAP TechEd — session catalogue, Daniel Hutzel, Daniel Schlachter, TalkA recipe
CAPVAL: A project is born
SAP Community, MattBrightman, Blog
Extend SAP Cloud Application Programming Model(CAP) Validations with the CAPVAL Plugin
SAP Community, MattBrightman, BlogThe reasoningRunnable code
Declarative Programming in CAP: Less Code, More Value
SAP Community, BirgitS, BlogThe reasoning
Everything new: Try out the latest features in CAP
SAP TechEd — session catalogue, Daniel Hutzel, Daniel Schlachter, TalkA recipe

Seen working1

ArtifactSignals
Samples1
assert-targetWhich CAP paths trigger @assert.target and which do not
No license

Details

What changed — and what it makes stale1
@sap/cds 8.5.0, CAP, Node.js

@assert.range accepts open intervals, a min or max wrapped in parentheses, and an underscore for infinity. Before this release a range was a closed [min, max] and a one-sided bound needed a case expression or a handler. CAP Java gained the same in 3.5.0.

Makes stale:

Advice that @assert.range cannot express 'strictly positive' or an unbounded side, and the before handlers written for that.

Coverage
statecore and community, CAP
assessed
relationshipunassessed
also inABAP, UI5
For your agent
Ref
capability:input-validation
JSON
/capabilities/input-validation.json
MCP
Connecting an agent