Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I warn on Unkown #2669

Closed
moonmeister opened this issue Sep 3, 2021 · 8 comments
Closed

Can I warn on Unkown #2669

moonmeister opened this issue Sep 3, 2021 · 8 comments
Assignees
Labels
support Questions, discussions, and general support

Comments

@moonmeister
Copy link

moonmeister commented Sep 3, 2021

Support plan

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v14.17.4
  • module version: v17.4.2
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): Part of the Gatsby project
  • any other relevant information:

How can we help?

We don't want to strip unknown object keys but we do want to throw a warning to user.

We're using validateAsync and must do so as we're using external.

allowUnkown disables errors, but doesn't let us catch unknown keys as warnings.

An initial implementation caught errors and filtered the error type on object.unknown and warned based on that. However, because this was catching a thrown error from Joi that meant the rest of our code in the try block stopped running and we can't return the modified value that should be returned thus causing a bug I just found. This is still true even with abortEarly: false.

I need a way to either catch and filter errors before they throw or to make is so unknowns are allowed but return in the warning object. Is this possible?

@moonmeister
Copy link
Author

Still hoping to solve this problem. I'm happy to submit a PR that enables returning errors from validateAsync. Would that be accepted?

@hueniverse
Copy link
Contributor

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

@hueniverse hueniverse added the support Questions, discussions, and general support label Dec 2, 2021
@hueniverse hueniverse self-assigned this Dec 2, 2021
@moonmeister
Copy link
Author

Thanks, much appreciated.

@imjoshin
Copy link

imjoshin commented Feb 2, 2022

Hello @hueniverse ! I'm curious if you have any ideas for marking Joi.object().pattern(/.*/, Joi.any().warning('any.unknown')); recursively. I acknowledge this isn't the greatest approach, but I couldn't think of a better way to do this at a high level without reaching into the internals of Joi.

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

joiInstance.object = wrappedJoiObjectWithUnknownWarnings

This basically wraps any .object call to also throw warnings on unknown fields, using the example above. From what I can see, it works, but is not great.

@hueniverse
Copy link
Contributor

@j0sh77 Are you looking to turn an error into a warning globally?

@tyhopp
Copy link

tyhopp commented Feb 7, 2022

@hueniverse In a nutshell yes, for a given schema we want to turn all unknown key errors into warnings.

The issue we're facing now when trying the solution you shared is that it works for the root level keys but not for nested object keys.

I've tried using Joi link to achieve this but with no luck.

@hueniverse
Copy link
Contributor

Please open a new issue for recursive warning support and I'll try to get to it.

@imjoshin
Copy link

imjoshin commented Feb 8, 2022

Thank you! Filed #2739.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

4 participants