Used to type check arrays of objects.
- checkArray_objType0n(object_array, object_type) will not fail on an empty object_array
- checkArray_objType1n(object_array, object_type) will fail on an empty object_array
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
Real world example where checkArray_objType0n() is used to type check async MongoDb calls.
NOTE, there are no tests for checkArray_objType0n() and checkArray_objType1n().