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

Missing language entry in joi.extend causes obscure runtime error #1230

Closed
SimonSchick opened this issue Jun 27, 2017 · 6 comments
Closed

Missing language entry in joi.extend causes obscure runtime error #1230

SimonSchick opened this issue Jun 27, 2017 · 6 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@SimonSchick
Copy link

Context

  • node version: 7.10.
  • joi version: 10.6.0
  • environment: node
  • used with: standalone

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

const joi = require('joi');
const ourJoi = joi.extend({
    base: joi.string(),
    name: 'string',
    language: {
        // Note: Missing translation here!
    },
    rules: [
        {
            name: 'something',
            validate (params, value, state, options) {
                if (value !== '0') {
                    return this.createError('string.something', {
                        v: value,
                    }, state, options);
                }
                return value;
            },
        },
    ],
});

ourJoi.validate('test', ourJoi.string().something()); // <<<< Error Here

Which result you had ?

Upon using the rule via .validate an error gets thrown.

TypeError: Cannot read property 'length' of undefined
    at Object.toString (XXX/node_modules/joi/lib/errors.js:100:31)
    at processErrors (XXX/node_modules/joi/lib/errors.js:153:36)
    at Object.exports.process (XXX/node_modules/joi/lib/errors.js:176:22)
    at type._validateWithOptions (XXX/node_modules/joi/lib/types/any/index.js:651:31)
    at Object.root.validate (XXX/node_modules/joi/lib/index.js:121:23)

What did you expect ?

I would expect to get an error upon calling extend telling me that the language entry is required.

@Marsup Marsup added the request label Jun 27, 2017
@Marsup
Copy link
Collaborator

Marsup commented Jun 27, 2017

It can't be done at extension time as it would mean parsing your code, but it can certainly be improved at validation time.

@SimonSchick
Copy link
Author

Can you not check that a property in language exists for each name property in rules, or are these are not strictly linked?

@Marsup
Copy link
Collaborator

Marsup commented Jun 27, 2017

No, new rules might re-use existing errors or throw several kinds, that's impossible.

@SimonSchick
Copy link
Author

Ah ok, makes sense 🙂

@Marsup Marsup added the non issue Issue is not a problem or requires changes label Jul 20, 2017
@Marsup Marsup self-assigned this Jul 20, 2017
@Marsup Marsup closed this as completed Jul 20, 2017
@Marsup Marsup removed the non issue Issue is not a problem or requires changes label Jul 20, 2017
@Marsup Marsup reopened this Jul 20, 2017
@Marsup
Copy link
Collaborator

Marsup commented Jul 20, 2017

Closed by mistake, sorry.

@Marsup Marsup marked this as a duplicate of #1250 Jul 20, 2017
@Marsup Marsup closed this as completed in e7cff68 Oct 9, 2017
@Marsup Marsup added this to the 11.3.4 milestone Oct 9, 2017
@SimonSchick
Copy link
Author

ty

@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants