Skip to content
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

Extending a schema #710

Closed
bsiddiqui opened this issue Aug 19, 2015 · 5 comments
Closed

Extending a schema #710

bsiddiqui opened this issue Aug 19, 2015 · 5 comments
Assignees
Labels
bug Bug or defect support Questions, discussions, and general support
Milestone

Comments

@bsiddiqui
Copy link

Is there a way to overwrite a key in the schema or add to the validation parameter?

Overwrite a key:

var base = Joi.object().keys({
    a: Joi.number(),
    b: Joi.string()
});

var extended = base.keys({
    a: Joi.string()
});

Add a parameter:

var base = Joi.object().keys({
    a: Joi.number(),
    b: Joi.string()
});

var extended = base.keys({
    a: Joi.number().min(10)
});
@danielb2
Copy link
Contributor

FYI, if the object is top level, you can use a regular object as opposed to a Joi.object()

But this looks like a bug. I would think the desired behavior is what @bsiddiqui is looking for

var Joi = require('./');

var base = Joi.object().keys({
    a: Joi.number(),
    b: Joi.string()
});
var extended = base.keys({ a: Joi.boolean() });

> Joi.assert({ a: 3, b: 'sdcsd'}, extended);
Error: {
  "b": "sdcsd",
  "a" [1]: 3
}
[1] "a" must be a boolean

> Joi.assert({ a: true, b: 'sdcsd'}, extended);
Error: {
  "b": "sdcsd",
  "a" [1]: true
}
[1] "a" must be a number

@danielb2
Copy link
Contributor

test case

      it('alters existing keys', function (done) {

          var a = Joi.object({ a: 1 });
          var b = a.keys({ a: 2 });

          b.validate({ a: 1 }, function (err, value) {

              expect(err).to.exist();
              b.validate({ a: 2 }, function (err2, value2) {

                  expect(err2).to.not.exist();
                  done();
              });
          });
      });

@danielb2
Copy link
Contributor

Or alternatively, it should give an error when trying to alter the key

@Marsup
Copy link
Collaborator

Marsup commented Aug 19, 2015

Is there a question here ? Looks like you put the question and answers in the same post.
You can also use concat to add on existing keys.

@Marsup Marsup closed this as completed Aug 19, 2015
@Marsup Marsup added the support Questions, discussions, and general support label Aug 19, 2015
@Marsup Marsup self-assigned this Aug 19, 2015
@bsiddiqui
Copy link
Author

@Marsup I put what I'd expect the result to be but that's now how Joi is working for me

var base = Joi.object().keys({
    a: Joi.number(),
    b: Joi.string()
});

var extended = base.keys({
    a: Joi.string()
});

Joi.validate({ a: 'hello' }, extended)
// => ValidationError: child "a" fails because ["a" must be a number]

I would expect extended's schema to overwrite base

@Marsup Marsup reopened this Aug 19, 2015
@Marsup Marsup added this to the 6.7.0 milestone Aug 19, 2015
@Marsup Marsup closed this as completed in aac5b70 Aug 19, 2015
@Marsup Marsup added the bug Bug or defect label Aug 20, 2015
ck-lee pushed a commit to ck-lee/joi that referenced this issue Sep 30, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

3 participants