Skip to content

Commit

Permalink
some more messages
Browse files Browse the repository at this point in the history
  • Loading branch information
patricebender committed Feb 14, 2025
1 parent 9e3ebca commit 83d19ba
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
15 changes: 9 additions & 6 deletions db-service/lib/assert-constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ function attachConstraints(_results, req) {
xpr.push({ xpr: condition.xpr })
const colsForConstraint = [{
xpr: wrapInCaseWhen(xpr),
as: name,
// avoid naming ambiguities for anonymous constraints,
// where the element itself is part of the msg params
as: name + '_constraint',
cast: {
type: 'cds.Boolean',
},
Expand Down Expand Up @@ -161,15 +163,16 @@ async function checkConstraints(req) {
const { validationQuery, constraints } = check
const result = await this.run(validationQuery)
if (!result) continue
for (const constraintName in constraints) {
for (const key in constraints) {
const constraintCol = key + '_constraint'
for (const row of result) {
if (!row[constraintName]) {
const { message, parameters } = constraints[constraintName]
if (!row[constraintCol]) {
const { message, parameters } = constraints[key]
const msgParams = {}
if (parameters) {
Object.keys(row).filter(alias => alias !== constraintName).forEach(alias => msgParams[alias] = row[alias])
Object.keys(row).filter(alias => alias !== constraintCol).forEach(alias => msgParams[alias] = row[alias])
}
req.error(400, message ? (cds.i18n.messages.at(message, msgParams) || message) : `@assert.constraint ”${constraintName}” failed`)
req.error(400, message ? (cds.i18n.messages.at(message, msgParams) || message) : `@assert.constraint ”${key}” failed`)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/bookshop/_i18n/messages.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ASSERT_RANGE = Value {0} is not in specified range [{1}, {2}]
STOCK_NOT_EMPTY = Stock for book "{title}" ({ID}) must not be a negative number
LIFE_BEFORE_DEATH = The Birthday "{dateOfBirth}" of author "{name}" must not be after the Deathday "{dateOfDeath}"
3 changes: 2 additions & 1 deletion test/bookshop/db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ entity Authors : managed {
dateOfBirth : Date;
@assert.constraint : {
condition: ( days_between(dateOfBirth, dateOfDeath) >= 0 ),
message: 'The date of birth must be before the date of death',
message: 'LIFE_BEFORE_DEATH',
parameters: ((name, dateOfBirth, dateOfDeath))
}
dateOfDeath : Date;
placeOfBirth : String;
Expand Down
2 changes: 1 addition & 1 deletion test/scenarios/bookshop/assert-constraint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Bookshop - assertions', () => {
},
{ auth: { username: 'alice' } },
),
).to.be.rejectedWith(/The date of birth must be before the date of death/)
).to.be.rejectedWith('The Birthday "null" of author "Brandon Sanderson" must not be after the Deathday "1975-12-19"')
// book should not have been created
const book = await SELECT.one.from(Books).where({ ID: 55 })
expect(book).to.not.exist
Expand Down

0 comments on commit 83d19ba

Please sign in to comment.