Closed Demogorrgon closed 5 years ago
Hi @oreshkoandrei It is in the readme https://github.com/Salesflare/joi-phone-number#how Is there anything not clear about it?
Hi @oreshkoandrei It is in the readme https://github.com/Salesflare/joi-phone-number#how Is there anything not clear about it?
hm, but it require to pass phone in validate(), actually. Any way to avoid this ?
i mean something like this in schema: phoneNumber: Joi.phoneNumber()
I don't understand your question. Do you have an example of what you are trying to do and what it not working or what you expect it to do?
This rule adds .phoneNumber()
onto .string()
so you have to do Joi.string().phoneNumber()
not Joi.phoneNumber()
Okey, i'm trying to do it simply as
export default Joi.object().keys({
id: Joi.number().greater(0),
firstName: Joi.string().required(),
lastName: Joi.string().required(),
email: Joi.string().email().required(),
phoneNumber: myCustomJoi.string().phoneNumber() // <--
});
but seems it doesn't work and i have to use validate() method and pass req.body.phoneNumber in it
I see, you need to use your custom Joi everywhere.
For example what we did was create a scoped joi package @salesflare/joi
that we require everywhere instead of Joi itself. The whole api stays the same but we get the extensions easily (see https://github.com/Salesflare/joi/blob/master/lib/index.js)
So we do
const Joi = require('@salesflare/joi');
And is the only change in our code.
You should do
export default myCustomJoi.object().keys({
id: myCustomJoi.number().greater(0),
firstName: myCustomJoi.string().required(),
lastName: myCustomJoi().required(),
email: myCustomJoi.string().email().required(),
phoneNumber: myCustomJoi.string().phoneNumber()
});
The more detailed explanation is, is that hapi calls validate for you when doing payload validation. So it calls .validate onto that schema. In your case that schema is made with vanilla joi causing it to validate with vanilla joi. Which doesn't know what that phonenumber rule thing is. If you make your schema with your custom joi it will know about the custom rule and it should work I believe.
Looks like, it now works in some way, thanks you very much But i can't get why are two digits is a valid phone number )
Take a look at https://github.com/google/libphonenumber (which is what this module uses), there are some interesting links there as well. Phone numbers can be weird :D
Take a look at https://github.com/google/libphonenumber (which is what this module uses), there are some interesting links there as well. Phone numbers can be weird :D
Thanks
No problem!
I also can't work out why 112 is valid when passing NZ
as defaultCountry
import HapiJoi from "@hapi/joi";
const Joi = HapiJoi.extend(require("joi-phone-number"));
const validationSchema = Joi.object().keys({
phone: Joi.string()
.phoneNumber({ defaultCountry: "NZ", format: "national" })
.required()
});
http://libphonenumber.appspot.com/phonenumberparser?number=112&country=NZ&geocodingLocale=en-NZ
I also can't work out why 112 is valid when passing
NZ
as defaultCountryimport HapiJoi from "@hapi/joi"; const Joi = HapiJoi.extend(require("joi-phone-number")); const validationSchema = Joi.object().keys({ phone: Joi.string() .phoneNumber({ defaultCountry: "NZ", format: "national" }) .required() });
http://libphonenumber.appspot.com/phonenumberparser?number=112&country=NZ&geocodingLocale=en-NZ
Try { defaultCountry: "NZ", format: "national", strict: true }
Hello
Is there a way to use it in joi schemas ?