701 - Array of Objects

by steen_hansen

HTML

<!DOCTYPE html><meta charset="utf-8" />
<title>701 Array of Objects</title>





<div id='js-div' style="width:35%">
  <a href='https://github.com/steenhansen/type-czech'  target="_blank" class='git-hub'>GitHub Package</a>
  <div id='js-code'>the javascript</div>
  <br>


























</div>


<div id="the-explanation" style="width:65%" >
  <div class='an-explantion'>701 - Array of Objects</div>
  <div id='explanation-text'> 
    Used to type check arrays of objects. 
    <ul>
      <li> checkArray_objType0n(object_array, object_type) will not fail on an empty object_array</li>
      <li> checkArray_objType1n(object_array, object_type) will fail on an empty object_array</li>
    </ul>
<pre>
const RECIPE_TYPES = {
  title: "string",
  ingredients: "array",
  minutes: "number"
};

recipe_arr = [{
  {title: "Mac and Cheese", ingredients: [ { ingredient: "cheese", amount: "handful" }, { ingredient: "milk", amount: "123ml" } ], minutes: 12},
  {title: "Cookies",        ingredients: [ { ingredient: "flour", amount: "1 cup" },    { ingredient: "milk", amount: "123ml" } ], minutes: 120},
  {title: "Banana Bread",   ingredients: [ { ingredient: "flour", amount: "1 cup" },    { ingredient: "Bananas", amount: "3"  } ], minutes: 45}
}]

const err = type_czech.checkArray_objType0n([], RECIPE_TYPES)
if (err) 
  throw "Cannot fail as no objects"

const err = type_czech.checkArray_objType0n(recipe_arr, RECIPE_TYPES)
if (err) 
  throw "recipe_arr element does not conform to RECIPE_TYPES " + err

const err = type_czech.checkArray_objType1n([], RECIPE_TYPES)
if (err) 
  throw "Must be at least 1 object in the array" + err


</pre>



<br>
<br>



  <a href='https://github.com/steenhansen/type-czech-phone-recipes/blob/main/mongoose-database/tc-recipe-collections.js' 
   target="_blank" style="float:left">Real world example </a>&nbsp;where checkArray_objType0n() is used to type check async MongoDb calls.
<br>
<br>


NOTE, there are no tests for checkArray_objType0n() and...