constraints on service level

This commit is contained in:
Daniel Hutzel
2025-07-23 17:36:58 +02:00
parent 044baec701
commit d57c29e126
4 changed files with 90 additions and 61 deletions

View File

@@ -1,19 +1,31 @@
// //
// Quick and dirty implementation for cds.validate() using db-level constraints // Quick and dirty implementation for cds.validate() using db-level constraints
// Test in cds.repl like that: // Test in cds.repl like that:
// {Books} = AdminService.entities
// await cds.run (()=> INSERT.into (Books, { title:' ', author_ID:150 }) .then (cds.validate(Books))) // await cds.run (()=> INSERT.into (Books, { title:' ', author_ID:150 }) .then (cds.validate(Books)))
// await AdminService.create ('Books', { title:' ', author_ID:150 })
// //
const cds = require('@sap/cds') const cds = require('@sap/cds')
cds.on('served', ()=> { cds.on('served', ()=> {
const $ = cds.validate; cds.validate = function (entity, key, ...columns) { const $ = cds.validate
cds.validate = function (entity, key, ...columns) {
if (!entity.is_entity) return $(...arguments) if (entity?.ref) entity = { // quick and dirty
if (!key) return key => cds.validate(entity,key) name: entity.ref[0],
constraints: { // even quicker and dirtier
name: entity.ref[0] +'.constraints',
keys: {ID:1}
}
}
else if (!entity.is_entity) return // we skip all standard validations for the experiments
else if (!entity.is_entity) return $(...arguments) // eslint-disable-line no-dupe-else-if
if (entity.constraints) entity = entity.constraints if (entity.constraints) entity = entity.constraints
if (key.results) key = key.results[0].lastInsertRowid if (!key) return key => cds.validate(entity,key)
if (key.results) key = key.results[0].lastInsertRowid // quick and dirty
if (key.ID) key = key.ID // quick and dirty
return SELECT.one.from (entity, key, columns.length && columns) .then (checks => { return SELECT.one.from (entity, key, columns.length && columns) .then (checks => {
const failed = {}; for (let c in checks) { const failed = {}; for (let c in checks) {
@@ -25,6 +37,8 @@ cds.on('served', ()=> {
}) })
} }
const { AdminService } = cds.services
AdminService.after (['CREATE','UPDATE'], (result,req) => cds.validate (req.subject, result))
}) })
Object.defineProperties (cds.entity.prototype, { Object.defineProperties (cds.entity.prototype, {

View File

@@ -1,2 +1,2 @@
using { AdminService } from './admin-service'; using { AdminService } from './admin-service';
annotate AdminService with @requires:'admin'; annotate AdminService with @requires: false; //'admin';

View File

@@ -1,6 +1,6 @@
using { sap.capire.bookshop as my } from '../db/schema'; using { sap.capire.bookshop as my } from '../db/schema';
service AdminService @(path:'/admin') { service AdminService @(path:'/admin') {
entity Authors as projection on my.Authors; entity Authors as projection on my.Authors excluding { books};
entity Books as projection on my.Books; entity Books as projection on my.Books;
entity Genres as projection on my.Genres; entity Genres as projection on my.Genres;
} }

View File

@@ -1,11 +1,25 @@
namespace sap.capire.bookshop; using { AdminService, sap.capire.bookshop as my } from './admin-service';
using from '../db/schema';
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 * Validation constraints for Books
*/ */
view Books.constraints as select from Books { ID, @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 // two-step mandatory check
case case
@@ -30,7 +44,7 @@ view Books.constraints as select from Books { ID,
// multiple constraints: mandatory + assert target + special // multiple constraints: mandatory + assert target + special
author.ID is null ? 'is missing' : // FIXME: 1) // TODO: 2) author.ID is null ? 'is missing' : // FIXME: 1) // TODO: 2)
not exists author ? 'Author does not exist: ' || author.ID : not exists author ? 'Author does not exist: ' || author.ID :
count(author.books.ID) -1 > 1 ? author.name || ' already wrote too many books' : // TODO: 3) count(base.author.books.ID) -1 > 1 ? author.name || ' already wrote too many books' : // TODO: 3)
null as author, null as author,
} group by ID; } group by ID;
@@ -47,7 +61,7 @@ view Books.constraints as select from Books { ID,
/** /**
* Validation constraints for Authors * Validation constraints for Authors
*/ */
view Authors.constraints as select from Authors { ID, view Authors.constraints as select from AdminService.Authors { ID, // FIXME: compiler should resolve Authors without AdminService prefix
// two-step mandatory check // two-step mandatory check
name = null ? 'is missing' : trim(name)='' ? 'must not be empty' : name = null ? 'is missing' : trim(name)='' ? 'must not be empty' :
@@ -59,3 +73,4 @@ view Authors.constraints as select from Authors { ID,
$self._born_before_death as dateOfDeath, $self._born_before_death as dateOfDeath,
} }
}