Changelog (latest relevant releases)
- Added check
- Breaking change: renamed
requireOnecheck to specify alternatives
Be careful using my stuff: I'm an amateur.
Froid is a sanity checker, just like Freud, but more French: "Froid" is the pronunciation of "Freud" written as if it was an Italian word, but it also happens to be a French word that has an entirely different meaning. I have no idea why I'm writing this.
Sanity in theory
sanity of an object (from now on
loony) is defined in object form. It has one field for every field the
loony has, named exactly the same.
Assuming that all of
loony's fields are supposed to hold a simple value such as a constructed object (Date, Function, MyCrazyClass, etc.), an Array, a String or a Number, the value of each of
sanity's fields is a
tests have a field for each
check the test needs to perform; the value of each
check defines what is valid and/or what isn't.
As for the fields
loony may have that are not constructed objects, meaning they are object literals, the corresponding fields in
sanity will not hold a
test object; they will hold an identical object literal instead.
loonys object literals are treated as other loonies, and their corresponding part of
sanity will be treated as their sanity check.
loony's fields according to the
tests. If something is wrong, it produces a moderately helpful message such as "'type' must be one of ['one', 'two'], got 'three' instead". Finally, all of the error messages are concatenated and returned.
loony passes all of the
froid simply leaves his house, returning a sad
At the moment
froid can perform a rather limited number of
checks. I add new ones as needed.
Sanity in practice
In the following example a couple of silly loonies have forgotten part or all of their address. Froid quickly reprimands them. Notice how Joe's appreciation of pickles is completely ignored - Froid can't be distracted easily.
var froid = ;var sanity =address: mandatory: true type: String;var loonieJoe =pickles: 'tasty';console;// => "address" is mandatory, got undefined insteadvar loonieMark =address: 5;console;// => "address" must be a String, got Number instead
High-functioning psychos Brad and Colin may know how to fake, but they are still no match for Froid's recursive inspection:
var froid = ;var sanity =address:mandatory: truefields:road: type: String mandatory: truenumber: type: Number greaterThan: 0 mandatory: truecity: type: String mandatory: true;var loonieBrad =address: 'somewhere';console;// => "address" must be a Object, got String insteadvar loonieColin =address:road: 5number: 'Bedlam Avenue';console;// => (in a single unbroken string)// => "address.road" must be a String, got Number instead;// => "address.number" must be a Number, got String instead;// => "address.number" must be greater than 0, got Bedlam Avenue instead;// => "address.city" is mandatory, got undefined instead
As you can see from the messages about
address.number, if a field violates more than one sanity rule Froid will report all of the failures rather than stop at the first one. This may help you fix all of the problems at once before testing again. It doesn't happen in the first case, when the entire
address object Froid is supposed to check is missing; in that case he simply calls you an idiot and leaves.
Sometimes people are perfectly sane:
var froid = ;var sanity =everything: String;var coolGuy =everything: 'OK';console;// => null
Checks currently supported
I'll add a bit to this section every day until I've documented everything. If you're reading this on NPM, come check this readme on github, where I can update it without releasing a new version after every change.