question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Joi.Forbidden() should prevent property from being output in typescript

See original GitHub issue

When Forbidden and/or Strip is used, the resulting typescript model should not have the entry defined in its model.

export const examplePOSTBodySchema = baseSchema
  .fork(['id'], (x) => x.forbidden().strip(true))
  .meta({className: 'examplePOSTBody'});

Here I am forking a base schema and trying to remove the id parameter. Unfortunately Forbidden is not supported so id is still output in the resulting model.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
shennancommented, Nov 26, 2021

I have a scenario where I use Joi.forbidden() to indicate that the value should be undefined.

So my expected behaviour would be:

const RequestBodySchema = Joi.forbidden()

RequestBodySchema.validate(undefined) // Passes
RequestBodySchema.validate({}) // Error
RequestBodySchema.validate({ foo: 'bar' }) // Error

With this in mind, I think Joi.forbidden() should actually compile to:

type RequestBodySchema = never

Any thoughts on this? Currently it just compiles to any, which I think is wrong.

0reactions
shennancommented, Jul 1, 2022

This doesn’t work in latest version (not tested 4.0.0).

This:

const SomeForbiddenSchema = Joi.forbidden()

Produces this:

export type undefined = any;
Read more comments on GitHub >

github_iconTop Results From Across the Web

17.7.0 API Reference - joi.dev
Validates a value against a schema, returns valid object, and throws if validation fails where: value - the value to validate. schema -...
Read more >
TypeScript Joi: Property 'validate' does not exist on type 'Root'
To Validate, you can use this code below: schema.validateAsync({ email, password, fullName }).then(val => { req.body = val; }).catch(err ...
Read more >
How to use the joi.forbidden function in joi - Snyk
Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately. var uuid = require('node-uuid'); var...
Read more >
joi-to-typescript - npm
This will allow you to use generate TypeScript interfaces from Joi Schemas that can be used to assign types to JavaScript objects or...
Read more >
21.1.0 API Reference - hapi.dev
Plugin-specific configuration which can later be accessed via server.settings.plugins . plugins is an object where each key is a plugin name and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found