-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation on the root element #104
Comments
Thanks for the feedback, will try to see how we can approach this as soon as I have some time. Should find some time this week! |
Thank you! :) I guess root validation would basically just the code "within" the Something like this might work (pseudo code): const validate = curry((successFn: Function, failFn: Function, spec: Object, input: Object): Object => {
const inputFn = typeof input === 'function' ? input : (key?: string) => key ? input : input
const input = inputFn();
if(typeof input === 'object') {
const keys = Object.keys(inputFn())
return reduce((result, key) => {
// ... just like it was before
}
} else {
// here comes the code from within the `reduce` function
// i just removed all the "key" and "...result" stuff
// i've not tested this at all
const inputObj = input;
const value = inputObj
const predicates = spec
if (Array.isArray(predicates)) {
return transform(() => successFn(value), failFn, map(f => runPredicate(f, value, inputObj, key), predicates)
} else if (typeof predicates === 'object') {
return validate(successFn, failFn, predicates, value)
} else if (typeof predicates === 'function') {
const rules = predicates(value)
return validate(successFn, failFn, rules, value)
} else {
return successFn([])
}
}
}) Btw. could you make the const inputFn = typeof input === 'function' // if input === 'function'
? input // then return input
: (key?: string) => key // else if ???
? input // then return input
: input // else return input The "else if" part doesn't make sense to me. Or maybe (more likely) I'm just reading it wrong Using this formatting, ternary operators (even nested) are really easy to read:
|
I want to validate a single (non-object) value, like this:
I get the following error:
Another use case would be something like this:
Or maybe: Is object or array? (both are valid). And then apply validation either to the object, or to all elements of the array.
The text was updated successfully, but these errors were encountered: