Compare commits

..

13 Commits

Author SHA1 Message Date
Daniel Hutzel
4e76d71def polishing 2025-07-24 13:52:01 +02:00
Daniel Hutzel
991e890ba9 cleanup impls 2025-07-24 13:31:45 +02:00
Daniel Hutzel
6dc1da2964 Reverting removal of cds.service.providers 2025-07-24 12:24:11 +02:00
Daniel Hutzel
65eccfad33 Moved dynamic constraints in a dedicated folder 2025-07-24 10:38:43 +02:00
Daniel Hutzel
c9021d0f46 Merge branch 'main' into dynamic-constraints 2025-07-23 17:41:16 +02:00
Daniel Hutzel
d57c29e126 constraints on service level 2025-07-23 17:36:58 +02:00
Daniel Hutzel
044baec701 Merge branch 'main' into dynamic-constraints 2025-07-18 17:22:34 +02:00
Daniel Hutzel
15fa7de880 . 2025-07-16 17:27:07 +02:00
Daniel Hutzel
04d659c9c2 more comments 2025-07-16 17:25:32 +02:00
Daniel Hutzel
4fc324c7fb Added instructions for ad-hoc tests in cds repl 2025-07-16 14:08:25 +02:00
Daniel Hutzel
7cd3a6d1e3 comments 2025-07-16 13:59:33 +02:00
Daniel Hutzel
b55cce3e63 case when equivalent 2025-07-16 13:49:45 +02:00
Daniel Hutzel
bd5a57189d feat: PoC for dynamic constraints using standard cds views 2025-07-16 13:23:35 +02:00
8 changed files with 178 additions and 5 deletions

View File

@@ -1068,15 +1068,14 @@
}
},
"node_modules/form-data": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.4.tgz",
"integrity": "sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==",
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.2.tgz",
"integrity": "sha512-hGfm/slu0ZabnNt4oaRZ6uREyfCj6P4fT/n6A1rGV+Z0VdGXjfOhVUpkn6qVQONHGIFwmveGXyDs75+nr6FM8w==",
"license": "MIT",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"es-set-tostringtag": "^2.1.0",
"hasown": "^2.0.2",
"mime-types": "^2.1.12"
},
"engines": {

View File

@@ -0,0 +1,30 @@
## Experimental Dynamic Constraints
This example demonstrates how to use dynamic constraints in a CAP application. It includes a service definition and a test setup to validate the constraints.
### Prerequisites
You've setup the [_cap/samples_](https://github.com/sap-samples/cloud-cap-samples) like so:
```sh
git clone -q https://github.com/sap-samples/cloud-cap-samples cap/samples
cd cap/samples
npm install
```
### Testing
Test like that in `cds.repl` from _cap/samples_ root:
```sh
cds repl --run bookshop/test/dynamic-constraints
````
```javascript
await AdminService.create ('Books', {})
await AdminService.create ('Books', { title:' ', author_ID:150 })
await AdminService.create ('Books', { title:'x' })
await cds.validate (Books.constraints, 201)
await cds.validate (Books.constraints)
```

View File

@@ -0,0 +1,17 @@
//
// Quick and dirty implementation for cds.validate()
// using db-level constraints.
//
const cds = require('@sap/cds'); require('./validate.js')
cds.on('served', ()=> {
const { AdminService } = cds.services
AdminService.after (['CREATE','UPDATE'], (result,req) => cds.validate (req.subject, result))
})
Object.defineProperties (cds.entity.prototype, {
constraints: { get() { return cds.model.definitions[this.name+'.constraints'] }},
fields: { get() { return cds.model.definitions[this.name+'.field.control'] }},
})

View File

@@ -0,0 +1,7 @@
namespace AdminService; //> for cds.entities
using { AdminService } from '../../../srv/admin-service';
annotate AdminService with @requires: false;
extend AdminService.Authors with columns {
null as books // to simulate the exclusion of books
}

View File

@@ -0,0 +1,10 @@
namespace sap.capire.bookshop;
using from './admin-service';
view Books.field.control as select from Books { ID,
genre.name == 'Drama' ? 'readonly' :
null as price
}
extend Books with {
fc : Association to Books.field.control on fc.ID = $self.ID
}

View File

@@ -0,0 +1,76 @@
using { AdminService, sap.capire.bookshop as my } from './admin-service';
extend service AdminService with {
// entity Books.drafts as projection on AdminService.Books;
// @cds.api.ignore view Books.drafts.constraints as select from AdminService.Books.drafts mixin {
// before: Association to my.Books on before.ID = $self.ID;
// base: Association to my.Books on base.ID = $self.ID;
// } into { ID, // FIXME: compiler should resolve Books without AdminService prefix
// case
// when title is null then 'is missing'
// when trim(title)='' then 'must not be empty'
// end as title,
// ...
// }
/**
* Validation constraints for Books
*/
@cds.api.ignore view Books.constraints as select from AdminService.Books mixin {
base: Association to my.Books on base.ID = $self.ID;
} into { ID, // FIXME: compiler should resolve Books without AdminService prefix
// two-step mandatory check
case
when title is null then 'is missing'
when trim(title)='' then 'must not be empty'
end as title,
// the above is equivalent to:
// title is null ? 'is missing' : trim(title)='' ? 'must not be empty' :
// range check
stock < 0 ? 'must not be negative' :
null as stock,
// range check
price < 0 ? 'must not be negative' :
null as price,
// assert target check
genre.ID is not null and not exists genre ? 'does not exist' :
null as genre,
// multiple constraints: mandatory + assert target + special
author.ID is null ? 'is missing' : // FIXME: 1) // TODO: 2)
not exists author ? 'Author does not exist: ' || author.ID :
count(base.author.books.ID) -1 > 1 ? author.name || ' already wrote too many books' : // TODO: 3)
null as author,
} group by ID;
// 1) FIXME: expected author.ID to refer to foreign key,
// apparently that is not the case -> move one line up
// and run test to see the erroneous impact.
// 2) TODO: we should allow to write author is null instead of author.ID is null
// 3) TODO: we should support count(author.books)
/**
* Validation constraints for Authors
*/
view Authors.constraints as select from AdminService.Authors { ID, // FIXME: compiler should resolve Authors without AdminService prefix
// two-step mandatory check
name = null ? 'is missing' : trim(name)='' ? 'must not be empty' :
null as name,
// constraint related to two fields
dateOfDeath < dateOfBirth ? 'we can''t die before we are born' : null as _born_before_death,
$self._born_before_death as dateOfBirth,
$self._born_before_death as dateOfDeath,
}
}

View File

@@ -0,0 +1,34 @@
const cds = require('@sap/cds')
const $super = { validate: cds.validate, skip(){} }
/**
* Quick and dirty implementation for cds.validate() using db-level constraints.
*/
cds.validate = function (x, pk, ...columns) {
// Delegate to base impl of cds.validate() for standard input validation
if (!_is_constraints(x)) return $super.skip (...arguments)
// Support subject refs to base entities as arguments
if (x?.ref) [ x, pk ] = [ x.ref +'.constraints', pk.ID||pk ]
// Run the constraints check query
const constraints = typeof x === 'string' ? cds.model.definitions[x] || cds.error `No such constraints view: ${x}` : x
return SELECT.from (constraints, pk, columns.length && columns)
// Collect and throw errors, if any
.then (rows => (rows.map ? rows : [rows]).map (checks => {
const failed = {}; for (let c in checks) {
if (c in constraints.keys) continue
if (c[0] == '_') continue
if (checks[c]) failed[c] = checks[c]
}
if (Object.keys(failed).length) throw cds.error `Invalid input: ${failed}`
return checks
}))
}
// Helpers
const _is_constraints = x => x.ref || x.is_entity || typeof x === 'string'

View File

@@ -52,7 +52,7 @@ module.exports = { DataService }
/** @returns {cds.Service} */
function findDataSource(dataSourceName, entityName) {
for (let srv of Object.values(cds.services)) { // all connected services
for (let srv of cds.service.providers) { // all connected services
if (!srv.name) continue // FIXME intermediate/pending in cds.services ?
if (dataSourceName === srv.name || entityName.startsWith(srv.name+'.')) {
log._debug && log.debug(`using ${srv.name} as data source`)