Skip to content

Add support for recursive warnings #2739

Open
@imjoshin

Description

@imjoshin

Context

This feature requests comes from the discussion in #2669.

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

What problem are you trying to solve?

We currently have the ability to set warnings on unknown keys within an object of Joi, but not recursively. It would be nice to have the ability to say "I have an object and if I receive any unknown keys at any depth, I'd like to warn the user." The following technically works, but is not great as it can hide future problems validating joi config.

const wrappedJoiObjectWithUnknownWarnings = <TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema> =>
  joi.object(schema)
    .pattern(/.*/, joiInstance.any().warning(`any.unknown`))

joiInstance.object = wrappedJoiObjectWithUnknownWarnings

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

Joi.object().pattern(/.*/, Joi.any().warning('any.unknown'));

Given the single-level warning above, I'm not sure what the best approach here would be. Attaching a recursive argument into one of these would make sense, but doesn't feel right in any of the method calls. My initial idea is to add a Joi.rules or some other top-level method to set up these types of controls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions