Open bookercodes opened 7 years ago
Thanks for the feedback, will have a look.
You need to change the isGreaterThan
function, which is being run immediately as you're calling it with isGreaterThan(5)
. Change it to accept two arguments like a => b => b > a
.
const spected = require('spected').default
const isGreaterThan = a => b => b > a
const hasCapitalLetter = text => x => true
const validationRules = {
name: [
[ isGreaterThan(5),
`Minimum Name length of 6 is required.`
],
],
random: [
[ isGreaterThan(7), 'Minimum Random length of 8 is required.' ],
[ hasCapitalLetter, 'Random should contain at least one uppercase letter.' ],
]
}
const inputData = { name: 'abcdef', random: 'z'}
const res = spected(validationRules, inputData)
console.log(res)
Ah, I see what is happening, thanks!
What do you think about an internal check like,
if (typeof predicate != "function") {
throw new Error("The validator you supplied is not a function")
}
?
Hard to tell at this early stage, but it seems like it could be a common error.
Yes, it makes sense to handle invalid rules. Instead of throwing an error we can handle it differently.
In dev mode we can issue a console warning. Added a warning function https://github.com/25th-floor/spected/blob/master/src/utils/warning.js
In any case we should skip validating and return true instead, treat it the same as if no predicate function was defined. Currently passing in { name: [] }
will return valid. If no function is provided, we skip and return true.
In
index.js
,When I run
node index.js
, I get this error: