Joi: Create a custom validator rule

Created on 8 Mar 2018  Â·  5Comments  Â·  Source: sideway/joi

Context

  • node version: 9.2.0
  • joi version: 12.0.0
  • environment (node, browser): TypeScript, Express
  • used with (hapi, standalone, ...): standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

Hi guys,

I'm wondering if i can create a custom validation rule.
I currently have this schema :

const schema = Joi.object().keys({
    businessName: Joi.string().required(),
    slug: Joi.string().regex(SLUG).required(),
    description: Joi.string().required(),
}).required();

And i want to had another rule to my slug for it to be uniq by checking in my DB, so i would like to have something like :

const schema = Joi.object().keys({
    businessName: Joi.string().required(),
    slug: Joi.string().regex(SLUG).isSlugUniq().required(),
    description: Joi.string().required(),
}).required();

Do you know if i can do this ? Can you tell me how ? (I did not find anything about this on issues list )

Thank you

EDIT : Ok i filtered my issue with not closed. I found this : https://gist.github.com/Marsup/14597d0c8eaa10c4addb and currently reading. My issue is on stand-by. I'll close it if this answers my question.

support

Most helpful comment

The enthusiasm of developers doesn't have much influence on the time I can spend on it unless they are enthusiastic enough to do PRs :)

All 5 comments

You can't, extensions are not yet async, unless you find a sync way this is not possible.

Ah ! But this article for Joi 7.0.0 seems to indicates otherwise… But ok.

This is not an article, this is a proposal.

Ah ^^ ok. I thought after 7 month and the enthousiasm of developers it would have been implemented :)
Thank you anyway.

The enthusiasm of developers doesn't have much influence on the time I can spend on it unless they are enthusiastic enough to do PRs :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sergibondarenko picture sergibondarenko  Â·  3Comments

a-c-m picture a-c-m  Â·  3Comments

n-sviridenko picture n-sviridenko  Â·  3Comments

REBELinBLUE picture REBELinBLUE  Â·  3Comments

normancarcamo picture normancarcamo  Â·  3Comments