Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
MattBrightman
Explorer
Input Validation is a key component of any application or service. Out of the box SAP CAP has some powerful annotations to validate user input.  However, it has been my experience from previous projects that although these annotations are useful from a data integrity standpoint, they do not offer the best user experience. The default message "Value is Required" is a simple, but common example. When this error occurs within a multilevel application with many mandatory fields, it can leave the user feeling lost.

To counter this we can implement various handlers such as Before Save and On Error, however this can result in:

  • Large handlers for custom validations/messages.

  • Repetitive development implementing custom validations and generating targets.


Recently, while considering these factors, I was left thinking “there must be a better way”. Surely, we can provide excellent error messages back to our users, while also having validations that are reusable, modular, and configurable.

As a result of this consideration I have written a plugin called CAPVAL, in the hope that I can address this common problem by extending the standard variations provided.

CAPVAL introduces two new annotations. The first annotation is @validation.message. This annotation works along side standard annotations such as @mandatory and@assert.target. Here is an example:
using { sap.fe.cap.travel as schema } from '../db/schema';

annotate schema.Travel {
@validation.message: 'Enter a valid begin date'
@mandatory
BeginDate;
}

Rather than the default message, "Value is Required", "Enter a Valid Begin Date" will now be returned to the user upon error. Localisation is also supported. For example, in the case above, the following annotation would yield the same result:@validation.message: 'i18n>begindate-error', provided an entry existed in the i18n.properties file.

Where applicable, some contextual information in a message can also be useful to the user:
db/validations.cds
using { sap.fe.cap.travel as schema } from '../db/schema';

annotate schema.Travel {
@validation.message: 'i18n>begindate-error'
@mandatory
BeginDate;
}

_i18n/i18n.properties
begindate-error = Begin Date required and must be before End Date {{Travel-EndDate}}

The second annotation "@validation.handler" is used for custom validations. "@validation.handler" allows a class to be specified that implements a custom validation. This allows for modularisation and reuse of validations.

This is the annotation:
db/validations.cds
using { sap.fe.cap.travel as schema } from '../db/schema';

annotate schema.Travel {
@validation: {
message: 'Please enter a begin date that is before the end date',
handler: 'srv/handlers/BeginDateChecks.js'
}
BeginDate;
}

And this is the corresponding implementation:
srv/handlers/BeginDateChecks.js

const BaseValidation = require("capval");
module.exports = class BeginDateChecks extends BaseValidation {
isValid(InputValue) {
var travel = this.getNode()

if (travel.BeginDate > travel.EndDate) {
return false
}

return true
}
};

As a developer, all that needs to be done is to implement the "isValid" method and return true or false. CAPVAL will do the rest - such as generating targets.

Further examples, installation, the source code, and a link to a fork of the SFLIGHT scenario can be found on GitHub.

Finally, this work would not have been possible without this excellent blog post from daniel.schlachter and connheim answer to this question

 

 

 
2 Comments
Labels in this area