58 points · by bruno02221 · 8y ago · 31 comments
Typical top HN comment.
Yeah, very complicated:
v8n()
.number()
.between(0, 100)
.even()
.not.equal(32)
.test(74); // true // helpers.js
const isNumber = typeof n === "number" && isFinite(n)
const isTestScore = n => isNumber(n) && n >= 0 && n <= 100;
// business-logic.js
const uploadScore = (n, cb) => {
if (!isTestScore(n))
return cb(new Error("invalid test score"));
// super contrived, but let's do it
if (n === 32 || n%2)
return cb(new Error("we don't like oddities and we hate 32"));
// do something with valid test score n
}
IMHO, a validation library should validate types and structure and should know nothing about the specific values of your data. The values are likely relevant to your business logic and should be handled there for finer-grained error handling, logical branching, etc.Your validation code is 293 characters and 8 lines, the example code is 95 characters and 6 lines and does more tests.
And that's not even including your helpers which if one uses that lib, they wont have to write.
So, you wrote like 4 times the code, and made it even less flexible (eg. hardcoding the test for 100 in your isTestScore check).
We don't "need" an abstraction for anything, in the sense that we need oxygen. We _want_ abstractions, and they usually make us more productive, which is something different.
Case in point the above horribly noisy procedural code for something that this library abstracts into neat, purpose specific, calls.
I think the code answers its own question.
It's more overhead than simple functional approaches combined with conditional statements, but I think it's more approachable. Otherwise I find it doesn't leave too much of a margin for error and it's reasonably fast.
It's a personal preference, but I prefer it far more than a fluent interface. I actually avoid those now despite really loving them in the past.
Best I've found is a few libraries supporting JSON Schema.
const num= 74;
( (typeof num === 'number') && (num > -1 && num < 101) && (num % 2 === 0) && (num !== 32) )
compared to their first example: v8n().number().between(0, 100).even().not.equal(32).test(74);I read the code, and I do not think this library is suitable for real-world use.
Consider `makeTestType` (https://github.com/imbrn/v8n/blob/master/src/v8n.js#L973-L98...):
return () => value => {
return (
typeof value === type ||
(value === null && type === "null") ||
(Array.isArray(value) && type === "array")
);
};
This means that null and "null" (as a string) are now numbers. v8n().number().test("null") // true
It also means that an array of numbers is a number... something that is counterintuitive for the user of the library.In addition to that, most of the time you want to deal with finite numbers. A validation library should have an API that reflects this, but this is not the case for v8n.
`makeTestType("null")()(null)` return true, but `makeTestType("number")()(null)` doesn't. The signature of `makeTestType` is `type: String => () => value: Any => Boolean`.
https://github.com/cross-check/cross-check/tree/master/packa...
It supports 'draft data', which can be used by auto-safe functionality or similar.