Skip to content

Toggle brackets when validating UUID's #3081

Open
@ben-walters

Description

@ben-walters

Runtime

nodejs

Runtime version

22.16.0 (LTS)

Module version

17.13.3

Used with

No response

Any other relevant information

No response

What problem are you trying to solve?

I noted an error with our API when validating UUID's. A consumer of our api passed what we believed was an erroneous value which passed validation:

// user passed
?uuid=[ba5d05aa-fc70-404f-85c1-0be5d3746f39]

// schema specified
uuid: joi.string().uuid()

I expected this to fail as [ba5d05aa-fc70-404f-85c1-0be5d3746f39] is not a valid uuid as a string. To mitigate this we implemented a realUuid() extension, but felt that it might be useful to others that had encountered similar issues to allow the brackets on the current uuid() function to be configurable.

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

I have authored a PR which allows a user to provide:

`wrapper` - defines the allowed or required GUID wrapper characters where:
    -   `undefined` - (default) the GUID can be optionally wrapped with `{}`, `[]`, or `()`. The opening and closing characters must be a matching pair.
    -   `true` - the GUID must be wrapped with `{}`, `[]`, or `()`. The opening and closing characters must be a matching pair.
    -   `false` - wrapper characters are not allowed.
    -   `'['`, `'{'`, or `'('` - a specific wrapper is required (e.g., if `wrapper` is `'['`, the GUID must be enclosed in square brackets).

Example:

joi.uuid()                  // retains current functionality

joi.uuid({wrapper: true})   // uuid MUST be wrapped by any of the allowed brackets
joi.uuid({wrapper: false})  // uuid MUST NOT be wrapped in any of the allowed brackets
joi.uuid({wrapper: '{'})    // uuid MUST be wrapped by { }
joi.uuid({wrapper: '['})    // uuid MUST be wrapped by [ ]
joi.uuid({wrapper: '('})    // uuid MUST be wrapped by ( )

This is my first PR to a public repo, so I welcome any feedback! Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions