12 Advanced data validation

 

A self-made gift

Hidden listing
function KlipseLoadLib(url, symbol) {
  if (window[symbol]) {
    return;
  }
  var script = document.createElement('script');
  script.src = url;
  document.body.append(script);
}
KlipseLoadLib("https://cdnjs.cloudflare.com/ajax/libs/ajv/6.12.6/ajv.bundle.js", "Ajv");
KlipseLoadLib("https://cdnjs.cloudflare.com/ajax/libs/json-schema-faker/0.5.0-rc23/bundle.umd.min.js", "JSONSchemaFaker");
function dev() {
  return true;
}
window.ajv = new Ajv();

This chapter covers

  • Validating function arguments
  • Validating function return values
  • Data validation beyond static types
  • Automatic generation of data model diagrams
  • Automatic generation of schema-based unit tests

As the size of a code base grows in a project that follows DOP principles, it becomes harder to manipulate functions that receive and return only generic data. It is hard to figure out the expected shape of the function arguments, and when we pass invalid data, we don’t get meaningful errors.

Until now, we have illustrated how to validate data at system boundaries. In this chapter, we will illustrate how to validate data when it flows inside the system by defining data schemas for function arguments and their return values. This allows us to make explicit the expected shape of function arguments, and it eases development. We gain some additional benefits from this endeavor, such as automatic generation of data model diagrams and schema-based unit tests.

12.1 Function arguments validation

12.2 Return value validation

12.3 Advanced data validation

12.4 Automatic generation of data model diagrams

12.5 Automatic generation of schema-based unit tests

12.6 A new gift

Summary