polishing

This commit is contained in:
Daniel Hutzel
2025-07-24 13:52:01 +02:00
parent 991e890ba9
commit 4e76d71def
3 changed files with 11 additions and 4 deletions

View File

@@ -22,5 +22,9 @@ cds repl --run bookshop/test/dynamic-constraints
```` ````
```javascript ```javascript
await AdminService.create ('Books', {})
await AdminService.create ('Books', { title:' ', author_ID:150 }) 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

@@ -1,3 +1,5 @@
namespace AdminService; //> for cds.entities
using { AdminService } from '../../../srv/admin-service'; using { AdminService } from '../../../srv/admin-service';
annotate AdminService with @requires: false; annotate AdminService with @requires: false;
extend AdminService.Authors with columns { extend AdminService.Authors with columns {

View File

@@ -14,18 +14,19 @@ cds.validate = function (x, pk, ...columns) {
if (x?.ref) [ x, pk ] = [ x.ref +'.constraints', pk.ID||pk ] if (x?.ref) [ x, pk ] = [ x.ref +'.constraints', pk.ID||pk ]
// Run the constraints check query // Run the constraints check query
const constraints = cds.model.definitions[x] || cds.error `No such constraints view: ${x}` const constraints = typeof x === 'string' ? cds.model.definitions[x] || cds.error `No such constraints view: ${x}` : x
return SELECT.one.from (constraints, pk, columns.length && columns) return SELECT.from (constraints, pk, columns.length && columns)
// Collect and throw errors, if any // Collect and throw errors, if any
.then (checks => { .then (rows => (rows.map ? rows : [rows]).map (checks => {
const failed = {}; for (let c in checks) { const failed = {}; for (let c in checks) {
if (c in constraints.keys) continue if (c in constraints.keys) continue
if (c[0] == '_') continue if (c[0] == '_') continue
if (checks[c]) failed[c] = checks[c] if (checks[c]) failed[c] = checks[c]
} }
if (Object.keys(failed).length) throw cds.error `Invalid input: ${failed}` if (Object.keys(failed).length) throw cds.error `Invalid input: ${failed}`
}) return checks
}))
} }