Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 991e890

Browse files
committed
cleanup impls
1 parent 6dc1da2 commit 991e890

3 files changed

Lines changed: 37 additions & 31 deletions

File tree

bookshop/test/dynamic-constraints/server.js

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,14 @@
33
// using db-level constraints.
44
//
55

6-
const cds = require('@sap/cds')
6+
const cds = require('@sap/cds'); require('./validate.js')
77
cds.on('served', ()=> {
8-
9-
const $ = cds.validate
10-
cds.validate = function (entity, key, ...columns) {
11-
12-
if (entity?.ref) entity = { // quick and dirty
13-
name: entity.ref[0],
14-
constraints: { // even quicker and dirtier
15-
name: entity.ref[0] +'.constraints',
16-
keys: {ID:1}
17-
}
18-
}
19-
else if (!entity.is_entity) return // we skip all standard validations for the experiments
20-
else if (!entity.is_entity) return $(...arguments) // eslint-disable-line no-dupe-else-if
21-
22-
if (entity.constraints) entity = entity.constraints
23-
if (!key) return key => cds.validate(entity,key)
24-
if (key.results) key = key.results[0].lastInsertRowid // quick and dirty
25-
if (key.ID) key = key.ID // quick and dirty
26-
27-
return SELECT.one.from (entity, key, columns.length && columns) .then (checks => {
28-
const failed = {}; for (let c in checks) {
29-
if (c in entity.keys) continue
30-
if (c[0] == '_') continue
31-
if (checks[c]) failed[c] = checks[c]
32-
}
33-
if (Object.keys(failed).length) throw cds.error `Invalid input: ${failed}`
34-
})
35-
}
36-
378
const { AdminService } = cds.services
389
AdminService.after (['CREATE','UPDATE'], (result,req) => cds.validate (req.subject, result))
3910
})
4011

12+
13+
4114
Object.defineProperties (cds.entity.prototype, {
4215
constraints: { get() { return cds.model.definitions[this.name+'.constraints'] }},
4316
fields: { get() { return cds.model.definitions[this.name+'.field.control'] }},

bookshop/test/dynamic-constraints/srv/validation.cds

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ extend service AdminService with {
6868
null as name,
6969

7070
// constraint related to two fields
71-
dateOfDeath > dateOfBirth ? 'we must be born before we die' : null as _born_before_death,
71+
dateOfDeath < dateOfBirth ? 'we can''t die before we are born' : null as _born_before_death,
7272
$self._born_before_death as dateOfBirth,
7373
$self._born_before_death as dateOfDeath,
7474

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const cds = require('@sap/cds')
2+
const $super = { validate: cds.validate, skip(){} }
3+
4+
5+
/**
6+
* Quick and dirty implementation for cds.validate() using db-level constraints.
7+
*/
8+
cds.validate = function (x, pk, ...columns) {
9+
10+
// Delegate to base impl of cds.validate() for standard input validation
11+
if (!_is_constraints(x)) return $super.skip (...arguments)
12+
13+
// Support subject refs to base entities as arguments
14+
if (x?.ref) [ x, pk ] = [ x.ref +'.constraints', pk.ID||pk ]
15+
16+
// Run the constraints check query
17+
const constraints = cds.model.definitions[x] || cds.error `No such constraints view: ${x}`
18+
return SELECT.one.from (constraints, pk, columns.length && columns)
19+
20+
// Collect and throw errors, if any
21+
.then (checks => {
22+
const failed = {}; for (let c in checks) {
23+
if (c in constraints.keys) continue
24+
if (c[0] == '_') continue
25+
if (checks[c]) failed[c] = checks[c]
26+
}
27+
if (Object.keys(failed).length) throw cds.error `Invalid input: ${failed}`
28+
})
29+
}
30+
31+
32+
// Helpers
33+
const _is_constraints = x => x.ref || x.is_entity || typeof x === 'string'

0 commit comments

Comments
 (0)