-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Question: how to extend joi to add some key by rule? #1201
Comments
If you don't want to export the base schema and compose your subschema from it, I would definitely use Joi's Check out #1179 for a more detailed discussion of a similar extension. That being said, since you aren't adding new rules or data coercion or anything, composing with Node honestly will be the easiest... // base.js
const Joi = require('joi');
module.exports = Joi.object().keys({
user: joi.string().required(),
pwd: joi.string().required()
}); // any other file
const BaseSchema = require('./base.js');
const extendedSchema = BaseSchema.keys({ /*additional keys here*/ });
/*
as opposed to using the userInfo extension:
const extendedSchema = Joi.object().keys({ /*additional keys here*/ }).userInfo();
/* |
@WesTyler joi.extend({
name: 'object',
base: joi.object(),
rules: [{
name: 'userinfo',
setup(param) {
this.concat({
user: joi.string().required(),
pwd: joi.string().required(),
})
}]
}) |
Looks to me like you just want : Joi.extend({
name: 'userinfo',
base: Joi.object().keys({
user: joi.string().required(),
pwd: joi.string().required(),
})
}) |
@Marsup |
@XGHeaven I don't understand why it is necessary to write Joi.object().keys({
user: joi.string().required(),
pwd: joi.string().required(),
}) |
@DavidTPate I think |
Gotcha, you can find some additional examples of Joi.extend() here. |
It doesn't seem like a very hard one to support, but I don't think it's possible right now. I'll keep this around as a request for when I have time to do it, or anyone feel free to try. It's a small modification of the |
I suggest that:
I'm trying to do this. |
#1201 - setup can return joi object to replace origin schema
My solution has been:
So now the object as
would give error where createdOn is required and createdBy and createdOn need to be paired. |
Hey @XGHeaven, How did you get this working with Joi 17.1.1 I am struggling with a similar situation and thought you would be kind enough to help me with this. :) |
@katlimruiz your solutions works for me to some extent, where i can extend keys. I also need to add few more keys in the final schema with out modifying base schema. My base schema is
And my final schema would be
In this case my final schema would have keys
My actual requirement is also to add few more fields like
So how can we add few more fields inside the nested array in the ItemSchema i.e. base schema. The ItemSchema should be intact and the additional fields should be part of OrderSchema only. Please let me know the solution if anyone has idea on it. |
Context
Question
I use a schema in many place, such as
but i don't like export it as a variable then import it to use. i want a easy way.
I don't know how to implement by
joi.extend()
. But I use follow code:so anyone knows a elegance way??
The text was updated successfully, but these errors were encountered: