Feature Request: originalValue in test
See original GitHub issue🍻 I’d like to propose making the test
function a bit more useful for complex tests:
Feature Use Case
We have the need to assert that a string is a valid date, that it isn’t before a particular date, and that it matches a certain format. As such, we’ve chosen to use test
for that, and this is what we’ve come up with:
const schema = yup.object().shape({
untilDate: yup
.date()
.min('01-01-2020')
.test('format', '${path} must match the mm-dd-yyyy format', function () {
return reDate.test((this.options as any).originalValue);
}),
});
If we use value
there, it contains a full ISO string value. e.g. '2020-08-11T23:58:15.712Z'
, and we want to validate what was passed, not what the transformed/translated value is.
The (this.options as any)
bit there is TypeScript, but it signifies that originalValue
is not documented on this.options
within the test
function. It does exist, but it’s not been officially documented. And as #207 suggests, it may not always be present. That makes using it in our scenario a little dicey.
Feature Proposal
I’d like to propose adding original
value to the test
function handler parameters. Nearly identical to the signature for transform
, so that the test
function would resemble:
const schema = yup.object().shape({
untilDate: yup
.date()
.min('01-01-2020')
.test('format', '${path} must match the mm-dd-yyyy format', function (value, originalValue) {
return reDate.test(originalValue);
}),
});
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
Understood. I’ll see about fitting this into our next sprint.
After reading the discussions and also the other closed issue I decided to take and work on this. It’s my First PR here, so any feedback and code review are welcome @shellscape and @jquense