-
Notifications
You must be signed in to change notification settings - Fork 18
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
"when" operator? #113
Comments
Sorry for the slow response. It looks like we haven't implemented it, sorry. I also see we haven't implemented |
Hi, is there a way to perform My use case is to require one of two fields only. For example, I want to require |
@dsc-leo Great idea to use Below is the test I wrote to verify this. I wrapped the it('when(), via custom()', async () => {
function exclusiveOr<T>(schema: Joi.AnySchema, propertyName: keyof T & string) {
return schema.when(propertyName, {
is: Joi.required(), // Check if the "other" property is defined
then: Joi.forbidden(), // If so, the current schema "forbids" any value except undefined
otherwise: Joi.required(), // Otherwise, the current schema rejects undefined values
});
}
class ClassToValidate {
@string().custom(({ schema }: { schema: Joi.Schema }) => exclusiveOr<ClassToValidate>(schema, 'phone'))
public email?: string;
@lazy(({ joi}) => exclusiveOr<ClassToValidate>(joi.string(), 'email'))
public phone?: string;
constructor() {}
}
let instance = new ClassToValidate();
instance.email = 'foo@bar.com';
expect(instance).toBeValid();
instance = new ClassToValidate();
instance.phone = '555-5555';
expect(instance).toBeValid();
instance = new ClassToValidate();
instance.email = 'foo@bar.com';
instance.phone = '555-5555';
expect(instance).not.toBeValid();
instance = new ClassToValidate();
expect(instance).not.toBeValid();
}); |
I'll create a pr for this, if you didn't start working on it. |
Is it possible to use the Joi "when" operator? In Joi, one can do:
Joi .string() .valid.apply(this, days) .required() .when("action", { is: "DELETE", then: Joi.string().optional(), otherwise: Joi.string().required() })
However, with jf,
@(jf .string() .valid.apply(this, days) .required() .when("action", { is: "DELETE", then: jf.string().optional(), otherwise: jf.string().required() }))
I receive an error:
The text was updated successfully, but these errors were encountered: