|
3 | 3 | // using db-level constraints. |
4 | 4 | // |
5 | 5 |
|
6 | | -const cds = require('@sap/cds') |
| 6 | +const cds = require('@sap/cds'); require('./validate.js') |
7 | 7 | 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 | | - |
37 | 8 | const { AdminService } = cds.services |
38 | 9 | AdminService.after (['CREATE','UPDATE'], (result,req) => cds.validate (req.subject, result)) |
39 | 10 | }) |
40 | 11 |
|
| 12 | + |
| 13 | + |
41 | 14 | Object.defineProperties (cds.entity.prototype, { |
42 | 15 | constraints: { get() { return cds.model.definitions[this.name+'.constraints'] }}, |
43 | 16 | fields: { get() { return cds.model.definitions[this.name+'.field.control'] }}, |
|
0 commit comments