-
Notifications
You must be signed in to change notification settings - Fork 95
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
Plans to add Validation type #152
Comments
This library has grown without much of a plan. If you're interested in creating one, I'm sure people would be interested. If you're looking for others who might be able to build one, you'll have to pique their interests; it would probably help to explain why you think this would be helpful. |
Personally I have found that using Either's disjunction nature you can implement validations with ease. The difference will mainly lie in the wording used for success/failure (left, right). Given its a monad and that it implements the bifunctor spec you have a lot of flexibility as to how you manage the two possible paths. |
I haven't used |
For such a use case you could make an Either of a Tuple where you store the data to be validated and a validation state array where you can store the errors. Upon Left, add error to the array while keeping the value, upon right, pass the previous input along (or viceversa, which ever rocks your boat). Then you'd just pass a checker function configured with a condition to both sides with a bimap call. There will be other ways do this of course. Any ideas? |
@ferborva Is this what you're talking about? Note I've had to do this using Folktale instead of ramda-fantasy, because this library doesn't expose a const Either = require('data.either')
// Either<X, Y>[] => Either<X[], Y[]>
const sequenceEithers = eithers => (
eithers
.reduce((acc, either) => (
acc.fold(
accLeft => either.fold(
left => Either.Left(accLeft.concat(left)),
right => Either.Left(accLeft)
),
accRight => either.fold(
left => Either.Left([left]),
right => Either.Right(accRight.concat(right))
)
)
), Either.Right([]))
)
{
const eithers = [
Either.Left(1),
Either.Left(2),
]
console.log(sequenceEithers(eithers))
}
{
const eithers = [
Either.Right(1),
Either.Right(2),
]
console.log(sequenceEithers(eithers))
}
{
const eithers = [
Either.Left(1),
Either.Left(2),
Either.Right(3),
Either.Right(4),
]
console.log(sequenceEithers(eithers))
} |
Morning @OliverJAsh. I was thinking more a long the lines of something like this:
You put the value into a Tuple inside an Identity container in order to map predicate validations while you accumulate the errors in the second position of the Tuple. Once that is done you can return the checkedTuple as is or check the Tuple of errors and place it into a Left or a Right. In case you put the Tuple into an Either you can use 'either' static to manage the outcome:
|
Are there any plans to add a Validation type, like in Folktale? docs.folktalejs.org/en/latest/api/data/validation/Validation.html
The text was updated successfully, but these errors were encountered: