Skip to content

Allow 0x prefix for hex strings #2386

@richardpringle

Description

@richardpringle

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise):
    Community
  • is this issue currently blocking your project? (yes/no):
    no
  • is this issue affecting a production system? (yes/no):
    yes

Context

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

What problem are you trying to solve?

While I can just solve this with a regex, it would be much cleaner if this was part of the Joi API.
JavaScript's parseInt already natively handles the 0x prefix.

const somePrefixedHexString = '0x123'; 
Joi.string().hex().validate(somePrefixedHexString); // error

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

It would be amazing if I didn't have to store that regular-expression somewhere and I instead could do the following:

const somePrefixedHexString = '0x123'
Joi.string().hex({ prefix: true }).validate(somePrefixedHexString); // passes validation

Metadata

Metadata

Assignees

Labels

bugBug or defectfeatureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions