Skip to content

Access current key as a parameter of the schema validator to generate dynamically styled labels #2947

Open
@SimonGodefroid

Description

@SimonGodefroid

I'm trying to achieve the following...

export const myValidationSchema = Joi.object<Whatever>({
  myField: Joi.boolean().required().label('My field'),
  myOtherField: Joi.boolean().required().label('My other field'),
}).unknown(false);

but without having having to specifically write the label by hand. Essentially the naming of our attributes is such that it'd be okay to pass a function accessing to this to the .label method so that we could just return the attribute name but in a human readable way.

I'm suggesting an implementation by giving an example but there must be a way to achieve this that I'm not aware of (maybe with all the $_ properties).

const toTitleCase = (label:string) => _.startCase(label);
 
export const myValidationSchema = Joi.object<Whatever>({
  myField: Joi.boolean().required().label(toTitleCase),
  myOtherField: Joi.boolean().required().label(toTitleCase),
}).unknown(false);

Which would make my validators spit an error like "My field is required"

Hope you understand what I'm trying to achieve.

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportQuestions, discussions, and general support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions