4. Set up the Folder Structure Adding Some Library Files

The MVC folder structure of our validation app extends the structure of the minimal app by adding a lib folder containing the generic code libraries browserShims.js, errorTypes.js and util.js. Thus, we get the following folder structure:


We discuss the contents of the added files in the following sub-sections.

4.1. Provide general utility functions and JavaScript fixes in library files

We add three library files to the lib folder:

  1. browserShims.js contains a definition of the string trim function for older browsers that don't support this function (which was only added to JavaScript in ES5, defined in 2009). More browser shims for other recently defined functions, such as querySelector and classList, could also be added to browserShims.js.

  2. util.js contains the definitions of a few utility functions such as isNonEmptyString(x) for testing if x is a non-empty string.

  3. errorTypes.js defines classes for error (or exception) types corresponding to the basic types of property constraints discussed above: StringLengthConstraintViolation, MandatoryValueConstraintViolation, RangeConstraintViolation, IntervalConstraintViolation, PatternConstraintViolation, UniquenessConstraintViolation. In addition, a class NoConstraintViolation is defined for being able to return a validation result object in the case of no constraint violation.

4.2. Create a start page

The start page index.html takes care of loading CSS page styling files with the help of the following two link elements:

<link rel="stylesheet" href="css/normalize.min.css">
<link rel="stylesheet" href="css/main.css">

Then it loads the following JavaScript files:

  1. browserShims.js and util.js from the lib folder, discussed in Section 4.1,

  2. errorTypes.js from the lib folder, defining exception classes.

  3. initialize.js from the src/c folder, defining the app's MVC namespaces, as discussed in Chapter 3

  4. Book.js from the src/m folder, a model class file that provides data management and other functions discussed in Section 5.