-
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
Check if parent is undefined, then check children #106
Comments
@benneq I will try to provide a detailed answer to your problem tomorrow! |
That is a very good example. What it would need is a way to define validation functions that validate the parent data and depending on the result either stop or continue validating. Maybe we can add a special function to spected, that enables to define a chain of functions, that stop validating as soon as one validation functions fails, and continues to validate deeper nested data as long as the higher level data is valid. Not sure how this might look like, but maybe something like this: const rules = {
foo: runValidations([
[a => a !== null, "no null"],
[a => a !== undefined, "no undefined"],
{
bar: barRules
}
])
} |
Is this somehow possible using a validation function? I still don't really know how this ramda stuff works, though I write some pseudo code)
The question is: Is it already possible to stop after the first error using some fancy ramda stuff? I think the whole process should stay functional. That's what this lib is all about. Some more pseudo code:
|
No, that isn't possible right now. spected runs all functions and collects all messages at the moment. |
But I'm sure, you could write a function, that does this internally :) A simple function that returns an array of validation rules. But this function does the validation itself ... kinda... Like the Would be really nice if you would help with the TypeScript stuff. Then I could play around with that stuff myself! I just need the function signature(s?) for this:
|
I will take a look at the types! Sure, we can implement it internally, it should be straight forward to use, I think this is the important part. |
Okay, I now found a way, to make this possible... It's not very nice, and it's even not working for now, because spected has some issues with null / undefined values 😢 Here's the code that (in my opinion) should work:
If you could make some changes to spected, to get this working, it should be quite easy to make a custom function for this, so you can then simply write:
Then it should also be possible to write some kind of |
The problem is that spected currently always expects an array or object as input, so the problem is recursion. I will see how we can improve this. |
Yeah, I saw that. Maybe there should be some "catch" at the beginning of the |
Now I'd like to validate
foo.bar
(if present), which works fine as long asfoo
is notundefined
. If it's undefined, there'll be an exception invar keys = Object.keys(inputFn())
.Here's a real world example I'd like to use:
foo === undefined
, it is VALID.foo === null
, it is INVALID: "must not be null"typeof foo !== 'object'
, it is INVALID: "must be an object"foo.bar <= 9000
, thenbar
is INVALID: "must be over 9000!"Is there some way to get this working?
The result objects then might looks like this:
The text was updated successfully, but these errors were encountered: