entity Student : cuid, managed {
rollno : String(5) @mandatory @Core.Immutable @assert.unique;
name : String(50) @mandatory ;
age : Integer @assert.range: [ 18, 50 ];
dept : String(3) @assert.range enum { CSE; ECE; EEE; };
email : String(200) @mandatory;
telephone : String(132) @mandatory;
postCode : String(10) @mandatory;
}
annotate Student with {
name @assert.format: '/^[a-zA-Z]+ [a-zA-Z]+$/';
email @assert.format: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$';
telephone @assert.format: '^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$';
} @mandatory@assert.uniquerollno property is annotated with @assert.unique. This means that each Student entity instance must have a unique value for the rollno property. When creating or updating a Student entity, the CAP framework automatically checks the uniqueness of the rollno value and raises an error if a duplicate value is detected.@assert.rangeage property is annotated with @assert.range: [18, 50]. This specifies that the age property must have a value between 18 and 50, inclusive. When creating or updating a Student entity, the CAP framework automatically checks the range of the age value and raises an error if it falls outside the specified range.@assert.range enum@assert.range enum {values}dept property is annotated with @assert.range and defined as an enumeration. It ensures that the department value is within the specified options: CSE, ECE, and EEE.This ensures that only valid department values are allowed for the dept property.@assert.formatname, email, and telephone) annotated with @assert.format.name property, the regular expression /^[a-zA-Z]+ [a-zA-Z]+$/ is specified as the format. This pattern enforces that the name value consists of two words separated by a space.email property is annotated with a regular expression pattern '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', which validates that the email value follows a standard email format.telephone property uses the regular expression pattern '^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$' to ensure the telephone value matches a specific phone number format.@assert.format annotations should be carefully crafted to match the intended format and account for any variations or edge cases that may arise.

@assert annotation.These standard messages can be overridden based on the business purpose.messages.properties file and define custom messages for specific validation errors or other standard messages. Here's how you can set up the messages.properties file:messages.properties file: In your CAP project, create a new file named messages.properties under the srv directory.messages.properties file and add key-value pairs representing the messages you want to override. The keys should correspond to the standard messages you wish to replace.assert.validation error. Add the following lines to the messages.properties file:ASSERT_FORMAT=The input "{0}" contains invalid characters
ASSERT_RANGE=The input "{0}" should be between "{1}" and "{2}"
ASSERT_NOT_NULL={0} is mandatory input
ASSERT_ENUM=Please only enter a value from {{1}}messages.properties file to provide dynamic and more informative messages. Placeholders allow you to include dynamic values or parameters within the messages that can be replaced with actual values at runtime.{0}, {1}, and {2} within the messages are placeholders that represent dynamic values.assert.mandatory validation fails for the name field, the placeholder {0} will be replaced with the field name, resulting in a message like "'name' is mandatory input."assert.range validation, if the value of the age property falls outside the specified range, the placeholders {0}, {1}, and {2} will be replaced with the property name and the range values, resulting in a message like "The input 'age' should be between 18 and 50."messages.properties file is placed in the correct location (srv directory) and that the file name is spelled correctly. Also, make sure that the custom messages in the file are in the format key=value, with one key-value pair per line.

You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 48 | |
| 47 | |
| 37 | |
| 32 | |
| 32 | |
| 30 | |
| 29 | |
| 24 | |
| 23 | |
| 22 |