2. Project 2 - Validate Country Constraints

The purpose of the app to be built is managing information about countries.

The app deals with just one object type: Country, as depicted in the diagram below. In the subsequent parts of the tutorial, you will extend this simple app by adding enumeration-valued attributes, as well as further model classes and the associations between them.

Figure 7.2. The object type Country defined with several constraints.

The object type Country defined with several constraints.

In this model, the following constraints have been expressed:

  1. Due to the fact that the name attribute is declared to be the standard identifier of Country, it is mandatory and unique.

  2. The name attribute has a string length constraint requiring its values to have at least 3 and at most 50 characters.

  3. The population attribute is mandatory, as indicated by the multiplicity expression [1] appended to the attribute name.

  4. The lifeExpectancy attribute is also mandatory and has an interval constraint: its values must be less than or equal to 100.

Notice that the militaryExpenditure attribute is not mandatory, but optional, as indicated by its multiplicity expression [0..1].

In addition to the constraints described in this list, there are the implicit range constraints defined by assigning the datatypes NonEmptyString to name, PositiveInteger to population, PositiveDecimal to lifeExpectancy and Percentage to militaryExpenditure (hint: a percentage value is a decimal number between 0 and 100). In our plain JavaScript approach, all these property constraints are encoded in the model class within property-specific check functions.

You can use the following sample data for testing your app:

Table 7.2. Sample country data

Name Population Life expectancy Military expend. (% GDP)
Germany 80,854,408 80.57 1.35
France 66,553,766 81.75 1.9
Russia 142,423,773 70.47 3.49
Monaco 30,535 89.52

More data about countries can be found in the CIA World Factbook.