Skip to content

Can you allow _ in rule names? #2242

@necojackarc

Description

@necojackarc

Support plan

  • which support plan is this issue covered by?: Community
  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v10.16.3
  • module version: 16.1.8
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): none
  • any other relevant information: none

What problem are you trying to solve?

I am currently working on upgrading Joi v15 to v16 and noticed using _ in rule names is not allowed in v16 because of the following regex.

internals.nameRx = /^[a-zA-Z0-9]+$/;

For instance, with the following extension,

const Joi = require('@hapi/joi')

Joi.extend((joi) => {
  return {
    type: 'string',
    base: joi.string(),
    rules: {
      new_rule: {
        method() {
          return this.$_addRule('new_rule');
        },
        validate(value, helpers, args, options) {
          // validation goes here...
        },
      },
    },
  };
});

You will see this error:

[1] "rules.new_rule" is not allowed
    at Object.exports.process (/node_modules/@hapi/joi/lib/errors.js:183:16)
    at Object.internals.entry (/node_modules/@hapi/joi/lib/validator.js:140:26)
    at Object.exports.entry (/node_modules/@hapi/joi/lib/validator.js:26:30)
    at internals.Base.validate (/node_modules/@hapi/joi/lib/base.js:532:26)
    at Object.internals.assert (/node_modules/@hapi/joi/lib/index.js:221:27)
    at Object.assert (/node_modules/@hapi/joi/lib/index.js:102:19)
    at Object.extend (/node_modules/@hapi/joi/lib/index.js:166:18)
    at Object.<anonymous> (/test.js:3:5)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

Is it possible to allow _ in rule names?

Do you have a new or modified API suggestion to solve the problem?

I would suggest just changing the regex to /^\w+$/(which is equivalent to /^[_a-zA-Z0-9]+$/) but I am not so sure how this would affect the entire functionality.

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions