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.

Required by default

See original GitHub issue

joy has a option presence: 'required' adding required() to all schemas. Is there an analog in yup?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jquensecommented, Nov 13, 2022

yeah add an extends this is required due to breaking changes in TS 4.8

0reactions
karlhorkycommented, Nov 12, 2022

It seems like adding extends AnyObject to TContext as suggested by the error message above makes the error go away:

export function requiredBoolean(
  msg?: Parameters<typeof boolean.prototype.required>[0],
): BooleanSchema<NonNullable<boolean>>;
export function requiredBoolean<
  T extends boolean = boolean,
  TContext extends AnyObject = AnyObject,
>(
  msg?: Parameters<typeof boolean.prototype.required>[0],
): BooleanSchema<NonNullable<T>, TContext>;

export function requiredBoolean<
  T extends boolean = boolean,
  TContext extends AnyObject = AnyObject,
>(
  msg?: Parameters<typeof boolean.prototype.required>[0],
): BooleanSchema<NonNullable<T>, TContext> {
  return boolean<T, TContext>().required(msg);
}

export function requiredNumber(
  msg?: Parameters<typeof number.prototype.required>[0],
): NumberSchema<NonNullable<number>>;
export function requiredNumber<
  T extends number = number,
  TContext extends AnyObject = AnyObject,
>(
  msg?: Parameters<typeof number.prototype.required>[0],
): NumberSchema<NonNullable<T>, TContext>;

export function requiredNumber<
  T extends number = number,
  TContext extends AnyObject = AnyObject,
>(
  msg?: Parameters<typeof number.prototype.required>[0],
): NumberSchema<NonNullable<T>, TContext> {
  return number<T, TContext>().required(msg);
}

export function requiredString(
  msg?: Parameters<typeof string.prototype.required>[0],
): StringSchema<NonNullable<string>>;
export function requiredString<
  T extends string = string,
  TContext extends AnyObject = AnyObject,
>(
  msg?: Parameters<typeof string.prototype.required>[0],
): StringSchema<NonNullable<T>, TContext>;

export function requiredString<
  T extends string = string,
  TContext extends AnyObject = AnyObject,
>(
  msg?: Parameters<typeof string.prototype.required>[0],
): StringSchema<NonNullable<T>, TContext> {
  return string<T, TContext>().required(msg);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Joi - make everything required by default? - Stack Overflow
You can use presence option to make fields required by default. Example: const mySchema = joi.object({ thing1: joi.string(), ...
Read more >
Defining required fields and default values - Documentation
1. Choose Tools > Administration > Required Fields & Default Values. · 2. Select an item Type. · 3. Select the field group...
Read more >
Rails 5 makes belongs_to association required by default
In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default after this change....
Read more >
Does it make sense to make a field mandatory which has a ...
If yes then it should be mandatory or else it should not. My strategy is to keep minimum fields mandatory and some sufficient...
Read more >
Default value | Opis JSON Schema
You can specify a default value for an item using the default keyword. When a data doesn't have a corresponding value, the value...
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