Froid
Changelog (latest relevant releases)
v1.2.2
- Added check
after
v1.2.0
- Breaking change: renamed
children
tofields
- Added
requireOne
check to specify alternatives
v1.1.3
- Added
depends
check
About
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
The 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 test
object.
test
s 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. loony
s object literals are treated as other loonies, and their corresponding part of sanity
will be treated as their sanity check.
froid
check
s the loony
's fields according to the test
s. 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.
If the loony
passes all of the test
s, froid
simply leaves his house, returning a sad null
.
At the moment froid
can perform a rather limited number of check
s. 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 instead var 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: true fields: road: type: String mandatory: true number: type: Number greaterThan: 0 mandatory: true city: type: String mandatory: true ; var loonieBrad = address: 'somewhere'; console;// => "address" must be a Object, got String instead var loonieColin = address: road: 5 number: '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.
Have fun!