`enforce()` with property name passed as string parameter doesn't work
bttger opened this issue · comments
The getting started shows the usage of enforce
with the property name passed as a string parameter. The field always stays blank regardless of the data object and thus it always hasErrors
.
import { create, test, enforce, only } from 'vest';
export const signUpSuite = create((data = {}, fieldName?: string) => {
if (fieldName) only(fieldName);
test('email', 'Please enter your email address', () => {
enforce('email').isNotEmpty().isNotBlank();
});
});
It only works with enforce(data.email).isNotEmpty().isNotBlank();
.
Is this intended? It caused a lot of confusion because I haven't seen this notation in the docs, though it makes sense to directly access the data object's properties.
Environment:
Node: 16.11
Vest: 4.0.0-next-6abc96
@bttger, thanks for reporting this.
I think this is an error on the docs. Enforce doesn't infer the data from the passed object, but instead, you need to pass it whatever data is actually needed. I published a fix to the docs. Really sorry about the confusion.
By the way, you can drop the if (fieldName)
before only
. only
is able to deal with undefined values :)
Thank you for fixing it. And thanks for the only
hint, I found it in the docs now.