This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Dependent validations for structs #29
Comments
Yes, with a function isValid(x) {
if (t.Dat.is(x.endDate)) {
return x.endDate.getTime() >= x.startDate.getTime();
}
return true;
}
var MyType = t.subtype(t.struct({
startDate: t.Dat,
endDate: t.maybe(t.Dat),
foo: t.Num
}), isValid);
console.log(t.validate({startDate: new Date(2015, 10, 30), foo: 1}, MyType).isValid()); // true
console.log(t.validate({startDate: new Date(2015, 10, 20), endDate: new Date(2015, 10, 21), foo: 1}, MyType).isValid()); // true
console.log(t.validate({startDate: new Date(2015, 10, 20), endDate: new Date(2015, 10, 19), foo: 1}, MyType).isValid()); // false All the the combinators |
Ah, that's brilliant! My next question was going to be how to ensure that the validations get layered so that the original struct's validations also run, but with |
@gcanti instead, i feel like the predicate should have access to the options/context. eg:
Or the predicate could have the same signature as validate. Person struct can be complex and having to set Person as a refinement on struct and then handle all dependent field in one predicate is not ideal imo. makes sense? |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Is it possible to validate one field of a struct depending on the value of another? In particular, I have a startDate and an optional endDate field, and I would like to make sure that endDate is after startDate, if endDate is given.
More specifically, if I do something like
How can I validate
MyType
as a whole? It doesn't seem like struct takes a validator function as an argument?The text was updated successfully, but these errors were encountered: