Skip to content

Question: how to extend joi to add some key by rule? #1201

@XGHeaven

Description

@XGHeaven

Context

  • node version: 7.10.0
  • joi version: 10.3.1
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information:

Question

I use a schema in many place, such as

joi.object().keys({
    user: joi.string().required(),
    pwd: joi.string().required(),
})

but i don't like export it as a variable then import it to use. i want a easy way.

joi.object().userinfo()

I don't know how to implement by joi.extend(). But I use follow code:

joi.object().constructor.prototype['userinfo'] = function(){
    return this.concat({
        user: joi.string().required(),
        pwd: joi.string().required(),
    })
}

so anyone knows a elegance way??

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions