Chapter 23. Constraint Validation with mODELcLASSjs

Table of Contents

1. Encoding the Design Model
2. Project Set-Up
3. The View and Controller Layers
4. Run the App and Get the Code
5. Concluding Remarks

In this part of the tutorial, we show how to build a simple app with constraint validation using the mODELcLASSjs library for avoiding boilerplate model code. Compared to the plain JavaScript validation app, we deal with the same issues: showing 1) how to define constraints in a model class, 2) how to perform responsive validation in the user interface based on the constraints defined in the model classes. The main difference when using mODELcLASSjs is that defining constraints becomes much simpler. Since mODELcLASSjs provides a generic method for checking property constraints, the property-specific check methods for checking property constraints are no longer needed. Since constraints are defined in a purely declarative manner, their textual encoding corresponds directly to their expression in the information design model. This implies that we can directly encode the information design model without first creating a data model from it.

As in other tutorials, the purpose of our app is to manage information about books. The information items and constraints are described in the information design model shown in Figure 23.1 below.

Figure 23.1. A platform-independent design model with the class Book and two invariants

A platform-independent design model with the class Book and two invariants

1. Encoding the Design Model

We now show how to encode the ten integrity constraints defined by the design model shown in Figure 23.1 above.

  1. For the first three of the four properties defined in the Book class, we have a mandatory value constraint, indicated by the multiplicity expression [1]. However, since properties are mandatory by default in mODELcLASSjs, we don't have to encode anything for them. Only for the property edition, we need to encode that it is optional with the key-value pair optional: true, as shown in the edition property declaration in the class definition below.

  2. The isbn attribute is declared to be the standard identifier of Book. We encode this (and the implied uniqueness constraint) in the isbn property declaration with the key-value pair isStandardId: true, as shown in the class definition below.

  3. The isbn attribute has a pattern constraint requiring its values to match the ISBN-10 format that admits only 10-digit strings or 9-digit strings followed by "X". We encode this with the key-value pair pattern:/\b\d{9}(\d|X)\b/ and the special constraint violation message defined by patternMessage:"The ISBN must be a 10-digit string or a 9-digit string followed by 'X'!".

  4. The title attribute has an string length constraint with a maximum of 50 characters. This is encoded with max: 50.

  5. The year attribute has an interval constraint with a minimum of 1459 and a maximum that is not fixed, but provided by the utility function nextYear(). We can encode this constraint with the key-value pairs min: 1459 and max: util.nextYear().

  6. Finally, there are four range constraints, one for each property. We encode them with corresponding key-value pairs, like range:"NonEmptyString".

This leads to the following definition of the model class Book :

Book = new mODELcLASS({
  typeName: "Book",
  properties: {
    isbn: {range:"NonEmptyString", isStandardId: true, label:"ISBN", pattern:/\b\d{9}(\d|X)\b/, 
        patternMessage:"The ISBN must be a 10-digit string or a 9-digit string followed by 'X'!"},
    title: {range:"NonEmptyString", max: 50},
    year: {range:"Integer", min: 1459, max: util.nextYear()},
    edition: {range:"PositiveInteger", optional: true}

For such a model class definition, mODELcLASSjs provides generic data management operations (Book.add, Book.update, Book.destroy, etc.) as well as property checks and setters (Book.check and bookObject.set).