cleanup impls

This commit is contained in:
Daniel Hutzel
2025-07-24 13:31:45 +02:00
parent 6dc1da2964
commit 991e890ba9
3 changed files with 37 additions and 31 deletions

View File

@@ -3,41 +3,14 @@
// using db-level constraints.
//
const cds = require('@sap/cds')
const cds = require('@sap/cds'); require('./validate.js')
cds.on('served', ()=> {
const $ = cds.validate
cds.validate = function (entity, key, ...columns) {
if (entity?.ref) entity = { // quick and dirty
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 (!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 => {
const failed = {}; for (let c in checks) {
if (c in entity.keys) continue
if (c[0] == '_') continue
if (checks[c]) failed[c] = checks[c]
}
if (Object.keys(failed).length) throw cds.error `Invalid input: ${failed}`
})
}
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

@@ -68,7 +68,7 @@ extend service AdminService with {
null as name,
// constraint related to two fields
dateOfDeath > dateOfBirth ? 'we must be born before we die' : null as _born_before_death,
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,33 @@
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 = cds.model.definitions[x] || cds.error `No such constraints view: ${x}`
return SELECT.one.from (constraints, pk, columns.length && columns)
// Collect and throw errors, if any
.then (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}`
})
}
// Helpers
const _is_constraints = x => x.ref || x.is_entity || typeof x === 'string'